module BoardGameGem

Constants

API_1_ROOT
API_2_ROOT
MAX_ATTEMPTS
VERSION

Public Class Methods

get_collection(username, options = {}) click to toggle source
# File lib/board-game-gem.rb, line 49
def self.get_collection(username, options = {})
        options[:username] = username
        collection_xml = BoardGameGem.request_xml("collection", options)
        if collection_xml.css("error").length > 0
                nil
        else
                BGGCollection.new(collection_xml)
        end
end
get_family(id, options = {}) click to toggle source
# File lib/board-game-gem.rb, line 37
def self.get_family(id, options = {})
        options[:id] = id
        family = BGGFamily.new(BoardGameGem.request_xml("family", options))
        family.id == 0 ? nil : family
end
get_item(id, statistics = false, api = 2, options = {}) click to toggle source
# File lib/board-game-gem.rb, line 9
def self.get_item(id, statistics = false, api = 2, options = {})
        options[:id] = id
        options[:stats] = statistics ? 1 : 0
        item = BGGItem.new(BoardGameGem.request_xml(api == 2 ? "thing" : "boardgame", options, api), api)
        item.id == 0 ? nil : item
end
get_items(ids, statistics = false, api = 2, options = {}) click to toggle source
# File lib/board-game-gem.rb, line 16
def self.get_items(ids, statistics = false, api = 2, options = {})
        options[:id] = ids.join(",")
        options[:stats] = statistics ? 1 : 0
        item_list = []
        if api == 2
                path = "thing"
                element = "item"
        else
                path = "boardgame"
                element = "boardgame"
        end

        item_xml = BoardGameGem.request_xml(path, options, api)               
        item_xml.css(element).wrap("<item_data></item_data>")         
        item_xml.css("item_data").each do |item_data|
                item_list.push(BGGItem.new(item_data, api))
        end

        item_list
end
get_user(username, options = {}) click to toggle source
# File lib/board-game-gem.rb, line 43
def self.get_user(username, options = {})
        options[:name] = username
        user = BGGUser.new(BoardGameGem.request_xml("user", options))
        user.id == 0 ? nil : user
end

Private Class Methods

hash_to_uri(hash) click to toggle source
# File lib/board-game-gem.rb, line 91
def self.hash_to_uri(hash)
        return hash.to_a.map { |x| "#{x[0]}=#{x[1]}" }.join("&")
end
request_xml(method, hash, api = 2) click to toggle source
# File lib/board-game-gem.rb, line 70
def self.request_xml(method, hash, api = 2)
        params = BoardGameGem.hash_to_uri(hash)
        if api == 2
                api_path = "#{API_2_ROOT}/#{method}?#{params}"
        else
                api_path = "#{API_1_ROOT}/#{method}/#{hash[:id]}?stats=#{hash[:stats] ? 1 : 0}"
        end
        p api_path
        value = BoardGameGem.retryable(tries: MAX_ATTEMPTS, on: OpenURI::HTTPError) do
                open(api_path) do |file|
                        if file.status[0] != "200"
                                sleep 0.05
                                throw OpenURI::HTTPError
                        else
                                value = Nokogiri::XML(file.read)
                        end
                end
        end 
        value
end
retryable(options = {}) { || ... } click to toggle source
# File lib/board-game-gem.rb, line 95
def self.retryable(options = {}, &block)
  opts = { :tries => 1, :on => Exception }.merge(options)

  retry_exception, retries = opts[:on], opts[:tries]

  begin
    return yield
  rescue retry_exception
    retry if (retries -= 1) > 0
  end

  yield
end