class Uktt::Commodity

A Commodity object for dealing with an API resource

Attributes

commodity_id[RW]
config[RW]
response[RW]

Public Class Methods

new(opts = {}) click to toggle source
# File lib/uktt/commodity.rb, line 6
def initialize(opts = {})
  @commodity_id = opts[:commodity_id] || nil
  Uktt.configure(opts)
  @config = Uktt.config
  @response = nil
end

Public Instance Methods

changes() click to toggle source
# File lib/uktt/commodity.rb, line 19
def changes
  return '@commodity_id cannot be nil' if @commodity_id.nil?

  fetch "#{COMMODITY}/#{@commodity_id}/changes.json"
end
config=(new_opts = {}) click to toggle source
# File lib/uktt/commodity.rb, line 25
def config=(new_opts = {})
  merged_opts = Uktt.config.merge(new_opts)
  Uktt.configure merged_opts
  @commodity_id = merged_opts[:commodity_id] || @commodity_id
  @config = Uktt.config
end
find(id) click to toggle source
# File lib/uktt/commodity.rb, line 32
def find(id)
  return '@response is nil, run #retrieve first' unless @response
  
  response = @response.included.select do |obj|
    obj.id === id || obj.type === id
  end
  response.length == 1 ? response.first : response
end
find_in(arr) click to toggle source
# File lib/uktt/commodity.rb, line 41
def find_in(arr)
  return '@response is nil, run #retrieve first' unless @response
  
  response = @response.included.select do |obj|
    arr.include?(obj.id)
  end
  response.length == 1 ? response.first : response
end
retrieve() click to toggle source
# File lib/uktt/commodity.rb, line 13
def retrieve
  return '@commodity_id cannot be nil' if @commodity_id.nil?

  fetch "#{COMMODITY}/#{@commodity_id}.json"
end

Private Instance Methods

fetch(resource) click to toggle source
# File lib/uktt/commodity.rb, line 52
def fetch(resource)
  @response = Uktt::Http.new(
    @config[:host], 
    @config[:version], 
    @config[:debug])
  .retrieve(resource, 
    @config[:return_json])
end