module Fountain::Api::RequestHelper

Fountain API HTTP request helper

Constants

DEFAULT_REQUEST_OPTIONS

Public Instance Methods

request(path, options = {}) click to toggle source
# File lib/fountain/api/request_helper.rb, line 28
def request(path, options = {})
  options = DEFAULT_REQUEST_OPTIONS.merge(options)

  raise Fountain::InvalidMethodError unless %i[get post put delete].include? options[:method]

  http = create_http(options)
  req = create_request(path, options)
  http.start { http.request(req) }
end
request_json(path, options = {}) click to toggle source
# File lib/fountain/api/request_helper.rb, line 21
def request_json(path, options = {})
  expected_response = options.delete :expected_response
  response = request path, options
  check_response response, expected_response
  parse_response response.body
end

Private Instance Methods

add_body(request, body) click to toggle source
# File lib/fountain/api/request_helper.rb, line 111
def add_body(request, body)
  if body.is_a? Hash
    request.body = body.to_json
    request.content_type = 'application/json'
  else
    request.body = body.to_s
  end
end
check_response(response, expected_response = nil) click to toggle source
# File lib/fountain/api/request_helper.rb, line 40
def check_response(response, expected_response = nil)
  expected_response ||= Net::HTTPOK
  case response
  when expected_response then nil
  when Net::HTTPUnauthorized then raise Fountain::AuthenticationError
  when Net::HTTPNotFound then raise Fountain::NotFoundError
  else raise HTTPError, "Invalid http response code: #{response.code}"
  end
end
create_delete_request(path, headers) click to toggle source
# File lib/fountain/api/request_helper.rb, line 93
def create_delete_request(path, headers)
  Net::HTTP::Delete.new(path, headers)
end
create_get_request(path, headers, body) click to toggle source
# File lib/fountain/api/request_helper.rb, line 97
def create_get_request(path, headers, body)
  path += '?' + body.map { |k, v| "#{k}=#{v}" }.join('&') if body
  Net::HTTP::Get.new(path, headers)
end
create_http(options) click to toggle source
# File lib/fountain/api/request_helper.rb, line 56
def create_http(options)
  server = URI.parse Fountain.host_path
  http = Net::HTTP.new(server.host, server.port)

  if server.scheme == 'https'
    http.use_ssl = true
    http.verify_mode = options[:ssl_verify_mode]
    http.ca_file = options[:ssl_ca_file] if options[:ssl_ca_file]
  end

  http
end
create_post_request(path, headers, body) click to toggle source
# File lib/fountain/api/request_helper.rb, line 81
def create_post_request(path, headers, body)
  req = Net::HTTP::Post.new(path, headers)
  add_body(req, body) if body
  req
end
create_put_request(path, headers, body) click to toggle source
# File lib/fountain/api/request_helper.rb, line 87
def create_put_request(path, headers, body)
  req = Net::HTTP::Put.new(path, headers)
  add_body(req, body) if body
  req
end
create_request(path, options) click to toggle source
# File lib/fountain/api/request_helper.rb, line 69
def create_request(path, options)
  headers = get_headers(options)
  body = options[:body]

  case options[:method]
  when :post then create_post_request path, headers, body
  when :put then create_put_request path, headers, body
  when :delete then create_delete_request path, headers
  else create_get_request path, headers, body
  end
end
get_headers(options) click to toggle source
# File lib/fountain/api/request_helper.rb, line 102
def get_headers(options)
  headers = options[:headers]
  headers ||= {}
  raise Fountain::MissingApiKeyError if Fountain.api_token.nil?

  headers['X-ACCESS-TOKEN'] = Fountain.api_token
  headers
end
parse_response(response) click to toggle source
# File lib/fountain/api/request_helper.rb, line 50
def parse_response(response)
  JSON.parse(response)
rescue JSON::ParserError
  raise Fountain::JsonParseError, "Fountain response parse error: #{response}"
end