Class: Auth

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

Direct Known Subclasses

Metabypass

Instance Method Summary collapse

Constructor Details

#initialize(client_id, client_secret, email, password) ⇒ Auth

attr_reader :email, :password, :client_id, :client_secret,



10
11
12
13
14
15
16
17
# File 'lib/auth/auth.rb', line 10

def initialize(client_id,client_secret,email,password)
  @client_id = client_id
  @client_secret = client_secret
  @password = password
  @email = email
  @access_token_file_path = File.join(__dir__, 'metabypass.token')
  @access_token = retrieve_access_token || generate_access_token
end

Instance Method Details

#generate_access_tokenObject



20
21
22
23
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/auth/auth.rb', line 20

def generate_access_token

  request_url = 'https://app.metabypass.tech/CaptchaSolver/oauth/token'

  params = {
    'grant_type' => 'password',
    'client_id' => @client_id,
    'client_secret' => @client_secret,
    'username' => @email,
    'password' => @password
  }

  headers = {
    'Content-Type' => 'application/json',
    'Accept' => 'application/json'
  }

  #webservice response
  response = send_request(request_url, params, 'POST', headers)

  if response.empty?
    message = 'error! server response is empty'

    #logger
    logger.error(message)
    return false
  end


  headers = JSON.parse(response['headers'])
  body = JSON.parse(response['body'])

  if headers['http_code'] == 200
    @access_token = body['access_token']

    File.write(@access_token_file_path, body['access_token'])
    return body['access_token']
  else
    message = 'error! unauth'
    #logger
    logger.error(message)
    return false
  end

end