Class: Uktt::Commodity

Inherits:
Object
  • Object
show all
Defined in:
lib/uktt/commodity.rb

Overview

A Commodity object for dealing with an API resource

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Commodity

Returns a new instance of Commodity.



6
7
8
9
10
11
# 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

Instance Attribute Details

#commodity_idObject

Returns the value of attribute commodity_id.



4
5
6
# File 'lib/uktt/commodity.rb', line 4

def commodity_id
  @commodity_id
end

#configObject

Returns the value of attribute config.



4
5
6
# File 'lib/uktt/commodity.rb', line 4

def config
  @config
end

#responseObject

Returns the value of attribute response.



4
5
6
# File 'lib/uktt/commodity.rb', line 4

def response
  @response
end

Instance Method Details

#changesObject



19
20
21
22
23
# 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

#find(id) ⇒ Object



32
33
34
35
36
37
38
39
# 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) ⇒ Object



41
42
43
44
45
46
47
48
# 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

#retrieveObject



13
14
15
16
17
# 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