-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split out a
nylas-streaming
gem (#151)
This lays the foundation for a future `nylas-rails` gem while keeping our build process and core dependencies lightweight. We can now have `nylas` be the rest/http client while `nylas-streaming` includes the event machine and yajl functionality so that users who aren't using those don't pull in unnecessary dependencies or run into dependency resolution conflicts. * Inform users that delta streams are now part of nylas-streaming in the README * Add post install message to ask people to read the upgrade guide
- Loading branch information
Showing
17 changed files
with
211 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
source 'https://rubygems.org' | ||
|
||
gemspec | ||
gemspec name: 'nylas-streaming' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,4 @@ source 'https://rubygems.org' | |
|
||
gem 'rails', '~> 5' | ||
|
||
gemspec path: ".." | ||
gemspec path: "..", name: 'nylas-streaming' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
require 'yajl' | ||
require 'em-http' | ||
require 'nylas' | ||
|
||
module Nylas | ||
class API | ||
def delta_stream(cursor, exclude_types=[], timeout=0, expanded_view=false, include_types=[], &block) | ||
Streaming::Stream.new(api: self, cursor: cursor, timeout: timeout, expanded_view: expanded_view, | ||
exclude_types: exclude_types, include_types: include_types).listen(&block) | ||
end | ||
end | ||
|
||
module Streaming | ||
class Stream | ||
extend Forwardable | ||
def_delegators :api, :url_for_path | ||
attr_accessor :api, :timeout, :expanded_view, :include_types, :exclude_types, :cursor | ||
|
||
def initialize(api: , cursor: , timeout: 0, expanded_view: false, include_types: [], exclude_types: []) | ||
self.api = api | ||
self.cursor = cursor | ||
self.timeout = timeout | ||
self.expanded_view = expanded_view | ||
self.include_types = TypesFilter.new(:include, types: include_types) | ||
self.exclude_types = TypesFilter.new(:exclude, types: exclude_types) | ||
end | ||
|
||
def listen | ||
raise 'Please provide a block for receiving the delta objects' if !block_given? | ||
|
||
exclude_string = exclude_types.to_query_string | ||
include_string = include_types.to_query_string | ||
|
||
# loop and yield deltas indefinitely. | ||
path = self.url_for_path("/delta/streaming?exclude_folders=false&cursor=#{cursor}#{exclude_string}#{include_string}") | ||
if expanded_view | ||
path += '&view=expanded' | ||
end | ||
|
||
parser = Yajl::Parser.new(:symbolize_keys => false) | ||
parser.on_parse_complete = proc do |data| | ||
delta = Nylas.interpret_response(OpenStruct.new(:code => '200'), data, {:expected_class => Object, :result_parsed => true}) | ||
|
||
if not OBJECTS_TABLE.has_key?(delta['object']) | ||
next | ||
end | ||
|
||
cls = OBJECTS_TABLE[delta['object']] | ||
if EXPANDED_OBJECTS_TABLE.has_key?(delta['object']) and expanded_view | ||
cls = EXPANDED_OBJECTS_TABLE[delta['object']] | ||
end | ||
|
||
obj = cls.new(api) | ||
|
||
case delta["event"] | ||
when 'create', 'modify' | ||
obj.inflate(delta['attributes']) | ||
obj.cursor = delta["cursor"] | ||
yield delta["event"], obj | ||
when 'delete' | ||
obj.id = delta["id"] | ||
obj.cursor = delta["cursor"] | ||
yield delta["event"], obj | ||
end | ||
end | ||
|
||
http = EventMachine::HttpRequest.new(path, :connect_timeout => 0, :inactivity_timeout => timeout).get(:keepalive => true) | ||
|
||
# set a callback on the HTTP stream that parses incoming chunks as they come in | ||
http.stream do |chunk| | ||
parser << chunk | ||
end | ||
|
||
http.errback do | ||
raise UnexpectedResponse.new http.error | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.