module Camdram::API

Attributes

http[RW]

Public Class Methods

new(options = {}) click to toggle source

Instantiate a new object

@param options [Hash] A single JSON hash with symbolized keys. @return [Object] The newly instantiated object.

Calls superclass method
# File lib/camdram/api.rb, line 12
def initialize(options = {})
  super(options)
end

Public Instance Methods

update!() click to toggle source

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

get(url_slug) click to toggle source

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
get_array(url_slug, object) click to toggle source

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_object(json, object) click to toggle source

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