class CrateAPI::Base

Base class which is used to authenticate user and perform calls.

Constants

API_VERSION

Current API Version.

AUTH_URL

Authorization URL Endpoint.

BASE_URL

Base URL Endpoint

CRATES_URL

Crates URL Endpoint.

ITEMS_URL

Items URL Endpoint.

SHORT_URL

Short URL Placeholder String.

Attributes

auth[RW]

@attr [Hash] :auth auth hash.

Public Class Methods

authorized?(user, pass) click to toggle source

Class method that will return a boolean whether or not the user is authorized.

@param [String] username Username to test for authorization. @param [String] pass Password to test for authorization. @return [Boolean] whether or not the user is authorized.

# File lib/crate_api/base.rb, line 72
def self.authorized?(user, pass)
  resp = self.get("#{AUTH_URL}", {:basic_auth => {:username => user, :password => pass}})
  if resp.code == 401
    return false
  end
  return true
end
call(url, verb, params={}) click to toggle source

Class method that return the response body of the call.

@param [String] url URL endpoint to make the call to. @param [Symbol] verb HTTP verb used for call. @param [Optional] params Hash of params used for call. @return [Object] body object from the response.

# File lib/crate_api/base.rb, line 53
def self.call(url, verb, params={})
  params.merge!({:basic_auth => @@auth})
  resp = nil
  case verb
  when :get
    resp = self.get(url, params)
  when :post
    resp = self.post(url, params)
  end
  if resp.code == 200
    return resp.body
  end
end
new(username, password) click to toggle source

Default initializer for the CrateAPI Client

@param [String] username username for the Crate user. @param [String] password password for the Crate user. @return [CrateAPI::Base] this will return the base class instance. @raise [NotValidUserError] this will occur if the username/password pair is not valid.

# File lib/crate_api/base.rb, line 41
def initialize(username, password)
  raise NotValidUserError unless CrateAPI::Base.authorized?(username, password)
  @@auth = {:username => username, :password => password}
end

Public Instance Methods

crates() click to toggle source

Default initializer for getting a Crates instance. @return [CrateAPI::Crates] a new Crates instance.

# File lib/crate_api/base.rb, line 29
def crates(); @crates || CrateAPI::Crates.new(); end
items() click to toggle source

Default initializer for getting a Items instance. @return [CrateAPI::Items] a new Items instance.

# File lib/crate_api/base.rb, line 33
def items(); @items || CrateAPI::Items.new(); end