Class: ProxyCrawl::API
- Inherits:
-
Object
- Object
- ProxyCrawl::API
- Defined in:
- lib/proxycrawl/api.rb
Direct Known Subclasses
Constant Summary collapse
- INVALID_TOKEN =
'Token is required'- INVALID_URL =
'URL is required'
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#original_status ⇒ Object
readonly
Returns the value of attribute original_status.
-
#pc_status ⇒ Object
readonly
Returns the value of attribute pc_status.
-
#status_code ⇒ Object
readonly
Returns the value of attribute status_code.
-
#storage_url ⇒ Object
readonly
Returns the value of attribute storage_url.
-
#timeout ⇒ Object
readonly
Returns the value of attribute timeout.
-
#token ⇒ Object
readonly
Returns the value of attribute token.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
- #get(url, options = {}) ⇒ Object
-
#initialize(options = {}) ⇒ API
constructor
A new instance of API.
- #post(url, data, options = {}) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ API
Returns a new instance of API.
14 15 16 17 18 19 |
# File 'lib/proxycrawl/api.rb', line 14 def initialize( = {}) raise INVALID_TOKEN if [:token].nil? @token = [:token] @timeout = [:timeout] || 120 end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
9 10 11 |
# File 'lib/proxycrawl/api.rb', line 9 def body @body end |
#original_status ⇒ Object (readonly)
Returns the value of attribute original_status.
9 10 11 |
# File 'lib/proxycrawl/api.rb', line 9 def original_status @original_status end |
#pc_status ⇒ Object (readonly)
Returns the value of attribute pc_status.
9 10 11 |
# File 'lib/proxycrawl/api.rb', line 9 def pc_status @pc_status end |
#status_code ⇒ Object (readonly)
Returns the value of attribute status_code.
9 10 11 |
# File 'lib/proxycrawl/api.rb', line 9 def status_code @status_code end |
#storage_url ⇒ Object (readonly)
Returns the value of attribute storage_url.
9 10 11 |
# File 'lib/proxycrawl/api.rb', line 9 def storage_url @storage_url end |
#timeout ⇒ Object (readonly)
Returns the value of attribute timeout.
9 10 11 |
# File 'lib/proxycrawl/api.rb', line 9 def timeout @timeout end |
#token ⇒ Object (readonly)
Returns the value of attribute token.
9 10 11 |
# File 'lib/proxycrawl/api.rb', line 9 def token @token end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
9 10 11 |
# File 'lib/proxycrawl/api.rb', line 9 def url @url end |
Instance Method Details
#get(url, options = {}) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/proxycrawl/api.rb', line 21 def get(url, = {}) raise INVALID_URL if url.empty? uri = prepare_uri(url, ) req = Net::HTTP::Get.new(uri) = { read_timeout: timeout, use_ssl: uri.scheme == 'https', verify_mode: OpenSSL::SSL::VERIFY_NONE } response = Net::HTTP.start(uri.hostname, uri.port, ) { |http| http.request(req) } prepare_response(response, [:format]) self end |
#post(url, data, options = {}) ⇒ Object
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/proxycrawl/api.rb', line 40 def post(url, data, = {}) raise INVALID_URL if url.empty? uri = prepare_uri(url, ) http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true 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 |