Class: GodvilleKit::APIRequester

Inherits:
Object
  • Object
show all
Defined in:
lib/godville_kit/api_requester.rb

Defined Under Namespace

Classes: AuthenticationCaptchaException, InvalidAuthenticationException, UnexpectedResponseException

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username, password, hero_guid, pantheons_guid) ⇒ APIRequester

Returns a new instance of APIRequester.



12
13
14
15
16
17
# File 'lib/godville_kit/api_requester.rb', line 12

def initialize(username, password, hero_guid, pantheons_guid)
  @username = username
  @password = password
  @hero_guid = hero_guid
  @pantheons_guid = pantheons_guid
end

Instance Attribute Details

#hero_guidObject (readonly)

Returns the value of attribute hero_guid.



3
4
5
# File 'lib/godville_kit/api_requester.rb', line 3

def hero_guid
  @hero_guid
end

#pantheons_guidObject (readonly)

Returns the value of attribute pantheons_guid.



3
4
5
# File 'lib/godville_kit/api_requester.rb', line 3

def pantheons_guid
  @pantheons_guid
end

#passwordObject (readonly)

Returns the value of attribute password.



3
4
5
# File 'lib/godville_kit/api_requester.rb', line 3

def password
  @password
end

#usernameObject (readonly)

Returns the value of attribute username.



3
4
5
# File 'lib/godville_kit/api_requester.rb', line 3

def username
  @username
end

Instance Method Details

#authenticateObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/godville_kit/api_requester.rb', line 24

def authenticate
  return if authenticated?

  RestClient.post(
    'https://godvillegame.com/login/login',
    { username: @username,
      password: @password },
    content_type: :json, accept: :json
  )do |response, request, result, &block|
    case response.code
    when 302
      @cookies = response.cookies
    when 200
      if response.to_s.include? 'captcha'
        raise AuthenticationCaptchaException, 'Too many failed logins, manual login required due to captcha'
      else
        raise InvalidAuthenticationException, 'Invalid Username/Password'
      end
    else
      raise UnexpectedResponseCodeException, "Unexpected response code #{response.code}"
    end
  end
rescue => e
  @cookies = nil
  puts e.message
end

#authenticated?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/godville_kit/api_requester.rb', line 51

def authenticated?
  !@cookies.nil?
end

#request_heroObject



19
20
21
22
# File 'lib/godville_kit/api_requester.rb', line 19

def request_hero
  authenticate
  GodvilleKit::Hero.new(request_raw_hero_data, request_raw_pantheons_data)
end

#request_raw_hero_dataObject

Request the raw hero data from godville



56
57
58
59
60
61
62
63
64
# File 'lib/godville_kit/api_requester.rb', line 56

def request_raw_hero_data
  return unless authenticated?

  response = RestClient.get(
    "https://godvillegame.com/fbh/feed?a=#{@hero_guid}",
    cookies: @cookies, content_type: :json, accept: :json
  )
  JSON.parse(response)
end

#request_raw_pantheons_dataObject

Request the raw pantheons data from godville



67
68
69
70
71
72
73
74
75
# File 'lib/godville_kit/api_requester.rb', line 67

def request_raw_pantheons_data
  return unless authenticated?

  response = RestClient.get(
    "https://godvillegame.com/fbh/feed?a=#{@pantheons_guid}",
    cookies: @cookies, content_type: :json, accept: :json
  )
  JSON.parse(response)
end