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.4"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = { }) ⇒ API

Returns a new instance of API.



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

def initialize ( opts = { } )

  self.class.base_uri    opts[:math_url] || 'https://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.



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

def accesskey
  @accesskey
end

#base_urlObject

Returns the value of attribute base_url.



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

def base_url
  @base_url
end

#user_idObject

Returns the value of attribute user_id.



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

def user_id
  @user_id
end

Instance Method Details

#authenticateObject

Raises:



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

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)


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

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

#get_records(args) ⇒ Object

Raises:

  • (ArgumentError)


46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/math-api.rb', line 46

def get_records args

  raise ArgumentError if args[:item_id].nil?

  options             = { }
  item_id             = args[:item_id]
  options[:page]      = args[:page] unless args[:page].nil?
  options[:per_page]  = args[:per_page] unless args[:per_page].nil?

  self.class.get("/api/1/users/#{@user_id.to_s}/items/#{item_id.to_s}/records.json", query: options )

end