Class: Math::API
Defined Under Namespace
Classes: Error
Constant Summary collapse
- VERSION =
"0.0.3"
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
-
#initialize(opts = { }) ⇒ API
constructor
A new instance of API.
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
#accesskey ⇒ Object
Returns the value of attribute accesskey.
12 13 14 |
# File 'lib/math-api.rb', line 12 def accesskey @accesskey end |
#base_url ⇒ Object
Returns the value of attribute base_url.
12 13 14 |
# File 'lib/math-api.rb', line 12 def base_url @base_url end |
#user_id ⇒ Object
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
#authenticate ⇒ Object
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
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 |