Class: Argonuts::API
- Inherits:
-
Object
- Object
- Argonuts::API
- Defined in:
- lib/argonuts/api.rb
Overview
Argonuts::API is responsible for making API requests. It takes a Argonuts::Client to send api_key, endpoint and region.
Direct Known Subclasses
Class Method Summary collapse
Class Method Details
.headers(cli) ⇒ Object
9 10 11 12 13 14 15 16 17 18 |
# File 'lib/argonuts/api.rb', line 9 def self.headers(cli) if cli.api_key.nil? raise Argonuts::Error, "You must specify an API key with Argonuts.api_key=" end { 'X-Auth-Token': cli.api_key, 'User-Agent': "Argonuts/#{Argonuts::VERSION}", } end |
.request(verb, path, options = {}) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/argonuts/api.rb', line 20 def self.request(verb, path, = {}) cli = [:client] || Argonuts.default_client url = URI("#{cli.endpoint}#{path}") http = Net::HTTP.new(url.host, url.port) http.use_ssl = true if url.instance_of? URI::HTTPS case verb when :get request = Net::HTTP::Get.new(url, 'Content-Type' => 'application/json') when :post request = Net::HTTP::Post.new(url, 'Content-Type' => 'application/json') request.body = [:json].to_json end headers(cli).map { |k, v| request[k] = v } resp = http.request(request) if resp.code.to_i > 399 # if response is 400 or 401, we return the error message and error code raise Argonuts::Error, "Server returned HTTP status #{resp.code}." end return JSON::parse(resp.body) end |