nylas 0.16.1 → 0.17.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 999719960074186f77ac3eef0bf60cef352dd7b5
4
- data.tar.gz: 8f77bcb3c45108e06dae0fd9be0d91c378e5b63b
3
+ metadata.gz: 48ec6f70dc6e37ea3a4d59aa91259f7930e6d4a2
4
+ data.tar.gz: ffbf49839a6076ad36cc7723b8bcf3ba0b246596
5
5
  SHA512:
6
- metadata.gz: f8d3944cf80027d4bb226e0863651156f665fb9aa18397a3f2d3b83eb88c1efb617b10dd1f41efdba732c39de7696eef38315c7e2346af1ea7d3d7c76519a419
7
- data.tar.gz: cb8ea4a706dfa3f5920d898b94ee2550ecae7f61dc11b87e184719bc53a01770a1f19a4a8586e883272b873c328ac266554fb439627368e6fbe908953520fcde
6
+ metadata.gz: abf3f677bea9bf62f9e062348a26948e40abf8fb09c20eb44fa001907346bab3ecfc2a50da0be59201ccf11a10b8af30906ee508ea2e666cfa932831a872dd40
7
+ data.tar.gz: 20875e13b7c38eec6aac73cfd543a434f945bb1a8195849b2967beafc2b68dfb25454272705f17e8d3ba7f591af03b09b158623773cfa12641e9b6eda72b2dd8
data/README.md CHANGED
@@ -19,7 +19,7 @@ You don't need to use this repo unless you're planning to modify the gem. If you
19
19
 
20
20
  - Ruby 1.8.7 or above. (Ruby 1.8.6 may work if you load ActiveSupport.)
21
21
 
22
- - rest-client, json
22
+ - rest-client, json, yajl-ruby, em-http-request
23
23
 
24
24
 
25
25
  ## Example Rails App
@@ -278,6 +278,39 @@ end
278
278
  save_to_db(last_cursor)
279
279
  ```
280
280
 
281
+ ### Using the Delta sync streaming API
282
+
283
+ The streaming API will receive deltas in real time, without needing to repeatedly poll. It uses EventMachine for async IO.
284
+
285
+ ````ruby
286
+ # Get all the messages starting from timestamp
287
+ #
288
+ # we first need to get a cursor object a cursor is simply the id of
289
+ # an individual change.
290
+ cursor = inbox.namespaces.first.get_cursor(1407543195)
291
+
292
+ last_cursor = nil
293
+ inbox.namespaces.first.delta_stream(cursor) do |event, object|
294
+ if event == "create" or event == "modify"
295
+ if object.is_a?(Inbox::Contact)
296
+ puts "#{object.name} - #{object.email}"
297
+ elsif object.is_a?(Inbox::Event)
298
+ puts "Event!"
299
+ end
300
+ elsif event == "delete"
301
+ # In the case of a deletion, the API only returns the ID of the object.
302
+ # In this case, the Ruby SDK returns a dummy object with only the id field
303
+ # set.
304
+ puts "Deleting from collection #{object.class.name}, id: #{object}"
305
+ end
306
+ last_cursor = object.cursor
307
+
308
+ # This will loop indefintely
309
+ end
310
+
311
+ ```
312
+
313
+
281
314
  ### Exclude changes from a specific type --- get only messages
282
315
  ````ruby
