class Synthetics::Client

This class wraps Excon to make requests to the Synthetics API.

Public Class Methods

new(api_key) click to toggle source
# File lib/synthetics/client.rb, line 6
def initialize(api_key)
  @api_key = api_key.freeze
end

Public Instance Methods

request(options) click to toggle source
# File lib/synthetics/client.rb, line 10
def request(options)
  wrap_errors do
    normailze_request_options!(options)
    response = excon.request(options)
    return nil if response.body.empty?
    deep_snakeify_keys(JSON.parse(response.body))
  end
end

Private Instance Methods

excon() click to toggle source
# File lib/synthetics/client.rb, line 44
def excon
  @excon ||= Excon.new(API_HOST)
end
normailze_request_options!(opts) click to toggle source
# File lib/synthetics/client.rb, line 31
def normailze_request_options!(opts)
  opts[:path] = API_PATH_PREFIX + opts[:path]
  opts[:headers] ||= {}
  opts[:headers].merge!(
    'Content-Type' => 'application/json',
    'X-API-Key' => @api_key
  )
  if opts.key?(:body) && !opts[:body].is_a?(String)
    opts[:body] = deep_camelize_keys(opts[:body]).to_json
  end
  opts[:expects] = 200..204
end
wrap_errors() { || ... } click to toggle source
# File lib/synthetics/client.rb, line 21
def wrap_errors
  yield
rescue JSON::JSONError => ex
  raise ParseError, ex
rescue Excon::Errors::ClientError => ex
  raise ClientError, ex
rescue Excon::Errors::ServerError => ex
  raise ServerError, ex
end