class Velocity::Api::Base

Attributes

args[RW]

Public Class Methods

new(args) click to toggle source
# File lib/velocity/api/base.rb, line 17
def initialize(args)
  @args = args
end

Public Instance Methods

build_query() click to toggle source
# File lib/velocity/api/base.rb, line 46
def build_query
  args.map do |key, value|
    "filter[#{key.to_s}][contains]=#{value}"
  end.join("&")
end
options() click to toggle source
# File lib/velocity/api/base.rb, line 21
def options
  {
    headers: {
      "Authorization" => "Bearer #{Velocity.configuration.api_token}",
      "Content-Type" => "application/vnd.api+json",
    }
  }
end
parse_response(response) click to toggle source
# File lib/velocity/api/base.rb, line 30
def parse_response(response)
  case response.code
    when 200..201
      JSON.parse(response.body)["data"]
    when 400
      errors = JSON.parse(response.body)["errors"]
      raise BadRequestError.new(errors.first["title"] + ": " + errors.first["detail"])
    when 401
      raise UnauthorizedError.new("401 Unauthorized");
    when 404
      raise NotFoundError.new("404 not found error");
    else
      raise InternalServerError.new("#{response.code} something went wrong: #{response.body}")
  end
end