Class: Phantomblaster::Client
- Inherits:
-
Object
- Object
- Phantomblaster::Client
- Defined in:
- lib/phantomblaster/client.rb
Constant Summary collapse
- API_URL =
'https://phantombuster.com/api/v1'.freeze
Class Method Summary collapse
- .build_request(path, args = {}) {|[http, uri]| ... } ⇒ Object
- .get(path, args = {}) ⇒ Object
- .post(path, args = {}) ⇒ Object
Class Method Details
.build_request(path, args = {}) {|[http, uri]| ... } ⇒ Object
9 10 11 12 13 14 15 16 17 18 |
# File 'lib/phantomblaster/client.rb', line 9 def self.build_request(path, args = {}, &_block) uri = URI("#{API_URL}#{path}") new_query = URI.decode_www_form(uri.query || '') args.to_a.each { |arg| new_query << arg } new_query << ['key', Phantomblaster.configuration.api_key] uri.query = URI.encode_www_form(new_query) http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true yield [http, uri] end |
.get(path, args = {}) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/phantomblaster/client.rb', line 20 def self.get(path, args = {}) response = build_request(path, args) do |http, uri| http.request(Net::HTTP::Get.new(uri)) end unless response.is_a?(Net::HTTPSuccess) raise APIError, "Unexpected response: #{response.read_body}" end JSON.parse(response.read_body)['data'] rescue JSON::ParserError => _e raise APIError, "Could not parse response: #{response.read_body}" end |
.post(path, args = {}) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/phantomblaster/client.rb', line 34 def self.post(path, args = {}) response = build_request(path, args) do |http, uri| http.request(Net::HTTP::Post.new(uri)) end unless response.is_a?(Net::HTTPSuccess) raise APIError, "Unexpected response: #{response.read_body}" end JSON.parse(response.read_body) rescue JSON::ParserError => _e raise APIError, "Could not parse response: #{response.read_body}" end |