class StatusCake::Client

Constants

APIs
DEFAULT_ADAPTERS
ENDPOINT
OPTIONS
USER_AGENT

Public Class Methods

new(options) { |faraday| ... } click to toggle source
# File lib/statuscake/client.rb, line 32
def initialize(options)
  @options = {}

  OPTIONS.each do |key|
    @options[key] = options.delete(key)
  end

  options[:url] ||= ENDPOINT

  @conn = Faraday.new(options) do |faraday|
    faraday.request  :url_encoded
    faraday.response :json, :content_type => /\bjson$/
    faraday.response :raise_error

    yield(faraday) if block_given?

    unless DEFAULT_ADAPTERS.any? {|i| faraday.builder.handlers.include?(i) }
      faraday.adapter Faraday.default_adapter
    end
  end

  @conn.headers[:user_agent] = USER_AGENT
end

Private Instance Methods

request(path, method, params = {}) { |req| ... } click to toggle source
# File lib/statuscake/client.rb, line 73
def request(path, method, params = {})
  response = @conn.send(method) do |req|
    req.url path

    case method
    when :get, :delete
      req.params = params
    when :post, :put
      req.body = params
    else
      raise 'must not happen'
    end

    req.headers[:API] = @options[:API]
    req.headers[:Username] = @options[:Username]
    yield(req) if block_given?
  end

  json = response.body
  validate_response(json)
  json
end
validate_response(json) click to toggle source
# File lib/statuscake/client.rb, line 96
def validate_response(json)
  if json.kind_of?(Hash) and json.has_key?('ErrNo')
    raise StatusCake::Error.new(json)
  end
end