Class: Gamewisp::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeClient

Only works if this class is derived (includes) HTTParty

if ENV

debug_output $stdout

end



26
27
28
29
# File 'lib/gamewisp/client.rb', line 26

def initialize
  self.token_store = TokenStore.new
  self.auth = Authorizer.new 'createauth', self.token_store
end

Instance Attribute Details

#authObject

Returns the value of attribute auth.



17
18
19
# File 'lib/gamewisp/client.rb', line 17

def auth
  @auth
end

#token_storeObject

Returns the value of attribute token_store.



16
17
18
# File 'lib/gamewisp/client.rb', line 16

def token_store
  @token_store
end

Instance Method Details

#authorizeObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/gamewisp/client.rb', line 31

def authorize
  puts "Visit the following URL in your browser and authorize #{token_store.app_name} to access your subscription metrics"
  puts "   #{auth.app_authorization_url}"
  puts

  server = auth.create_server_instance
  auth_result = server.get_authentication_token

  puts auth_result
  if auth_result["code"]
    puts "Authorization received"
  else
    raise GamewispError, "Authorization failed."
  end

  auth.renew_tokens_with_auth_code auth_result["code"]
end

#get_subscribersObject



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

def get_subscribers
  url = "https://api.gamewisp.com/pub/v1/channel/subscribers"

  dbg "Client.get_subscribers [access_token]", token_store.access_token

  response = HTTParty
    .get(url,
        :query => {
          :access_token => token_store.access_token,
          :include => 'user,channel,benefits',
        }
      )

  dbg "Client.get_subscribers [response.code]", response.code
  dbg "Client.get_subscribers [response]", response

  response
end

#renew_access_tokenObject



49
50
51
52
53
54
55
# File 'lib/gamewisp/client.rb', line 49

def renew_access_token
  if token_store.refresh_token.nil? || token_store.refresh_token.empty?
    return authorize
  end

  auth.renew_tokens_with_refresh_token token_store.refresh_token
end