Sha256: 8361cbbe67b7d7db7464b52f9d81b80931e58d18aa2f7940a4268f43bc059d71

Contents?: true

Size: 1.79 KB

Versions: 2

Compression:

Stored size: 1.79 KB

Contents

module StorageRoom
  # The superclass of all the classes that load data from the API
  class Base
    include Attributes
    include HTTParty
    
    headers 'User-Agent' => 'StorageRoom Ruby Gem', 'Accept' => 'application/json', 'Content-Type' => 'application/json'
    format :json

    
    class << self
      # Load an object with the specified URL from the API
      def load(url, parameters = {})
        httparty = get(url, parameters)
        
        handle_critical_response_errors(httparty)
        create_from_api(httparty.parsed_response.first[1])
      end
      
      # Handle known server errors
      def handle_critical_response_errors(httparty) # :nodoc:
        case httparty.response.code
          when '200', '201', '422' then true
          else
            raise StorageRoom::RequestFailed.new("Invalid HTTP Response: #{httparty.response.code}")
        end
      end
      
      # Creates a new object with a hash from the API with a @type attribute
      def create_from_api(hash)  # :nodoc:
        type = hash['@type']
        
        object = case type
          when 'Array' then Array.new
          when 'Collection' then Collection.new
          else
            StorageRoom.class_for_name(type).new
        end
        
        object.set_from_api(hash)
        object
      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_api(httparty.parsed_response.first[1])
      true
    end
       
    # Returns the remote URL of the object        
    def url
      self[:@url]
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
storage_room-0.1.2 lib/storage_room/base.rb
storage_room-0.1.1 lib/storage_room/base.rb