Class: Chaos::API

Inherits:
Object
  • Object
show all
Defined in:
lib/chaos/api.rb,
lib/chaos/api/version.rb

Constant Summary collapse

URLS =
{
  development: 'http://localhost:3000',
  production: 'http://chaosgame.herokuapp.com'
}
ACTIONS =
{
  get_available_games: {
    route: '/games/available'
  },
  create_game: {
    route: '/games/create'
  },
  game_ready: {
    route: '/games/ready'
  },
  join_game: {
    route: '/games/join'
  },
  get_stats: {
    route: '/games/get-stats'
  },
  send_actions: {
    route: '/actions/send-selected'
  },
  get_game_locations: {
    route: '/games/locations'
  },
  team_mates:{
    route: '/games/team-mates'
  },
  start_game: {
    route: '/games/start'
  }
}
VERSION =
"0.0.6"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env = :development) ⇒ API

Returns a new instance of API.



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

def initialize env = :development
  @url_prefix = URLS[env.to_sym] if URLS.keys.include? env.to_sym

  @connection = Faraday.new(
    url: @url_prefix,
    ssl: {
      ca_path: '/usr/lib/ssl/certs',
      verify: false
    }
  )
end

Instance Attribute Details

#url_prefixObject (readonly)

Returns the value of attribute url_prefix.



57
58
59
# File 'lib/chaos/api.rb', line 57

def url_prefix
  @url_prefix
end

Instance Method Details

#call(command, params = {}, as_json = false) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/chaos/api.rb', line 59

def call command, params = {}, as_json = false
  method = ACTIONS[command][:verb] || :post
  route = ACTIONS[command][:route]

  response = @connection.send(method, route, params)
  return JSON(response.body) if as_json
  response
end

#debug(command, params = {}, as_json = false) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/chaos/api.rb', line 68

def debug command, params = {}, as_json = false
  response = call(command, params, as_json)

  if as_json
    ap response
    return response
  end

  ap response.status
  parsed = (JSON.parse(response.body) rescue response.body)
  ap parsed
  response
end