Class: ProxyCrawl::LeadsAPI

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

Constant Summary collapse

INVALID_TOKEN =
'Token is required'
INVALID_DOMAIN =
'Domain is required'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ LeadsAPI

Returns a new instance of LeadsAPI.

Raises:



14
15
16
17
18
19
# File 'lib/proxycrawl/leads_api.rb', line 14

def initialize(options = {})
  raise INVALID_TOKEN if options[:token].nil? || options[:token].empty?

  @token = options[:token]
  @timeout = options[:timeout] || 120
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



9
10
11
# File 'lib/proxycrawl/leads_api.rb', line 9

def body
  @body
end

#remaining_requestsObject (readonly)

Returns the value of attribute remaining_requests.



9
10
11
# File 'lib/proxycrawl/leads_api.rb', line 9

def remaining_requests
  @remaining_requests
end

#status_codeObject (readonly)

Returns the value of attribute status_code.



9
10
11
# File 'lib/proxycrawl/leads_api.rb', line 9

def status_code
  @status_code
end

#successObject (readonly)

Returns the value of attribute success.



9
10
11
# File 'lib/proxycrawl/leads_api.rb', line 9

def success
  @success
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



9
10
11
# File 'lib/proxycrawl/leads_api.rb', line 9

def timeout
  @timeout
end

#tokenObject (readonly)

Returns the value of attribute token.



9
10
11
# File 'lib/proxycrawl/leads_api.rb', line 9

def token
  @token
end

Instance Method Details

#get(domain) ⇒ Object

Raises:



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/proxycrawl/leads_api.rb', line 21

def get(domain)
  raise INVALID_DOMAIN if domain.empty?

  uri = URI('https://api.proxycrawl.com/leads')
  uri.query = URI.encode_www_form({ token: token, domain: domain })

  req = Net::HTTP::Get.new(uri)

  req_options = {
    read_timeout: timeout,
    use_ssl: uri.scheme == 'https',
    verify_mode: OpenSSL::SSL::VERIFY_NONE
  }

  response = Net::HTTP.start(uri.hostname, uri.port, req_options) { |http| http.request(req) }
  @status_code = response.code.to_i
  @body = response.body

  json_body = JSON.parse(response.body)
  @success = json_body['success']
  @remaining_requests = json_body['remaining_requests'].to_i

  self
end

#postObject



46
47
48
# File 'lib/proxycrawl/leads_api.rb', line 46

def post
  raise 'Only GET is allowed for the LeadsAPI'
end