class OiApi::Client

Public Class Methods

new(options = {}) click to toggle source
# File lib/oi_api/client.rb, line 33
def initialize(options = {})
  # Use options passed in, but fall back to config module defaults
  merged_options = OiApi.options.merge(options)

  # Copy the merged values to this client and ignore those
  # not part of our configuration
  OiApi::Configuration::VALID_CONFIG_KEYS.each do |key|
    public_send("#{key}=", merged_options[key])
  end

  if username.nil? || password.nil?
    raise NoCredentialsError, 'you must provide a username and password'
  end

  # set base_uri and format via HTTParty class methods
  self.class.base_uri(api_endpoint)
  self.class.format(format)
end

Public Instance Methods

delete(url, options = {}) click to toggle source
# File lib/oi_api/client.rb, line 64
def delete(url, options = {})
  self.class.delete url, options_with_basic_auth(options)
end
get(url, options = {}) click to toggle source
# File lib/oi_api/client.rb, line 52
def get(url, options = {})
  self.class.get url, options_with_basic_auth(options)
end
post(url, options = {}) click to toggle source
# File lib/oi_api/client.rb, line 56
def post(url, options = {})
  self.class.post url, options_with_basic_auth(options)
end
put(url, options = {}) click to toggle source
# File lib/oi_api/client.rb, line 60
def put(url, options = {})
  self.class.put url, options_with_basic_auth(options)
end

Private Instance Methods

basic_auth_options() click to toggle source
# File lib/oi_api/client.rb, line 70
def basic_auth_options
  {
    :username => username,
    :password => password
  }
end
default_header_options() click to toggle source
# File lib/oi_api/client.rb, line 77
def default_header_options
  {
    'Content-Type' => content_type,
    'User-Agent' => user_agent
  }
end
options_with_basic_auth(options = {}) click to toggle source
# File lib/oi_api/client.rb, line 84
def options_with_basic_auth(options = {})
  headers = options.has_key?(:headers) ? options.delete(:headers) : {}
  query_string = options.has_key?(:query) ? options.delete(:query) : {}
  body = options

  _options = {
    basic_auth: basic_auth_options,
    headers: default_header_options.merge(headers),
  }

  _options[:debug_output] = debug_output if debug_output
  _options[:body] = body.to_json unless body.empty?
  _options[:query] = query_string unless query_string.empty?

  _options
end