Sha256: 47b50292a7d0785cc4e818e5a3d69718ee1f410a9b4de67abac3e5296286654c

Contents?: true

Size: 1.83 KB

Versions: 1

Compression:

Stored size: 1.83 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.upcoming_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/upcoming?#{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.0 lib/gameboy.rb