Class: Airvend

Inherits:
Object
  • Object
show all
Defined in:
lib/airvend.rb,
lib/airvend/version.rb

Constant Summary collapse

VERSION =
"0.1.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username = nil, password = nil, api_key = nil, production = false) ⇒ Airvend

Returns a new instance of Airvend.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/airvend.rb', line 21

def initialize(username=nil, password=nil, api_key=nil, production=false)

  @username = username
  @password = password
  @api_key = api_key
  @production = production
  sandbox_url = BASE_ENDPOINTS::SANDBOX_URL
  live_url = BASE_ENDPOINTS::LIVE_URL

  if (ENV['RAILS_ENV'].nil?)
    @production = production
    warn "Warning: Make sure you set RAILS_ENV to production or development"
  else
    if ENV['RAILS_ENV'] == "production"
      @production=true
    else
      @prodcution=false
    end
  end

  # set rave url to sandbox or live if we are in production or development
  if production == false
      @url = sandbox_url
  else
      @url = live_url
  end

  # check if we set our public and secret keys to the environment variable
  if (username.nil? && password.nil?)
    @username = ENV['AIRVEND_USERNAME']
    @password = ENV['AIRVEND_PASSWORD']
  else
    @username = username
    @password = password
    warn "Warning: To ensure your account credentials are safe, It is best to always set your password in the environment variable with AIRVEND_USERNAME & AIRVEND_PASSWORD"
  end

  # raise this error if no username is passed
  unless !@username.nil?
    raise AirvendBadUserError, "No Username supplied and couldn't find any in environment variables. Make sure to set public key as an environment variable AIRVEND_USERNAME"
  end

  # raise this error if no password is passed
  unless !@password.nil?
    raise AirvendBadPassError, "No password supplied and couldn't find any in environment variables. Make sure to set secret key as an environment variable AIRVEND_PASSWORD"
  end


  if (api_key.nil?)
    @api_key = ENV['AIRVEND_API_KEY']
  else
    @api_key = api_key
    warn "Warning: To ensure your account key is safe, It is best to always set your password in the environment variable with AIRVEND_API_KEY"
  end

  # raise this error if no username is passed
  unless !@api_key.nil?
    raise AirvendBadKeyError, "No Api Key supplied and couldn't find any in environment variables. Make sure to set public key as an environment variable AIRVEND_USERNAME"
  end
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



19
20
21
# File 'lib/airvend.rb', line 19

def api_key
  @api_key
end

#passwordObject

Returns the value of attribute password.



19
20
21
# File 'lib/airvend.rb', line 19

def password
  @password
end

#productionObject

Returns the value of attribute production.



19
20
21
# File 'lib/airvend.rb', line 19

def production
  @production
end

#urlObject

Returns the value of attribute url.



19
20
21
# File 'lib/airvend.rb', line 19

def url
  @url
end

#usernameObject

Returns the value of attribute username.



19
20
21
# File 'lib/airvend.rb', line 19

def username
  @username
end

Instance Method Details

#base_urlObject

method to return the base url



83
84
85
# File 'lib/airvend.rb', line 83

def base_url
  return url
end

#hash_req(details) ⇒ Object



97
98
99
100
101
# File 'lib/airvend.rb', line 97

def hash_req(details)
  api_hash = details.to_json+self.api_key
  api_hash = Digest::SHA512.hexdigest api_hash
  return api_hash
end

#headers(hashkey) ⇒ Object



87
88
89
90
91
92
93
94
95
# File 'lib/airvend.rb', line 87

def headers(hashkey)
  return {
    UpperCaseString.new('Content-Type') => 'application/json',
    UpperCaseString.new('username') => self.username,
    UpperCaseString.new('password') => self.password,
    UpperCaseString.new('hash') => hashkey,
    user_agent: "Airvend-0.1.0"
  }
end