Class: Math::API
Defined Under Namespace
Classes: Error
Constant Summary collapse
- VERSION =
"0.0.4"
Instance Attribute Summary collapse
-
#accesskey ⇒ Object
Returns the value of attribute accesskey.
-
#base_url ⇒ Object
Returns the value of attribute base_url.
-
#user_id ⇒ Object
Returns the value of attribute user_id.
Instance Method Summary collapse
- #authenticate ⇒ Object
- #create_records(data) ⇒ Object
- #get_records(args) ⇒ Object
-
#initialize(opts = { }) ⇒ API
constructor
A new instance of API.
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
#accesskey ⇒ Object
Returns the value of attribute accesskey.
11 12 13 |
# File 'lib/math-api.rb', line 11 def accesskey @accesskey end |
#base_url ⇒ Object
Returns the value of attribute base_url.
11 12 13 |
# File 'lib/math-api.rb', line 11 def base_url @base_url end |
#user_id ⇒ Object
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
#authenticate ⇒ Object
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
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
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? = { } item_id = args[:item_id] [:page] = args[:page] unless args[:page].nil? [: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: ) end |