Class: Gamercard::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/gamercard/client.rb

Constant Summary collapse

GAMERCARD_SERVICE_URL =
"http://gamercard.xbox.com"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(gamertag) ⇒ Client

Returns a new instance of Client.



9
10
11
# File 'lib/gamercard/client.rb', line 9

def initialize gamertag
  self.gamertag = gamertag
end

Instance Attribute Details

#gamertagObject

Returns the value of attribute gamertag.



5
6
7
# File 'lib/gamercard/client.rb', line 5

def gamertag
  @gamertag
end

Instance Method Details

#build_urlObject



36
37
38
# File 'lib/gamercard/client.rb', line 36

def build_url
  "#{gamertag}.card"
end

#connectionObject



30
31
32
33
34
# File 'lib/gamercard/client.rb', line 30

def connection
  @connection ||= Faraday.new(:url => GAMERCARD_SERVICE_URL) do |b|
    b.adapter Gamercard.adapter
  end
end

#fetchObject



13
14
15
16
17
18
19
20
# File 'lib/gamercard/client.rb', line 13

def fetch
  response = fetch_with_redirect
  if response.success?
    parse response
  else
    response
  end
end

#fetch_with_redirectObject



22
23
24
25
26
27
28
# File 'lib/gamercard/client.rb', line 22

def fetch_with_redirect
  resp = connection.get(build_url)
  if resp.status == 302 && resp['location']
    resp = connection.get(resp['location'])
  end
  resp
end

#parse(response) ⇒ Object



40
41
42
# File 'lib/gamercard/client.rb', line 40

def parse response
  CardParser.parse response
end