Sha256: 04bf24b00009c7cb4450c27659366673b7c796befd8f825734d09a8ef830cfda

Contents?: true

Size: 1.25 KB

Versions: 5

Compression:

Stored size: 1.25 KB

Contents

module StorageRoom
  # The superclass of all the classes that load data from the API
  class Resource
    include HTTParty
    extend Plugins
    
    include Accessors
    
    plugin IdentityMap
    
    headers 'User-Agent' => "StorageRoom Ruby Gem (#{StorageRoom.version})", 'Accept' => 'application/json', 'Content-Type' => 'application/json'
    format :json

    class << self            
      # Handle known server errors
      def handle_critical_response_errors(httparty) # :nodoc:
        case httparty.response.code
          when '200', '201', '409', '422' then true
          else
            raise StorageRoom::RequestFailedError.new("Invalid HTTP Response: #{httparty.response.code}")
        end
      end
      
      # Find out if a key is user-defined or meta-data (begins with @)      
      def meta_data?(key) 
        key[0...1] == '@'
      end            
    end
    
    # Reload an object from the API. Optionally pass an URL.
    def reload(url = nil, parameters = {})
      httparty = self.class.get(url || self[:@url], parameters)
      set_from_response_data(httparty.parsed_response.first[1])
      true
    end
    
    # Has the Resource been loaded from the API?
    def loaded?
      self['@url'] ? true : false
    end
       

  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
storage_room-0.3.9 lib/storage_room/resource.rb
storage_room-0.3.8 lib/storage_room/resource.rb
storage_room-0.3.7 lib/storage_room/resource.rb
storage_room-0.3.6 lib/storage_room/resource.rb
storage_room-0.3.5 lib/storage_room/resource.rb