module Camdram::API
Attributes
Public Class Methods
Instantiate a new object
@param options [Hash] A single JSON hash with symbolized keys. @return [Object] The newly instantiated object.
# File lib/camdram/api.rb, line 12 def initialize(options = {}) super(options) end
Public Instance Methods
Update the object
@return [Object] The object the method is called on. @note The object this method is called on is updated 'in place'.
# File lib/camdram/api.rb, line 20 def update! json = get(self.url_slug) self.send(:initialize, json) return self end
Private Instance Methods
Send a HTTP
get request and parse the returned JSON
@param url_slug [String] The URL slug to send the HTTP
get request to. @return [Hash] A hash parsed from the JSON response with symbolized keys.
# File lib/camdram/api.rb, line 32 def get(url_slug) response = HTTP.instance.get(url_slug, 3) JSON.parse(response, symbolize_names: true) end
Return an array of objects of the given class
@param slug [String] The URL slug to send the HTTP
get request to. @param object [Object] The class to instantiate. @return [Array] An array of objects of the specified class.
# File lib/camdram/api.rb, line 42 def get_array(url_slug, object) json = get(url_slug) split_object(json, object) end
Split a JSON array into a Ruby array of object of the specified class
@param json [Array] The JSON array to itterate through. @param object [Object] The class to instantiate. @return [Array] An array of objects of the specified class.
# File lib/camdram/api.rb, line 52 def split_object(json, object) objects = Array.new json.each do |obj| objects << object.new( obj ) end return objects end