Sha256: 2e24bba52d9f1fb2999e0a8fd6ad58c45e0f8b891dd7d232a610a5b6a578433c

Contents?: true

Size: 1.81 KB

Versions: 1

Compression:

Stored size: 1.81 KB

Contents

require "gameboy/version"
require 'active_support/all'
require 'json'
require 'rest-client'
require 'hashie'

module Gameboy
  REQUEST_TIMEOUT = 60
  OPEN_TIMEOUT = 5
  @@api_key = "SET_YOUR_API_KEY"
  @@gamer_host = nil # Optional

  def self.api_key=(key)
    @@api_key = key
  end

  def self.gamer_host=(host)
    @@gamer_host = host
  end

  def self.gamer_host
    @@gamer_host ||= "https://gamer.edmodo.com"
  end

  def self.achievements_for_user(user_id, options={})
    api_key = options.delete(:api_key) || @@api_key

    response = RestClient::Request.execute(
      method: :get,
      url: "#{self.gamer_host}/users/#{user_id}/achievements?#{options.to_query}",
      headers: {"Authorization" => "api_key #{api_key}", :content_type => :json, :accept => :json},
      timeout: REQUEST_TIMEOUT,
      open_timeout: OPEN_TIMEOUT
    )
    JSON.parse(response).map{|r| Hashie::Mash.new(r)}
  end

  def self.users(user_ids, options={})
    api_key = options.delete(:api_key) || @@api_key
    options[:ids] = Array.wrap(user_ids).join(',')
    response = RestClient::Request.execute(
      method: :get,
      url: "#{self.gamer_host}/users?#{options.to_query}",
      headers: {"Authorization" => "api_key #{api_key}", :content_type => :json, :accept => :json},
      timeout: REQUEST_TIMEOUT,
      open_timeout: OPEN_TIMEOUT
    )
    JSON.parse(response).map{|r| Hashie::Mash.new(r)}
  end

  def self.user(user_id, options={})
    api_key = options.delete(:api_key) || @@api_key
    response = RestClient::Request.execute(
      method: :get,
      url: "#{self.gamer_host}/users/#{user_id}?#{options.to_query}",
      headers: {"Authorization" => "api_key #{api_key}", :content_type => :json, :accept => :json},
      timeout: REQUEST_TIMEOUT,
      open_timeout: OPEN_TIMEOUT
    )
    Hashie::Mash.new(JSON.parse(response))
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
gameboy-0.1.1 lib/gameboy.rb