class AlertLogic::Client

JSON parsing HTTP client with some helper methods

Constants

DEFAULT_ENDPOINT

Attributes

endpoint[R]
secret_key[R]

Public Class Methods

new(secret_key = nil, endpoint = nil) click to toggle source
# File lib/alert_logic/client/base_client.rb, line 11
def initialize(secret_key = nil, endpoint = nil)
  @secret_key = secret_key  || AlertLogic.secret_key
  @endpoint   = endpoint    || DEFAULT_ENDPOINT
  @logger     = AlertLogic.logger
  init
end

Public Instance Methods

endpoint=(endpoint) click to toggle source
# File lib/alert_logic/client/base_client.rb, line 24
def endpoint=(endpoint)
  @endpoint = endpoint
  reload!
  @endpoint
end
reload!() click to toggle source
# File lib/alert_logic/client/base_client.rb, line 30
def reload!
  @connection = nil
  init
end
secret_key=(secret_key) click to toggle source
# File lib/alert_logic/client/base_client.rb, line 18
def secret_key=(secret_key)
  @secret_key = secret_key
  reload!
  @secret_key
end

Private Instance Methods

flatten(type, resources) click to toggle source
# File lib/alert_logic/client/base_client.rb, line 89
def flatten(type, resources)
  resources.count >= 1 ? resources.map! { |item| item[type] } : []
end
init() click to toggle source
# File lib/alert_logic/client/base_client.rb, line 37
def init
  verify_key
  options = { :url => @endpoint,
              :ssl => { :verify => false }
            }
  headers = { 'Accept'      => 'application/json',
              'User-Agent'  => "alert_logic gem v#{VERSION}"
            }
  @connection = Faraday.new(options) do |con|
    con.use         Faraday::Response::Logger,  @logger
    con.use         Faraday::Response::RaiseError
    con.adapter     Faraday.default_adapter
    con.headers     = headers
    con.basic_auth  @secret_key, ''
  end
end
normalize_response(resource_type, js) click to toggle source
# File lib/alert_logic/client/base_client.rb, line 72
def normalize_response(resource_type, js)
  if js.respond_to?(:key?)
    if js.key?(pluralize(resource_type))
      flatten(resource_type, js[pluralize(resource_type)])
    elsif js.key?(resource_type)
      [js[resource_type]]
    else
      js.first
    end
  elsif js.respond_to?(:empty?) && !js.empty?
    js
  else
    @logger.info "No #{pluralize(resource_type)} found.."
    []
  end
end
parse_response_for(resource_type) { |api_call| ... } click to toggle source
# File lib/alert_logic/client/base_client.rb, line 62
def parse_response_for(resource_type, &api_call)
  res = yield(api_call)
  ClientResponse.new(
    res.status,
    normalize_response(resource_type, JSON.parse(res.body))
  )
rescue => e
  raise ClientError, e.message
end
verify_key() click to toggle source
# File lib/alert_logic/client/base_client.rb, line 54
def verify_key
  unless @secret_key =~ /^[a-fA-F\d]{50}$/
    msg = "#{@secret_key.inspect} is invalid. "
    msg << 'You must supply a valid 50 character secret_key!'
    fail InvalidKey, msg
  end
end