inbox 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: b3768cff4f9314c27d94cba1ab7cce94e481c8e2
4
- data.tar.gz: b79094f6a174bd27ba868c9c02520e46703a08f0
3
+ metadata.gz: 5086b79c08f4d42025a25eba333f19d44fe21280
4
+ data.tar.gz: 6dbee9a11674e8a9a3edd2dc9d70b75f82b39b17
5
5
  SHA512:
6
- metadata.gz: 4a8631bbd3c531ad0b0a92933de4d7ccd291187ffbe138b0074ce86f2002c02a2d4f7799f17724187984583cdbd284b6edad568d98c3af7695d28e380204944a
7
- data.tar.gz: cec71ec9c4861c3b311924c4808b1178f542a7be35f2cdc889de0b219ee0fb35fd05bfddb95e7a9b5d8f90bc82024e1eaaeff278d117a976bd86d3ac82ec0491
6
+ metadata.gz: 184cd8fb0c44ce22e996feb18aedbfab81ac9ab5d1c4833d1048bbe6ddd7debb03cd667576f0ecc1b3a4a7340c01d682337b07317bb89995096aaa07a96091af
7
+ data.tar.gz: 77acb364d13cedba5eb124e4fc0a401a5d8419948ee8c4de0e50e2061d643cc565f3375bcc1daa38438ffc7c2b230f9798f2c73814cab312d4b7bed8d6398a77
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: inbox
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