Class: ProxyCrawl::API
- Inherits:
-
Object
- Object
- ProxyCrawl::API
- Defined in:
- lib/proxycrawl/api.rb
Constant Summary collapse
- BASE_URL =
'https://api.proxycrawl.com'
- 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.
-
#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.
15 16 17 18 19 |
# File 'lib/proxycrawl/api.rb', line 15 def initialize( = {}) raise INVALID_TOKEN if [:token].nil? @token = [:token] end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
8 9 10 |
# File 'lib/proxycrawl/api.rb', line 8 def body @body end |
#original_status ⇒ Object (readonly)
Returns the value of attribute original_status.
8 9 10 |
# File 'lib/proxycrawl/api.rb', line 8 def original_status @original_status end |
#pc_status ⇒ Object (readonly)
Returns the value of attribute pc_status.
8 9 10 |
# File 'lib/proxycrawl/api.rb', line 8 def pc_status @pc_status end |
#status_code ⇒ Object (readonly)
Returns the value of attribute status_code.
8 9 10 |
# File 'lib/proxycrawl/api.rb', line 8 def status_code @status_code end |
#token ⇒ Object (readonly)
Returns the value of attribute token.
8 9 10 |
# File 'lib/proxycrawl/api.rb', line 8 def token @token end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
8 9 10 |
# File 'lib/proxycrawl/api.rb', line 8 def url @url end |
Instance Method Details
#get(url, options = {}) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/proxycrawl/api.rb', line 21 def get(url, = {}) raise INVALID_URL if url.empty? uri = prepare_uri(url, ) response = Net::HTTP.get_response(uri) prepare_response(response, [:format]) self end |
#post(url, data, options = {}) ⇒ Object
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 |
# File 'lib/proxycrawl/api.rb', line 33 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 |