Skip to content

Commit

Permalink
Split out a nylas-streaming gem (#151)
Browse files Browse the repository at this point in the history
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
zspencer authored Nov 30, 2017
1 parent b33b833 commit b0fd658
Show file tree
Hide file tree
Showing 17 changed files with 211 additions and 134 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
source 'https://rubygems.org'

gemspec
gemspec name: 'nylas-streaming'
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Nylas REST API Ruby bindings ![Travis build status](https://travis-ci.org/nylas/nylas-ruby.svg?branch=master)

This README is for the Nylas Ruby SDK version 4. For those who are still using Nylas Ruby SDK Version 3, the documentation and source code is located in the [3.X-master branch](https://github.com/nylas/nylas-ruby/tree/3.X-master)
This README is for the Nylas Ruby SDK version 4. For those who are still using Nylas Ruby SDK Version 3, the documentation and source code is located in the [3.X-master branch](https://github.com/nylas/nylas-ruby/tree/3.X-master). For those upgrading from 3.X to 4.0, review the [upgrade guide](https://github.com/nylas/nylas-ruby/wiki/Upgrading-from-3.X-to-4.0)

## Installation

Expand Down
2 changes: 1 addition & 1 deletion gemfiles/Gemfile.rails4
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ source 'https://rubygems.org'

gem 'rails', '~> 4.2'

gemspec path: ".."
gemspec path: '..', name: 'nylas-streaming'
8 changes: 5 additions & 3 deletions gemfiles/Gemfile.rails4.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ PATH
specs:
nylas (4.0.0.rc1)
rest-client (>= 1.6)
nylas-streaming (4.0.0.rc1)
em-http-request (~> 1.1, >= 1.1.3)
nylas (>= 4.0)
yajl-ruby (~> 1.2, >= 1.2.1)

GEM
remote: https://rubygems.org/
Expand Down Expand Up @@ -220,9 +224,8 @@ PLATFORMS

DEPENDENCIES
bundler (>= 1.3.5)
em-http-request (~> 1.1, >= 1.1.3)
jeweler (>= 2.1.2)
nylas!
nylas-streaming!
pry (>= 0.10.4)
pry-nav (>= 0.2.4)
pry-stack_explorer (>= 0.4.9.2)
Expand All @@ -231,7 +234,6 @@ DEPENDENCIES
shoulda (>= 3.4.0)
sinatra (>= 1.4.7)
webmock (>= 2.1.0)
yajl-ruby (~> 1.2, >= 1.2.1)
yard (>= 0.9.12)

BUNDLED WITH
Expand Down
2 changes: 1 addition & 1 deletion gemfiles/Gemfile.rails5
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ source 'https://rubygems.org'

gem 'rails', '~> 5'

gemspec path: ".."
gemspec path: "..", name: 'nylas-streaming'
8 changes: 5 additions & 3 deletions gemfiles/Gemfile.rails5.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ PATH
specs:
nylas (4.0.0.rc1)
rest-client (>= 1.6)
nylas-streaming (4.0.0.rc1)
em-http-request (~> 1.1, >= 1.1.3)
nylas (>= 4.0)
yajl-ruby (~> 1.2, >= 1.2.1)

GEM
remote: https://rubygems.org/
Expand Down Expand Up @@ -228,9 +232,8 @@ PLATFORMS

DEPENDENCIES
bundler (>= 1.3.5)
em-http-request (~> 1.1, >= 1.1.3)
jeweler (>= 2.1.2)
nylas!
nylas-streaming!
pry (>= 0.10.4)
pry-nav (>= 0.2.4)
pry-stack_explorer (>= 0.4.9.2)
Expand All @@ -239,7 +242,6 @@ DEPENDENCIES
shoulda (>= 3.4.0)
sinatra (>= 1.4.7)
webmock (>= 2.1.0)
yajl-ruby (~> 1.2, >= 1.2.1)
yard (>= 0.9.12)

BUNDLED WITH
Expand Down
2 changes: 1 addition & 1 deletion gemfiles/Gemfile.rest-client.1
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ source 'https://rubygems.org'

gem 'rest-client', '~> 1.6'

gemspec path: ".."
gemspec path: "..", name: 'nylas-streaming'
8 changes: 5 additions & 3 deletions gemfiles/Gemfile.rest-client.1.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ PATH
specs:
nylas (4.0.0.rc1)
rest-client (>= 1.6)
nylas-streaming (4.0.0.rc1)
em-http-request (~> 1.1, >= 1.1.3)
nylas (>= 4.0)
yajl-ruby (~> 1.2, >= 1.2.1)

GEM
remote: https://rubygems.org/
Expand Down Expand Up @@ -148,9 +152,8 @@ PLATFORMS

DEPENDENCIES
bundler (>= 1.3.5)
em-http-request (~> 1.1, >= 1.1.3)
jeweler (>= 2.1.2)
nylas!
nylas-streaming!
pry (>= 0.10.4)
pry-nav (>= 0.2.4)
pry-stack_explorer (>= 0.4.9.2)
Expand All @@ -159,7 +162,6 @@ DEPENDENCIES
shoulda (>= 3.4.0)
sinatra (>= 1.4.7)
webmock (>= 2.1.0)
yajl-ruby (~> 1.2, >= 1.2.1)
yard (>= 0.9.12)

BUNDLED WITH
Expand Down
2 changes: 1 addition & 1 deletion gemfiles/Gemfile.rest-client.2
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ source 'https://rubygems.org'

gem 'rest-client', '~> 2.0'

gemspec path: ".."
gemspec path: "..", name: 'nylas-streaming'
8 changes: 5 additions & 3 deletions gemfiles/Gemfile.rest-client.2.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ PATH
specs:
nylas (4.0.0.rc1)
rest-client (>= 1.6)
nylas-streaming (4.0.0.rc1)
em-http-request (~> 1.1, >= 1.1.3)
nylas (>= 4.0)
yajl-ruby (~> 1.2, >= 1.2.1)

GEM
remote: https://rubygems.org/
Expand Down Expand Up @@ -148,9 +152,8 @@ PLATFORMS

DEPENDENCIES
bundler (>= 1.3.5)
em-http-request (~> 1.1, >= 1.1.3)
jeweler (>= 2.1.2)
nylas!
nylas-streaming!
pry (>= 0.10.4)
pry-nav (>= 0.2.4)
pry-stack_explorer (>= 0.4.9.2)
Expand All @@ -159,7 +162,6 @@ DEPENDENCIES
shoulda (>= 3.4.0)
sinatra (>= 1.4.7)
webmock (>= 2.1.0)
yajl-ruby (~> 1.2, >= 1.2.1)
yard (>= 0.9.12)

BUNDLED WITH
Expand Down
81 changes: 81 additions & 0 deletions lib/nylas-streaming.rb
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

64 changes: 28 additions & 36 deletions lib/nylas.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
require 'ostruct'
require_relative 'nylas/to_query'

require_relative 'nylas/types_filter'

require 'nylas/account'
require 'nylas/api_account'
require 'nylas/thread'
Expand All @@ -23,6 +25,25 @@
require 'nylas/version'

module Nylas
OBJECTS_TABLE = {
"account" => Nylas::Account,
"calendar" => Nylas::Calendar,
"draft" => Nylas::Draft,
"thread" => Nylas::Thread,
"contact" => Nylas::Contact,
"event" => Nylas::Event,
"file" => Nylas::File,
"message" => Nylas::Message,
"folder" => Nylas::Folder,
"label" => Nylas::Label,
}

# It's possible to ask the API to expand objects.
# In this case, we do the right thing and return
# an expanded object.
EXPANDED_OBJECTS_TABLE = {
"message" => Nylas::ExpandedMessage,
}
Error = Class.new(::StandardError)
NoAuthToken = Class.new(Error)
UnexpectedAccountAction = Class.new(Error)
Expand Down Expand Up @@ -265,46 +286,13 @@ def latest_cursor
cursor
end

OBJECTS_TABLE = {
"account" => Nylas::Account,
"calendar" => Nylas::Calendar,
"draft" => Nylas::Draft,
"thread" => Nylas::Thread,
"contact" => Nylas::Contact,
"event" => Nylas::Event,
"file" => Nylas::File,
"message" => Nylas::Message,
"folder" => Nylas::Folder,
"label" => Nylas::Label,
}

# It's possible to ask the API to expand objects.
# In this case, we do the right thing and return
# an expanded object.
EXPANDED_OBJECTS_TABLE = {
"message" => Nylas::ExpandedMessage,
}

def _build_types_filter_string(filter, types)
return "" if types.empty?
query_string = "&#{filter}_types="

types.each do |value|
count = 0
if OBJECTS_TABLE.has_value?(value)
param_name = OBJECTS_TABLE.key(value)
query_string += "#{param_name},"
end
end

query_string = query_string[0..-2]
end

def deltas(cursor, exclude_types=[], expanded_view=false, include_types=[])
return enum_for(:deltas, cursor, exclude_types, expanded_view, include_types) unless block_given?

exclude_string = _build_types_filter_string(:exclude, exclude_types)
include_string = _build_types_filter_string(:include, include_types)
exclude_string = TypesFilter.new(:exclude, types: exclude_types).to_query_string
include_string = TypesFilter.new(:include, types: include_types).to_query_string

# loop and yield deltas until we've come to the end.
loop do
Expand Down Expand Up @@ -351,6 +339,10 @@ def deltas(cursor, exclude_types=[], expanded_view=false, include_types=[])
end
end

require 'nylas/api/delta_stream'
def delta_stream(cursor, exclude_types=[], timeout=0, expanded_view=false, include_types=[], &block)
raise NotImplementedError, "the `#delta_stream` method was removed in 4.0 in favor of using the " \
"`nylas-streming` gem. This reduces the dependency footprint of the core " \
"nylas gem for those not using the streaming API."
end
end
end
Loading

0 comments on commit b0fd658

Please sign in to comment.