class Wing::Client

Constants

API_VERSION
BASE_URL
USER_AGENT

Attributes

api_key[R]
api_secret[R]

Public Class Methods

new(api_key: nil, api_secret: nil) click to toggle source
# File lib/wing/client.rb, line 14
def initialize(api_key: nil, api_secret: nil)
  @api_key = api_key || Wing::configuration.api_key
  @api_secret = api_secret || Wing::configuration.api_secret
end

Public Instance Methods

base_url() click to toggle source
# File lib/wing/client.rb, line 19
def base_url
  "#{BASE_URL}#{API_VERSION}"
end
get(path, options = {}) click to toggle source
# File lib/wing/client.rb, line 23
def get(path, options = {})
  execute(:get, path, options)
end
post(path, data = {}, options = {}) click to toggle source
# File lib/wing/client.rb, line 27
def post(path, data = {}, options = {})
  execute(:post, path, options.merge(body: data))
end

Private Instance Methods

base_options() click to toggle source
# File lib/wing/client.rb, line 64
def base_options
  {
    format: :json,
    headers: {
      "Accept" => "application/json",
      "User-Agent" => USER_AGENT
    },
    basic_auth: {
      username: api_key,
      password: api_secret
    }
  }
end
execute(method, path, options) click to toggle source
# File lib/wing/client.rb, line 35
def execute(method, path, options)
  begin
    response = request(method, path, options)
  rescue *Error::NET_HTTP_ERRORS => err
    raise ConnectionError.new, err.message
  end

  case response.code
  when 200..299
    response
  when 400
    raise BadRequestError.new(response['errorDescription'])
  when 401
    raise AuthenticationError.new(response['errorDescription'])
  when 404
    raise NotFoundError.new(response['errorDescription'])
  when 400..499
    raise ResponseError.new(response['errorDescription'])
  when 500
    raise InternalServerError.new(response['errorDescription'])
  when 500..599
    raise ServerError.new(response['errorDescription'])
  end
end
request(method, path, options) click to toggle source
# File lib/wing/client.rb, line 60
def request(method, path, options)
  HTTParty.send(method, base_url + path, base_options.merge(options))
end