Class: StorageRoom::Resource

Inherits:
Object
  • Object
show all
Extended by:
Plugins
Includes:
HTTParty, Accessors
Defined in:
lib/storage_room/resource.rb

Overview

The superclass of all the classes that load data from the API

Direct Known Subclasses

Array, Model, WebhookCall

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from Plugins

included, plugin, plugins

Methods included from Accessors

#[], #as_json, #attributes, #attributes=, #eql?, #hash, #initialize, #inspect, #proxy?, #reset!, #response_data, #response_data=, #set_from_response_data, #to_hash

Class Method Details

+ (Object) handle_critical_response_errors(httparty)

Handle known server errors



16
17
18
19
20
21
22
# File 'lib/storage_room/resource.rb', line 16

def handle_critical_response_errors(httparty) # :nodoc:
  case httparty.response.code
    when '200', '201', '204', '409', '422' then true
    else
      raise StorageRoom::RequestFailedError.new("Invalid HTTP Response: #{httparty.response.code}")
  end
end

+ (Boolean) meta_data?(key)

Find out if a key is user-defined or meta-data (begins with @)

Returns:

  • (Boolean)


25
26
27
# File 'lib/storage_room/resource.rb', line 25

def meta_data?(key) 
  key[0...1] == '@'
end

Instance Method Details

- (Boolean) loaded?

Has the Resource been loaded from the API?

Returns:

  • (Boolean)


40
41
42
# File 'lib/storage_room/resource.rb', line 40

def loaded?
  self['@url'] ? true : false
end

- (Object) reload(url = nil, parameters = {})

Reload an object from the API. Optionally pass an URL.



31
32
33
34
35
36
37
# File 'lib/storage_room/resource.rb', line 31

def reload(url = nil, parameters = {})
  httparty = self.class.get(url || self[:@url], StorageRoom.request_options.merge(parameters))
  hash = httparty.parsed_response.first[1]
  reset!
  set_from_response_data(hash)
  true
end