Class: BackpackTF::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/backpack_tf/client.rb

Constant Summary collapse

@@env_var =

store your API key as an environment variable ‘export <env_var>=’<your api key>‘`

'BPTF_API_KEY'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



74
75
76
# File 'lib/backpack_tf/client.rb', line 74

def initialize
  @db = nil 
end

Instance Attribute Details

#dbObject (readonly)

Instance Methods



72
73
74
# File 'lib/backpack_tf/client.rb', line 72

def db
  @db
end

Class Method Details

.api_key(key = nil) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/backpack_tf/client.rb', line 16

def self.api_key key = nil
  default = key || ENV[@@env_var]
  
  if default.nil?
    msg = "Assign your API key to an environment variable.\n"
    msg << "ex: `export #{@@env_var}=value`"
    raise KeyError, msg
  elsif default.class == String && (default.length != 24 || !!default[/\H/])
    msg = "The key should be a hexadecimal number, 24-digits long"
    raise ArgumentError, msg
  else
    default
  end
end

.build_url_via(action, query_options = {}) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/backpack_tf/client.rb', line 35

def self.build_url_via action, query_options = {}
  case action
  when :get_prices, :prices, :price
    version = 4
    interface_url = "/#{Price.interface}/v#{version}/?"
  when :get_currencies, :currencies, :currency
    version = 1
    interface_url = "/#{Currency.interface}/v#{version}/?"
  when :get_special_items, :special_items, :special_item, :specialitem
    version = 1
    interface_url = "/#{SpecialItem.interface}/v#{version}/?"
  when :get_users, :users, :user
    version = 3
    interface_url = "/#{User.interface}/v#{version}/?"
  when :get_user_listings, :user_listings, :user_listing, :userlisting
    version = 1
    interface_url = "/#{UserListing.interface}/v#{version}/?"
  else
    raise ArgumentError, 'pass in valid action as a Symbol object'
  end
  
  base_uri + interface_url + extract_query_string(query_options)
end

.env_varObject



14
# File 'lib/backpack_tf/client.rb', line 14

def self.env_var; @@env_var; end

.extract_query_string(options = {}) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/backpack_tf/client.rb', line 59

def self.extract_query_string options = {}
  options.each_pair.map do |key, val|
    unless val.class == Array
      "#{key}=#{val}"
    else
      "#{key}=#{val.join(',')}"
    end
  end.join('&')
end

Instance Method Details

#fetch(interface, query_options = {}) ⇒ Object



78
79
80
# File 'lib/backpack_tf/client.rb', line 78

def fetch interface, query_options = {}
  get_data(interface, query_options)['response']
end

#update(class_to_update, data_to_update) ⇒ Object



82
83
84
85
# File 'lib/backpack_tf/client.rb', line 82

def update class_to_update, data_to_update
  send_update_to_master_hash(class_to_update, data_to_update)
  refresh_class_hash(class_to_update)
end