class Reliquary::Client

Constants

HTTP_METHODS

Attributes

api_base[R]

@!attribute [r] api_base

@return [URI] the base URI on which additional REST calls will be built
api_key[R]

@!attribute [r] api_key

@return [String] a [New Relic REST API](https://rpm.newrelic.com/api/explore) key

Public Class Methods

new(api_key = ENV['NEWRELIC_API_KEY']) click to toggle source

@!method initialize(api_key = ENV)

Constructor method
@param api_key [String] (see api_key)
@return [Reliquary::Client] the initialized client
# File lib/reliquary/client.rb, line 28
def initialize(api_key = ENV['NEWRELIC_API_KEY'])
  begin
    @api_key = validate_api_key(api_key)
    @api_base = build_api_base('https://api.newrelic.com/v2/')

  rescue NoMethodError => e
    false

  rescue StandardError => e
    raise e
  end
end

Public Instance Methods

method_missing(method_name, *args, &block) click to toggle source

@!method method_missing(method_name, *args, &block)

Delegate HTTP method calls to RestClient::Resource

@param method_name [Symbol] name of method (must be a member of
  {Reliquary::Client::HTTP_METHODS})
@param args [Array] additional method params
@param block [Proc] block to which method will yield
# File lib/reliquary/client.rb, line 63
def method_missing(method_name, *args, &block)
  begin
    self.api_base.send(method_name.to_sym, *args, &block)

  rescue StandardError => e
    raise e
  end
end
parse(json) click to toggle source

@!method parse(json)

Parse returned JSON into a Ruby object

@param [String] json JSON-formatted string
@return [Object] Ruby object representing JSON-formatted string
# File lib/reliquary/client.rb, line 46
def parse(json)
  begin
    # strip off some layers of nonsense added by Oj
    MultiJson.load(json, :symbolize_keys => true).values[0]

  rescue StandardError => e
    raise e
  end
end
respond_to_missing?(method_name, include_private = false) click to toggle source
Calls superclass method
# File lib/reliquary/client.rb, line 72
def respond_to_missing?(method_name, include_private = false)
  HTTP_METHODS.include?(method_name.to_s) || super
end

Private Instance Methods

auth_header(api_key = self.api_key) click to toggle source
# File lib/reliquary/client.rb, line 106
def auth_header(api_key = self.api_key)
  begin
    {:x_api_key => api_key}

  rescue StandardError => e
    raise e
  end
end
build_api_base(uri) click to toggle source
# File lib/reliquary/client.rb, line 78
def build_api_base(uri)
  begin
    validated_uri = URI(uri)

    RestClient::Resource.new(validated_uri.to_s, :headers => auth_header)

  rescue URI::InvalidURIError => e
    raise "'#{uri}' does not look like a valid URI"

  rescue StandardError => e
    raise e
  end
end
validate_api_key(api_key) click to toggle source
# File lib/reliquary/client.rb, line 93
def validate_api_key(api_key)
  begin
    if /^[\h]{47}$/ =~ api_key
      api_key
    else
      raise "'#{api_key}' does not look like a valid New Relic REST API key"
    end

  rescue StandardError => e
    raise e
  end
end