Class: Math::API

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/math-api.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

VERSION =
"0.0.3"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = { }) ⇒ API

Returns a new instance of API.



15
16
17
18
19
20
21
22
23
# File 'lib/math-api.rb', line 15

def initialize ( opts = { } )

  self.class.base_uri    opts[:math_url] || 'http://mathematics.io'
  self.accesskey      =  opts[:accesskey]
  self.user_id        =  opts[:user_id]

  self.authenticate

end

Instance Attribute Details

#accesskeyObject

Returns the value of attribute accesskey.



12
13
14
# File 'lib/math-api.rb', line 12

def accesskey
  @accesskey
end

#base_urlObject

Returns the value of attribute base_url.



12
13
14
# File 'lib/math-api.rb', line 12

def base_url
  @base_url
end

#user_idObject

Returns the value of attribute user_id.



12
13
14
# File 'lib/math-api.rb', line 12

def user_id
  @user_id
end

Instance Method Details

#authenticateObject

Raises:



26
27
28
29
30
31
32
# File 'lib/math-api.rb', line 26

def authenticate

  resp = self.class.get('/api/1/users/profile.json', query: { accesskey: @accesskey })

  raise Error.new("Unauthorized") unless resp.code == 200

end

#create_records(data) ⇒ Object

Raises:

  • (ArgumentError)


35
36
37
38
39
40
41
42
43
44
45
# File 'lib/math-api.rb', line 35

def create_records (data)

  raise ArgumentError, "Must specify an item_name" if data[:item_name].nil?
  raise ArgumentError, "Must specify an amount" if data[:amount].nil?


  data = data.merge({ accesskey: @accesskey })

  self.class.post("/api/1/users/#{@user_id.to_s}/records.json", body: data )

end