class HubbleObservatory::Request
A wrapper around Net::HTTP
to send HTTP requests to the Hubble API and return their result or raise an error if the result is unexpected. The basic way to use Request
is by calling run_request
on an instance.
Public Class Methods
new(attrs: {})
click to toggle source
Initializes a Request
object. @param [Hash] attrs are the options for the request. @option attrs [Symbol] :method (:get) The HTTP method to use. @option attrs [String] :route The (base) route of the API endpoint @option attrs [Hash] :body_attrs ({}) The attributes for the body per the JSON API spec
# File lib/hubble_observatory/request.rb, line 12 def initialize(attrs: {}) @request_type = attrs.fetch :request_type, :get @route = attrs.fetch :route, "talent-accounts" @query_params = attrs.fetch :query_params, {} @body_attrs = attrs.fetch :body_attrs, nil @request_format = attrs.fetch :request_format, :json @auth_header = attrs.fetch :auth_header, false end
Public Instance Methods
parse(response)
click to toggle source
parse the JSON response body
# File lib/hubble_observatory/request.rb, line 29 def parse(response) JSON.parse response.body, symbolize_names: true end
run_request()
click to toggle source
Sends the request and returns the response
# File lib/hubble_observatory/request.rb, line 22 def run_request if response.is_a?(Net::HTTPSuccess) || response.is_a?(Net::HTTPUnprocessableEntity) parse(response) end end
Private Instance Methods
assign_request_body(request)
click to toggle source
# File lib/hubble_observatory/request.rb, line 64 def assign_request_body(request) if @body_attrs request.body = serialize_attributes(attributes: @body_attrs).to_json end end
assign_request_headers(request)
click to toggle source
# File lib/hubble_observatory/request.rb, line 70 def assign_request_headers(request) http_headers = default_headers if @auth_header http_headers.merge!(authorization_header) end http_headers.each_key do |header| request[header] = http_headers[header] end request end
create_http_request()
click to toggle source
# File lib/hubble_observatory/request.rb, line 56 def create_http_request net_http_class = Object.const_get("Net::HTTP::#{@request_type.capitalize}") @http_request ||= net_http_class.new(uri.request_uri).tap do |request| assign_request_body request assign_request_headers request end end
default_headers()
click to toggle source
# File lib/hubble_observatory/request.rb, line 81 def default_headers {"Content-Type" => "application/vnd.api+json"} end
host()
click to toggle source
# File lib/hubble_observatory/request.rb, line 89 def host subdomain = ENV['HUBBLE_ENV'] == 'production' ? 'hubble' : 'rc-hubble' "#{subdomain}.fullscreen.net" end
response()
click to toggle source
# File lib/hubble_observatory/request.rb, line 48 def response Net::HTTP.start(uri.host, 443, use_ssl: true) do |http| http.request create_http_request end rescue *ConnectionError.errors => e raise ConnectionError, e.message end
serialize_attributes(attributes:)
click to toggle source
# File lib/hubble_observatory/request.rb, line 35 def serialize_attributes(attributes:) { data: { type: @route, attributes: attributes } } end
uri()
click to toggle source
# File lib/hubble_observatory/request.rb, line 44 def uri @uri ||= URI::HTTPS.build host: host, path: "/api/v1/#{@route}", query: URI.encode_www_form(@query_params) end