Class: Scrapio::HttpClient
- Inherits:
-
Object
- Object
- Scrapio::HttpClient
- Defined in:
- lib/scrapio/http_client.rb
Constant Summary collapse
- DEFAULT_BASE_URL =
"https://api.scrapio.dev"- DEFAULT_TIMEOUT =
30
Instance Method Summary collapse
- #get(path, params = {}) ⇒ Object
-
#initialize(api_key, base_url: DEFAULT_BASE_URL, timeout: DEFAULT_TIMEOUT) ⇒ HttpClient
constructor
A new instance of HttpClient.
- #post(path, body = {}) ⇒ Object
Constructor Details
#initialize(api_key, base_url: DEFAULT_BASE_URL, timeout: DEFAULT_TIMEOUT) ⇒ HttpClient
Returns a new instance of HttpClient.
10 11 12 13 14 |
# File 'lib/scrapio/http_client.rb', line 10 def initialize(api_key, base_url: DEFAULT_BASE_URL, timeout: DEFAULT_TIMEOUT) @api_key = api_key @base_url = base_url @timeout = timeout end |
Instance Method Details
#get(path, params = {}) ⇒ Object
16 17 18 19 20 21 |
# File 'lib/scrapio/http_client.rb', line 16 def get(path, params = {}) uri = URI("#{@base_url}#{path}") filtered = params.compact uri.query = URI.encode_www_form(filtered) unless filtered.empty? request(Net::HTTP::Get.new(uri)) end |
#post(path, body = {}) ⇒ Object
23 24 25 26 27 28 29 |
# File 'lib/scrapio/http_client.rb', line 23 def post(path, body = {}) uri = URI("#{@base_url}#{path}") req = Net::HTTP::Post.new(uri) req["Content-Type"] = "application/json" req.body = JSON.generate(body.compact) request(req) end |