class BackpackTF::Client

Attributes

db[R]

Instance Methods

Public Class Methods

api_key(key = nil) click to toggle source
# 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 = {}) click to toggle source
# 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_var() click to toggle source
# File lib/backpack_tf/client.rb, line 14
def self.env_var; @@env_var; end
extract_query_string(options = {}) click to toggle source
# 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
new() click to toggle source
# File lib/backpack_tf/client.rb, line 74
def initialize
  @db = nil 
end

Public Instance Methods

fetch(interface, query_options = {}) click to toggle source
# 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) click to toggle source
# 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

Private Instance Methods

get_data(action, query_options = {}) click to toggle source
# File lib/backpack_tf/client.rb, line 97
def get_data action, query_options = {}
  handle_timeouts do
    url = self.class.build_url_via(action, query_options)
    self.class.get(url)
  end
end
handle_timeouts() { || ... } click to toggle source

HTTParty raises an errors after time limit defined by ::default_timeout

  • if it cannot connect to server, then it raises Net::OpenTimeout

  • if it cannot read response from server, then it raises Net::ReadTimeout

if one of those happen, then an empty hash is returned

# File lib/backpack_tf/client.rb, line 108
def handle_timeouts
  begin
    yield
  rescue Net::OpenTimeout, Net::ReadTimeout
    {}
  end
end
refresh_class_hash(class_to_update) click to toggle source
# File lib/backpack_tf/client.rb, line 93
def refresh_class_hash class_to_update
  class_to_update.response
end
send_update_to_master_hash(class_to_update, data_to_update) click to toggle source
# File lib/backpack_tf/client.rb, line 89
def send_update_to_master_hash class_to_update, data_to_update
  Response.responses( { class_to_update.to_sym => data_to_update } )
end