Class: CrateAPI::Base

Inherits:
Object
  • Object
show all
Includes:
HTTMultiParty
Defined in:
lib/crate_api/base.rb

Overview

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

Constant Summary collapse

API_VERSION =

Current API Version.

1
BASE_URL =

Base URL Endpoint

"https://api.letscrate.com/#{API_VERSION}"
AUTH_URL =

Authorization URL Endpoint.

"#{BASE_URL}/users/authenticate.json"
ITEMS_URL =

Items URL Endpoint.

"#{BASE_URL}/files"
CRATES_URL =

Crates URL Endpoint.

"#{BASE_URL}/crates"
SHORT_URL =

Short URL Placeholder String.

"http://lts.cr/%s"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username, password) ⇒ CrateAPI::Base

Default initializer for the CrateAPI Client

Parameters:

  • username (String)

    username for the Crate user.

  • password (String)

    password for the Crate user.

Raises:

  • (NotValidUserError)

    this will occur if the username/password pair is not valid.



41
42
43
44
# 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

Instance Attribute Details

#authObject



13
14
15
# File 'lib/crate_api/base.rb', line 13

def auth
  @auth
end

Class Method Details

.authorized?(user, pass) ⇒ Boolean

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

Parameters:

  • username (String)

    Username to test for authorization.

  • pass (String)

    Password to test for authorization.

Returns:

  • (Boolean)

    whether or not the user is authorized.



72
73
74
75
76
77
78
# 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 = {}) ⇒ Object

Class method that return the response body of the call.

Parameters:

  • url (String)

    URL endpoint to make the call to.

  • verb (Symbol)

    HTTP verb used for call.

  • params (Optional) (defaults to: {})

    Hash of params used for call.

Returns:

  • (Object)

    body object from the response.



53
54
55
56
57
58
59
60
61
62
63
64
65
# 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

Instance Method Details

#cratesCrateAPI::Crates

Default initializer for getting a Crates instance.

Returns:



29
# File 'lib/crate_api/base.rb', line 29

def crates(); @crates || CrateAPI::Crates.new(); end

#itemsCrateAPI::Items

Default initializer for getting a Items instance.

Returns:



33
# File 'lib/crate_api/base.rb', line 33

def items(); @items || CrateAPI::Items.new(); end