Class: Inbox::Namespace
Constant Summary
collapse
- OBJECTS_TABLE =
{
"account" => Inbox::Account,
"calendar" => Inbox::Calendar,
"draft" => Inbox::Draft,
"thread" => Inbox::Thread,
"contact" => Inbox::Contact,
"event" => Inbox::Event,
"file" => Inbox::File,
"message" => Inbox::Message,
"namespace" => Inbox::Namespace,
"tag" => Inbox::Tag,
"folder" => Inbox::Folder,
"label" => Inbox::Label,
}
Class Method Summary
collapse
Instance Method Summary
collapse
#==, #as_json, #destroy, #inflate, #initialize, #save!, #update, #url
#time_attr_accessor
Methods included from Parameters
included, #parameters
Class Method Details
.collection_name ⇒ Object
30
31
32
|
# File 'lib/namespace.rb', line 30
def self.collection_name
"n"
end
|
Instance Method Details
#_build_exclude_types(exclude_types) ⇒ Object
104
105
106
107
108
109
110
111
112
113
114
115
116
|
# File 'lib/namespace.rb', line 104
def _build_exclude_types(exclude_types)
exclude_string = "&exclude_types="
exclude_types.each do |value|
count = 0
if OBJECTS_TABLE.has_value?(value)
param_name = OBJECTS_TABLE.key(value)
exclude_string += "#{param_name},"
end
end
exclude_string = exclude_string[0..-2]
end
|
#delta_stream(cursor, exclude_types = [], timeout = 0) ⇒ Object
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
|
# File 'lib/namespace.rb', line 163
def delta_stream(cursor, exclude_types=[], timeout=0)
raise 'Please provide a block for receiving the delta objects' if !block_given?
exclude_string = ""
if exclude_types.any?
exclude_string = _build_exclude_types(exclude_types)
end
path = @_api.url_for_path("/n/#{@namespace_id}/delta/streaming?cursor=#{cursor}#{exclude_string}")
parser = Yajl::Parser.new(:symbolize_keys => false)
parser.on_parse_complete = proc do |data|
delta = Inbox.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']]
obj = cls.new(@_api, @namespace_id)
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
EventMachine.run do
http = EventMachine::HttpRequest.new(path, :connect_timeout => 0, :inactivity_timeout => timeout).get(:keepalive => true)
http.stream do |chunk|
parser << chunk
end
http.errback do
raise UnexpectedResponse.new http.error
end
end
end
|
#deltas(cursor, exclude_types = []) ⇒ Object
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
# File 'lib/namespace.rb', line 118
def deltas(cursor, exclude_types=[])
raise 'Please provide a block for receiving the delta objects' if !block_given?
exclude_string = ""
if exclude_types.any?
exclude_string = _build_exclude_types(exclude_types)
end
loop do
path = @_api.url_for_path("/n/#{@namespace_id}/delta?cursor=#{cursor}#{exclude_string}")
json = nil
RestClient.get(path) do |response,request,result|
json = Inbox.interpret_response(result, response, {:expected_class => Object})
end
start_cursor = json["cursor_start"]
end_cursor = json["cursor_end"]
json["deltas"].each do |delta|
if not OBJECTS_TABLE.has_key?(delta['object'])
next
end
cls = OBJECTS_TABLE[delta['object']]
obj = cls.new(@_api, @namespace_id)
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
break if start_cursor == end_cursor
cursor = end_cursor
end
end
|
#get_cursor(timestamp) ⇒ Object
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
# File 'lib/namespace.rb', line 74
def get_cursor(timestamp)
path = @_api.url_for_path("/n/#{@namespace_id}/delta/generate_cursor")
data = { :start => timestamp }
cursor = nil
RestClient.post(path, data.to_json, :content_type => :json) do |response,request,result|
json = Inbox.interpret_response(result, response, {:expected_class => Object})
cursor = json["cursor"]
end
cursor
end
|