283
316
  nylas.namespaces.first.deltas(cursor, exclude=[Nylas::Contact,
data/lib/inbox.rb CHANGED
@@ -36,7 +36,7 @@ module Inbox
36
36
 
37
37
  # Handle content expectation errors
38
38
  raise UnexpectedResponse.new if options[:expected_class] && result_content.empty?
39
- json = JSON.parse(result_content)
39
+ json = options[:result_parsed]? result_content : JSON.parse(result_content)
40
40
  if json.is_a?(Hash) && (json['type'] == 'api_error' or json['type'] == 'invalid_request_error')
41
41
  if result.code.to_i == 400
42
42
  exc = InvalidRequest
data/lib/namespace.rb CHANGED
@@ -7,6 +7,9 @@ require 'contact'
7
7
  require 'file'
8
8
  require 'calendar'
9
9
  require 'event'
10
+ require 'yajl'
11
+ require 'em-http'
12
+ require 'ostruct'
10
13
 
11
14
  # Rather than saying require 'thread', we need to explicitly force
12
15
  # the thread model to load. Otherwise, we can't reference it below.
@@ -87,24 +90,28 @@ module Inbox
87
90
  "tag" => Inbox::Tag,
88
91
  }
89
92
 
93
+ def _build_exclude_types(exclude_types)
94
+ exclude_string = "&exclude_types="
95
+
96
+ exclude_types.each do |value|
97
+ count = 0
98
+ if OBJECTS_TABLE.has_value?(value)
99
+ param_name = OBJECTS_TABLE.key(value)
100
+ exclude_string += "#{param_name},"
101
+ end
102
+ end
103
+
104
+ exclude_string = exclude_string[0..-2]
105
+ end
106
+
90
107
  def deltas(cursor, exclude_types=[])
91
108
  raise 'Please provide a block for receiving the delta objects' if !block_given?
92
109
  exclude_string = ""
93
110
 
94
111
  if exclude_types.any?
95
- exclude_string = "&exclude_types="
96
-
97
- exclude_types.each do |value|
98
- count = 0
99
- if OBJECTS_TABLE.has_value?(value)
100
- param_name = OBJECTS_TABLE.key(value)
101
- exclude_string += "#{param_name},"
102
- end
103
- end
112
+ exclude_string = _build_exclude_types(exclude_types)
104
113
  end
105
114
 
106
- exclude_string = exclude_string[0..-2]
107
-
108
115
  # loop and yield deltas until we've come to the end.
109
116
  loop do
110
117
  path = @_api.url_for_path("/n/#{@namespace_id}/delta?cursor=#{cursor}#{exclude_string}")
@@ -143,5 +150,47 @@ module Inbox
143
150
  end
144
151
  end
145
152
 
153
+ def delta_stream(cursor, exclude_types=[], timeout=0)
154
+ raise 'Please provide a block for receiving the delta objects' if !block_given?
155
+
156
+ exclude_string = ""
157
+
158
+ if exclude_types.any?
159
+ exclude_string = _build_exclude_types(exclude_types)
160
+ end
161
+
162
+ # loop and yield deltas indefinitely.
163
+ path = @_api.url_for_path("/n/#{@namespace_id}/delta/streaming?cursor=#{cursor}#{exclude_string}")
164
+
165
+ parser = Yajl::Parser.new(:symbolize_keys => false)
166
+ parser.on_parse_complete = proc do |data|
167
+ delta = Inbox.interpret_response(OpenStruct.new(:code => '200'), data, {:expected_class => Object, :result_parsed => true})
168
+
169
+ cls = OBJECTS_TABLE[delta['object']]
170
+ obj = cls.new(@_api, @namespace_id)
171
+
172
+ case delta["event"]
173
+ when 'create', 'modify'
174
+ obj.inflate(delta['attributes'])
175
+ obj.cursor = delta["cursor"]
176
+ yield delta["event"], obj
177
+ when 'delete'
178
+ obj.id = delta["id"]
179
+ obj.cursor = delta["cursor"]
180
+ yield delta["event"], obj
181
+ end
182
+ end
183
+
184
+ EventMachine.run do
185
+ http = EventMachine::HttpRequest.new(path, :connect_timeout => 0, :inactivity_timeout => timeout).get(:keepalive => true)
186
+ http.stream do |chunk|
187
+ parser << chunk
188
+ end
189
+ http.errback do
190
+ raise UnexpectedResponse.new http.error
191
+ end
192
+ end
193
+ end
194
+
146
195
  end
147
196
  end
data/lib/nylas.rb CHANGED
@@ -36,7 +36,7 @@ module Inbox
36
36
 
37
37
  # Handle content expectation errors
38
38
  raise UnexpectedResponse.new if options[:expected_class] && result_content.empty?
39
- json = JSON.parse(result_content)
39
+ json = options[:result_parsed]? result_content : JSON.parse(result_content)
40
40
  if json.is_a?(Hash) && (json['type'] == 'api_error' or json['type'] == 'invalid_request_error')
41
41
  if result.code.to_i == 400
42
42
  exc = InvalidRequest
data/lib/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Inbox
2
- VERSION = "0.16.1"
2
+ VERSION = "0.17.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nylas
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.16.1
4
+ version: 0.17.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Gotow
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2015-06-15 00:00:00.000000000 Z
13
+ date: 2015-06-25 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rest-client
@@ -26,6 +26,34 @@ dependencies:
26
26
  - - ~>
27
27
  - !ruby/object:Gem::Version
28
28
  version: '1.7'
29
+ - !ruby/object:Gem::Dependency
30
+ name: yajl-ruby
31
+ requirement: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - '>='
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ type: :runtime
37
+ prerelease: false
38
+ version_requirements: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ - !ruby/object:Gem::Dependency
44
+ name: em-http-request
45
+ requirement: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - '>='
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ type: :runtime
51
+ prerelease: false
52
+ version_requirements: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - '>='
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
29
57
  - !ruby/object:Gem::Dependency
30
58
  name: rspec
31
59
  requirement: !ruby/object:Gem::Requirement