Class: Snafu::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty, Achievements, Giants, Locations, Util
Defined in:
lib/snafu/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util

#glitch_time

Methods included from Giants

#get_giants, #get_giants_favor

Methods included from Achievements

#achievement_count, #get_achievements

Methods included from Locations

#get_hub, #get_hubs, #get_hubs!, #get_street

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



22
23
24
# File 'lib/snafu/client.rb', line 22

def initialize(options={})
  @oauth_token = options[:oauth_token]
end

Instance Attribute Details

#last_request_resultObject

Returns the value of attribute last_request_result.



20
21
22
# File 'lib/snafu/client.rb', line 20

def last_request_result
  @last_request_result
end

#oauth_tokenObject

Returns the value of attribute oauth_token.



20
21
22
# File 'lib/snafu/client.rb', line 20

def oauth_token
  @oauth_token
end

Instance Method Details

#call(method, query_parameters = {}) ⇒ Object

Make a raw call to the Glitch API.

snafu = Snafu.new(:oauth_token => "some-token")
snafu.call("calendar.getHolidays")

For Glitch methods which require authentication, set :authentication => true

snafu = snafu.call("players.stats", :authenticate => true)

Invalid method calls will raise a GlitchAPIError



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/snafu/client.rb', line 36

def call(method, query_parameters={})
  unless method.is_a? String
    raise ArgumentError.new("Method argument must be a string")
  end

  options = { :format => :json }
  unless query_parameters.empty?
    options[:query] = query_parameters
    if options[:query].has_key?(:authenticate) && options[:query][:authenticate] == true
      if self.oauth_token.nil?
        raise GlitchAuthenticationError.new("You cannot perform an authenticated call without an oauth token")
      end

      # Replace the authenticate key with the oauth token
      options[:query].delete(:authenticate)
      options[:query].update(:oauth_token => @oauth_token)
    end
  end
  request_uri = "/#{method}"
  parse_response(self.class.get(request_uri, options))
end

#parse_response(response) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/snafu/client.rb', line 58

def parse_response(response)
  if response["error"] == "invalid_token"
    raise GlitchAuthenticationError.new("Invalid Token")
  elsif response["ok"] == 0
    if response["error"] == "missing_scope"
      raise GlitchAuthenticationError.new("The token supplied has insufficient scope for this API method")
    elsif response["error"] == "not_authenticated"
      raise GlitchAuthenticationError.new("This API method requires authentication and no OAuth token has been supplied")
    else
      raise GlitchAPIError.new(response["error"])
    end
  else
    @last_request_result = response
    response
  end
end