Class: Strutta::API

Inherits:
Object
  • Object
show all
Includes:
Version
Defined in:
lib/strutta-api.rb

Overview

Strutta API engine Handles API requests and errors

Constant Summary

Constants included from Version

Version::VERSION, Version::VERSION_PATH

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token, host = 'http://strutta-api.herokuapp.com/', path = Version::VERSION_PATH) ⇒ Strutta::API

Initializes the Strutta API wrapper

Parameters:

  • token (String)

    your Strutta API token

  • host (String) (defaults to: 'http://strutta-api.herokuapp.com/')

    Strutta API host URL - used to switched between prod/staging

  • path (String) (defaults to: Version::VERSION_PATH)

    Strutta API host path - used to switched between versions

[View source]

29
30
31
32
33
34
35
36
37
# File 'lib/strutta-api.rb', line 29

def initialize(token, host = 'http://strutta-api.herokuapp.com/', path = Version::VERSION_PATH)
  fail Error, 'You must provide a Strutta API key' unless token

  @host = host
  @path = path
  @token = token
  @session = Excon.new @host
  @debug = debug
end

Instance Attribute Details

#debugObject

Returns the value of attribute debug.


21
22
23
# File 'lib/strutta-api.rb', line 21

def debug
  @debug
end

#hostObject

Returns the value of attribute host.


21
22
23
# File 'lib/strutta-api.rb', line 21

def host
  @host
end

#pathObject

Returns the value of attribute path.


21
22
23
# File 'lib/strutta-api.rb', line 21

def path
  @path
end

#sessionObject

Returns the value of attribute session.


21
22
23
# File 'lib/strutta-api.rb', line 21

def session
  @session
end

#tokenObject

Returns the value of attribute token.


21
22
23
# File 'lib/strutta-api.rb', line 21

def token
  @token
end

Instance Method Details

#call(method, url, params = {}) ⇒ Hash

Makes an API call

Parameters:

  • method (String)

    the required HTTP method

  • url (String)

    URL for the call

  • params (Hash) (defaults to: {})

    Parameters for the call

Returns:

  • (Hash)

    Parsed body of the response

[View source]

45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/strutta-api.rb', line 45

def call(method, url, params = {})
  params = JSON.generate(params)
  r = @session.send(method, path: "#{@path}#{url}", headers: api_headers, body: params)

  # Delete calls have no JSON return
  return true if r.status == 204

  # Raise exceptions on error response codes
  cast_error(r.status, r.body) if r.status >= 400

  JSON.parse(r.body)
end

#games(id = nil) ⇒ Strutta::Games

Instantiates a Strutta::Games object

Parameters:

  • id (Integer, nil) (defaults to: nil)

    the ID of the Strutta game

Returns:

[View source]

62
63
64
# File 'lib/strutta-api.rb', line 62

def games(id = nil)
  Games.new id, self
end