Class: Crawlbase::API

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

Direct Known Subclasses

ScraperAPI, ScreenshotsAPI

Constant Summary collapse

INVALID_TOKEN =
'Token is required'
INVALID_URL =
'URL is required'
DEFAULT_TIMEOUT =
90

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ API

Returns a new instance of API.

Raises:



19
20
21
22
23
24
# File 'lib/crawlbase/api.rb', line 19

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

  @token = options[:token]
  @timeout = options.fetch(:timeout, DEFAULT_TIMEOUT)
end

Instance Attribute Details

#bodyObject (readonly)

Crawlbase HTTP response code for the API call itself.



10
11
12
# File 'lib/crawlbase/api.rb', line 10

def body
  @body
end

#cb_statusObject (readonly)

Crawlbase status from the response (preferred name for former pc_status).



13
14
15
# File 'lib/crawlbase/api.rb', line 13

def cb_status
  @cb_status
end

#original_statusObject (readonly)

Crawlbase HTTP response code for the API call itself.



10
11
12
# File 'lib/crawlbase/api.rb', line 10

def original_status
  @original_status
end

#status_codeObject (readonly)

Crawlbase HTTP response code for the API call itself.



10
11
12
# File 'lib/crawlbase/api.rb', line 10

def status_code
  @status_code
end

#storage_urlObject (readonly)

Crawlbase HTTP response code for the API call itself.



10
11
12
# File 'lib/crawlbase/api.rb', line 10

def storage_url
  @storage_url
end

#timeoutObject (readonly)

Crawlbase HTTP response code for the API call itself.



10
11
12
# File 'lib/crawlbase/api.rb', line 10

def timeout
  @timeout
end

#tokenObject (readonly)

Crawlbase HTTP response code for the API call itself.



10
11
12
# File 'lib/crawlbase/api.rb', line 10

def token
  @token
end

#urlObject (readonly)

Crawlbase HTTP response code for the API call itself.



10
11
12
# File 'lib/crawlbase/api.rb', line 10

def url
  @url
end

Instance Method Details

#get(url, options = {}) ⇒ Object

Raises:



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/crawlbase/api.rb', line 32

def get(url, options = {})
  raise INVALID_URL if url.empty?

  uri = prepare_uri(url, options)
  http = build_http(uri)
  request = Net::HTTP::Get.new(uri.request_uri)
  response = http.request(request)

  prepare_response(response, options[:format])

  self
end

#pc_statusObject

Deprecated: use #cb_status. Returns the same resolved Crawlbase status.



27
28
29
30
# File 'lib/crawlbase/api.rb', line 27

def pc_status
  StatusResolution.warn_pc_status_deprecated
  @pc_status
end

#post(url, data, options = {}) ⇒ Object

Raises:



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/crawlbase/api.rb', line 45

def post(url, data, options = {})
  raise INVALID_URL if url.empty?

  uri = prepare_uri(url, options)
  http = build_http(uri)

  content_type = options[:post_content_type].to_s.include?('json') ? { 'Content-Type': 'text/json' } : nil

  request = Net::HTTP::Post.new(uri.request_uri, content_type)

  if options[:post_content_type].to_s.include?('json')
    request.body = data.to_json
  else
    request.set_form_data(data)
  end

  response = http.request(request)

  prepare_response(response, options[:format])

  self
end