Class: Crawlbase::API
- Inherits:
-
Object
- Object
- Crawlbase::API
- Defined in:
- lib/crawlbase/api.rb
Direct Known Subclasses
Constant Summary collapse
- INVALID_TOKEN =
'Token is required'- INVALID_URL =
'URL is required'- DEFAULT_TIMEOUT =
90
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Crawlbase HTTP response code for the API call itself.
-
#cb_status ⇒ Object
readonly
Crawlbase status from the response (preferred name for former
pc_status). -
#original_status ⇒ Object
readonly
Crawlbase HTTP response code for the API call itself.
-
#status_code ⇒ Object
readonly
Crawlbase HTTP response code for the API call itself.
-
#storage_url ⇒ Object
readonly
Crawlbase HTTP response code for the API call itself.
-
#timeout ⇒ Object
readonly
Crawlbase HTTP response code for the API call itself.
-
#token ⇒ Object
readonly
Crawlbase HTTP response code for the API call itself.
-
#url ⇒ Object
readonly
Crawlbase HTTP response code for the API call itself.
Instance Method Summary collapse
- #get(url, options = {}) ⇒ Object
-
#initialize(options = {}) ⇒ API
constructor
A new instance of API.
-
#pc_status ⇒ Object
Deprecated: use #cb_status.
- #post(url, data, options = {}) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ API
Returns a new instance of API.
19 20 21 22 23 24 |
# File 'lib/crawlbase/api.rb', line 19 def initialize( = {}) raise INVALID_TOKEN if [:token].nil? @token = [:token] @timeout = .fetch(:timeout, DEFAULT_TIMEOUT) end |
Instance Attribute Details
#body ⇒ Object (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_status ⇒ Object (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_status ⇒ Object (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_code ⇒ Object (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_url ⇒ Object (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 |
#timeout ⇒ Object (readonly)
Crawlbase HTTP response code for the API call itself.
10 11 12 |
# File 'lib/crawlbase/api.rb', line 10 def timeout @timeout end |
#token ⇒ Object (readonly)
Crawlbase HTTP response code for the API call itself.
10 11 12 |
# File 'lib/crawlbase/api.rb', line 10 def token @token end |
#url ⇒ Object (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
32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/crawlbase/api.rb', line 32 def get(url, = {}) raise INVALID_URL if url.empty? uri = prepare_uri(url, ) http = build_http(uri) request = Net::HTTP::Get.new(uri.request_uri) response = http.request(request) prepare_response(response, [:format]) self end |
#pc_status ⇒ Object
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
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, = {}) raise INVALID_URL if url.empty? uri = prepare_uri(url, ) http = build_http(uri) content_type = [:post_content_type].to_s.include?('json') ? { 'Content-Type': 'text/json' } : nil request = Net::HTTP::Post.new(uri.request_uri, content_type) if [: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, [:format]) self end |