Class: GameRocket::PlayerGateway

Inherits:
Object
  • Object
show all
Defined in:
lib/gamerocket/player_gateway.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(gateway) ⇒ PlayerGateway

Returns a new instance of PlayerGateway.



4
5
6
7
# File 'lib/gamerocket/player_gateway.rb', line 4

def initialize(gateway)
  @gateway = gateway
  @config = gateway.config
end

Class Method Details

.createSignatureObject



9
10
11
12
# File 'lib/gamerocket/player_gateway.rb', line 9

def self.createSignature
  signature = ['name', 'locale']
  signature
end

Instance Method Details

#_do_create(url, params = nil) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/gamerocket/player_gateway.rb', line 39

def _do_create(url, params = nil)
  json = @config.http.post url, params
  if json["player"]
    SuccessfulResult.new(:player => Player._new(@gateway, json["player"]))
  elsif json["error"]
    ErrorResult.new(@gateway, json)
  else
    raise "expected :player or :error"
  end
end

#_do_update(http_verb, url, params) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# File 'lib/gamerocket/player_gateway.rb', line 50

def _do_update(http_verb, url, params)
  json = @config.http.send http_verb, url, params
  if json["player"]
    SuccessfulResult.new(:player => Player._new(@gateway, json["player"]))
  elsif json["error"]
    ErrorResult.new(@gateway, json)
  else
    raise UnexpectedError, "expected :player or :error"
  end
end

#create(attributes = {}) ⇒ Object



14
15
16
17
# File 'lib/gamerocket/player_gateway.rb', line 14

def create(attributes = {})
  Util.verify_keys(PlayerGateway.createSignature, attributes)
  result = _do_create "/games/" + GameRocket::Configuration::apiKey + "/players", attributes
end

#delete(id) ⇒ Object



34
35
36
37
# File 'lib/gamerocket/player_gateway.rb', line 34

def delete(id)
  json = @config.http.delete("/players/#{id}")
  SuccessfulResult.new
end

#find(id) ⇒ Object

Raises:

  • (ArgumentError)


19
20
21
22
23
24
25
26
27
28
# File 'lib/gamerocket/player_gateway.rb', line 19

def find(id)
  raise ArgumentError, "id contains invalid characters" unless id.to_s =~ /\A[\w_]+\z/
  raise ArgumentError, "id cannot be blank" if id.nil?|| id.to_s.strip ==""
  json = @config.http.get("/players/#{id}")
  if !json["player"].nil?
    Player._new(@gateway, json["player"])
  else
    nil
  end
end

#update(id, attributes) ⇒ Object



30
31
32
# File 'lib/gamerocket/player_gateway.rb', line 30

def update(id, attributes)
  _do_update(:put, "/players/#{id}", attributes)
end