Class: Inbox::API
- Inherits:
-
Object
- Object
- Inbox::API
- Defined in:
- lib/inbox.rb,
lib/nylas.rb
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, "tag" => Inbox::Tag, "folder" => Inbox::Folder, "label" => Inbox::Label, }
- EXPANDED_OBJECTS_TABLE =
It’s possible to ask the API to expand objects. In this case, we do the right thing and return an expanded object.
{ "message" => Inbox::ExpandedMessage, }
Instance Attribute Summary collapse
-
#access_token ⇒ Object
readonly
Returns the value of attribute access_token.
-
#api_server ⇒ Object
Returns the value of attribute api_server.
-
#app_id ⇒ Object
readonly
Returns the value of attribute app_id.
-
#app_secret ⇒ Object
readonly
Returns the value of attribute app_secret.
Instance Method Summary collapse
- #_build_exclude_types(exclude_types) ⇒ Object
- #account ⇒ Object
- #accounts ⇒ Object
- #calendars ⇒ Object
- #contacts ⇒ Object
- #delta_stream(cursor, exclude_types = [], timeout = 0, expanded_view = false) ⇒ Object
- #deltas(cursor, exclude_types = [], expanded_view = false) ⇒ Object
- #drafts ⇒ Object
- #events ⇒ Object
- #files ⇒ Object
- #folders ⇒ Object
- #get_cursor(timestamp) ⇒ Object
-
#initialize(app_id, app_secret, access_token = nil, api_server = 'https://api.nylas.com', service_domain = 'api.nylas.com') ⇒ API
constructor
A new instance of API.
- #labels ⇒ Object
- #latest_cursor ⇒ Object
- #messages ⇒ Object
- #set_access_token(token) ⇒ Object
- #tags ⇒ Object
-
#threads ⇒ Object
API Methods.
- #token_for_code(code) ⇒ Object
- #url_for_authentication(redirect_uri, login_hint = '', options = {}) ⇒ Object
- #url_for_management ⇒ Object
- #url_for_path(path) ⇒ Object
- #using_hosted_api? ⇒ Boolean
Constructor Details
#initialize(app_id, app_secret, access_token = nil, api_server = 'https://api.nylas.com', service_domain = 'api.nylas.com') ⇒ API
Returns a new instance of API.
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/inbox.rb', line 92 def initialize(app_id, app_secret, access_token = nil, api_server = 'https://api.nylas.com', service_domain = 'api.nylas.com') raise "When overriding the Inbox API server address, you must include https://" unless api_server.include?('://') @api_server = api_server @access_token = access_token @app_secret = app_secret @app_id = app_id @service_domain = service_domain @version = Inbox::VERSION if ::RestClient.before_execution_procs.empty? ::RestClient.add_before_execution_proc do |req, params| req.add_field('X-Inbox-API-Wrapper', 'ruby') req['User-Agent'] = "Nylas Ruby SDK #{@version} - #{RUBY_VERSION}" end end end |
Instance Attribute Details
#access_token ⇒ Object (readonly)
Returns the value of attribute access_token.
88 89 90 |
# File 'lib/inbox.rb', line 88 def access_token @access_token end |
#api_server ⇒ Object
Returns the value of attribute api_server.
87 88 89 |
# File 'lib/inbox.rb', line 87 def api_server @api_server end |
#app_id ⇒ Object (readonly)
Returns the value of attribute app_id.
89 90 91 |
# File 'lib/inbox.rb', line 89 def app_id @app_id end |
#app_secret ⇒ Object (readonly)
Returns the value of attribute app_secret.
90 91 92 |
# File 'lib/inbox.rb', line 90 def app_secret @app_secret end |
Instance Method Details
#_build_exclude_types(exclude_types) ⇒ Object
263 264 265 266 267 268 269 270 271 272 273 274 275 |
# File 'lib/inbox.rb', line 263 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 |
#account ⇒ Object
188 189 190 191 192 193 194 195 196 197 |
# File 'lib/inbox.rb', line 188 def account path = self.url_for_path("/account") RestClient.get(path, {}) do |response,request,result| json = Inbox.interpret_response(result, response, {:expected_class => Object}) model = APIAccount.new(self) model.inflate(json) model end end |
#accounts ⇒ Object
203 204 205 206 207 208 209 |
# File 'lib/inbox.rb', line 203 def accounts if self.using_hosted_api? @accounts ||= ManagementModelCollection.new(Account, self) else @accounts ||= RestfulModelCollection.new(APIAccount, self) end end |
#calendars ⇒ Object
172 173 174 |
# File 'lib/inbox.rb', line 172 def calendars @calendars ||= RestfulModelCollection.new(Calendar, self) end |
#contacts ⇒ Object
168 169 170 |
# File 'lib/inbox.rb', line 168 def contacts @contacts ||= RestfulModelCollection.new(Contact, self) end |
#delta_stream(cursor, exclude_types = [], timeout = 0, expanded_view = false) ⇒ Object
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 |
# File 'lib/inbox.rb', line 331 def delta_stream(cursor, exclude_types=[], timeout=0, =false) 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 and yield deltas indefinitely. path = self.url_for_path("/delta/streaming?exclude_folders=false&cursor=#{cursor}#{exclude_string}") if path += '&view=expanded' end 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']] if EXPANDED_OBJECTS_TABLE.has_key?(delta['object']) and cls = EXPANDED_OBJECTS_TABLE[delta['object']] end obj = cls.new(self) 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 = [], expanded_view = false) ⇒ Object
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 |
# File 'lib/inbox.rb', line 277 def deltas(cursor, exclude_types=[], =false) return enum_for(:deltas, cursor, exclude_types, ) unless block_given? exclude_string = "" if exclude_types.any? exclude_string = _build_exclude_types(exclude_types) end # loop and yield deltas until we've come to the end. loop do path = self.url_for_path("/delta?exclude_folders=false&cursor=#{cursor}#{exclude_string}") if path += '&view=expanded' end 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']] if EXPANDED_OBJECTS_TABLE.has_key?(delta['object']) and cls = EXPANDED_OBJECTS_TABLE[delta['object']] end obj = cls.new(self) 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 |
#drafts ⇒ Object
164 165 166 |
# File 'lib/inbox.rb', line 164 def drafts @drafts ||= RestfulModelCollection.new(Draft, self) end |
#events ⇒ Object
176 177 178 |
# File 'lib/inbox.rb', line 176 def events @events ||= RestfulModelCollection.new(Event, self) end |
#files ⇒ Object
160 161 162 |
# File 'lib/inbox.rb', line 160 def files @files ||= RestfulModelCollection.new(File, self) end |
#folders ⇒ Object
180 181 182 |
# File 'lib/inbox.rb', line 180 def folders @folders ||= RestfulModelCollection.new(Folder, self) end |
#get_cursor(timestamp) ⇒ Object
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/inbox.rb', line 211 def get_cursor() # Get the cursor corresponding to a specific timestamp. warn "Nylas#get_cursor is deprecated. Use Nylas#latest_cursor instead." path = self.url_for_path("/delta/generate_cursor") data = { :start => } 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 |
#labels ⇒ Object
184 185 186 |
# File 'lib/inbox.rb', line 184 def labels @labels ||= RestfulModelCollection.new(Label, self) end |
#latest_cursor ⇒ Object
228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/inbox.rb', line 228 def latest_cursor # Get the cursor corresponding to a specific timestamp. path = self.url_for_path("/delta/latest_cursor") cursor = nil RestClient.post(path, :content_type => :json) do |response,request,result| json = Inbox.interpret_response(result, response, {:expected_class => Object}) cursor = json["cursor"] end cursor end |
#messages ⇒ Object
156 157 158 |
# File 'lib/inbox.rb', line 156 def @messages ||= RestfulModelCollection.new(Message, self) end |
#set_access_token(token) ⇒ Object
129 130 131 |
# File 'lib/inbox.rb', line 129 def set_access_token(token) @access_token = token end |
#tags ⇒ Object
152 153 154 |
# File 'lib/inbox.rb', line 152 def @tags ||= RestfulModelCollection.new(Tag, self) end |
#threads ⇒ Object
API Methods
148 149 150 |
# File 'lib/inbox.rb', line 148 def threads @threads ||= RestfulModelCollection.new(Thread, self) end |
#token_for_code(code) ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/inbox.rb', line 133 def token_for_code(code) data = { 'client_id' => app_id, 'client_secret' => app_secret, 'grant_type' => 'authorization_code', 'code' => code } ::RestClient.post("https://#{@service_domain}/oauth/token", data) do |response, request, result| json = Inbox.interpret_response(result, response, :expected_class => Object) return json['access_token'] end end |
#url_for_authentication(redirect_uri, login_hint = '', options = {}) ⇒ Object
116 117 118 119 120 121 122 |
# File 'lib/inbox.rb', line 116 def url_for_authentication(redirect_uri, login_hint = '', = {}) trialString = 'false' if [:trial] == true trialString = 'true' end "https://#{@service_domain}/oauth/authorize?client_id=#{@app_id}&trial=#{trialString}&response_type=code&scope=email&login_hint=#{login_hint}&redirect_uri=#{redirect_uri}" end |
#url_for_management ⇒ Object
124 125 126 127 |
# File 'lib/inbox.rb', line 124 def url_for_management protocol, domain = @api_server.split('//') accounts_path = "#{protocol}//#{@app_secret}:@#{domain}/a/#{@app_id}/accounts" end |
#url_for_path(path) ⇒ Object
110 111 112 113 114 |
# File 'lib/inbox.rb', line 110 def url_for_path(path) raise NoAuthToken.new if @access_token == nil and (@app_secret != nil or @app_id != nil) protocol, domain = @api_server.split('//') "#{protocol}//#{@access_token}:@#{domain}#{path}" end |
#using_hosted_api? ⇒ Boolean
199 200 201 |
# File 'lib/inbox.rb', line 199 def using_hosted_api? return !@app_id.nil? end |