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

#initialize ⇒ Client

Returns a new instance of Client.



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

def initialize
  @db = nil 
end

Instance Attribute Details

#db ⇒ Object (readonly)

Instance Methods



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

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
58
59
60
# File 'lib/backpack_tf/client.rb', line 35

def self.build_url_via action, query_options = {}
  case action
  when :get_prices, :prices
    version = 4
    interface_url = "/#{Prices.interface}/v#{version}/?"
  when :get_currencies, :currencies
    version = 1
    interface_url = "/#{Currencies.interface}/v#{version}/?"
  when :get_special_items, :special_items
    raise RuntimeError, "Unfortunately, this interface is not yet supported."
    #version = 1
    #interface_url = "/IGetSpecialItems/v#{version}/?"
  when :get_users, :users
    raise RuntimeError, "Unfortunately, this interface is not yet supported."
    #version = 3
    #interface_url = "/IGetUsers/v#{version}/?"
  when :get_user_listings, :user_listings
    raise RuntimeError, "Unfortunately, this interface is not yet supported."
    #version = 1
    #interface_url = "/IGetUserListings/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_var ⇒ Object



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

def self.env_var; @@env_var; end

.extract_query_string(options = {}) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/backpack_tf/client.rb', line 62

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



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

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

#update(class_to_update, data_to_update) ⇒ Object



85
86
87
88
# File 'lib/backpack_tf/client.rb', line 85

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