math-api 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem 'fakeweb'
6
+ gem 'rspec'
7
+ gem 'rake'
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Neil Pullman
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,34 @@
1
+ # Math::Api
2
+
3
+ API wrapper for Mathematics.io
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'math-api'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install math-api
18
+
19
+ ## Usage
20
+
21
+ @math = Math::API.new( accesskey: MATH_APIKEY, user_id: MATH_USERID, math_url: MATH_URL )
22
+ @math.create_records({ item_name: 'athing', amount: 25 })
23
+
24
+ `math_url` is optional and will default to `mathematics.io`.
25
+
26
+ `create_records` will also accept a `timestamp` argument. If none is specified, it will default to `Time.now`
27
+
28
+ ## Contributing
29
+
30
+ 1. Fork it
31
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
32
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
33
+ 4. Push to the branch (`git push origin my-new-feature`)
34
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rspec/core/rake_task'
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/lib/math-api.rb ADDED
@@ -0,0 +1,47 @@
1
+ require 'httparty'
2
+
3
+ module Math
4
+ class API
5
+
6
+ VERSION = "0.0.2"
7
+
8
+ include HTTParty
9
+ format :json
10
+
11
+ attr_accessor :base_url, :accesskey, :user_id
12
+
13
+
14
+ def initialize ( opts = { } )
15
+
16
+ self.class.base_uri opts[:math_url] || 'http://mathematics.io'
17
+ self.accesskey = opts[:accesskey]
18
+ self.user_id = opts[:user_id]
19
+
20
+ self.authenticate
21
+
22
+ end
23
+
24
+
25
+ def authenticate
26
+ resp = self.class.get('/api/1/users/profile.json', { accesskey: @accesskey })
27
+
28
+ raise Error.new("Unauthorized") unless resp.code == 200
29
+ end
30
+
31
+
32
+ def create_records (data)
33
+
34
+ raise ArgumentError, "Must specify an item_name" if data[:item_name].nil?
35
+ raise ArgumentError, "Must specify an amount" if data[:amount].nil?
36
+
37
+
38
+ data = data.merge({ accesskey: @accesskey })
39
+
40
+ self.class.post("/api/1/users/#{@user_id.to_s}/records.json", body: data )
41
+
42
+ end
43
+
44
+ end
45
+
46
+ class API::Error < ::StandardError; end
47
+ end
data/math-api.gemspec ADDED
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'math-api'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "math-api"
8
+ gem.version = Math::API::VERSION
9
+ gem.authors = ["Neil Pullman"]
10
+ gem.email = ["neil@descend.org"]
11
+ gem.description = %q{ API wrapper for Mathematics.io }
12
+ gem.summary = %q{ API wrapper for Mathematics.io }
13
+ gem.homepage = "http://github.com/neiltron/math-api"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+ end
@@ -0,0 +1,67 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), 'spec_helper'))
2
+
3
+ API_ROOT = 'http://mathurl.com'
4
+ ACCESSKEY = 123
5
+ USER_ID = 456
6
+
7
+ describe Math::API do
8
+
9
+ FakeWeb.register_uri(:get, "#{API_ROOT}/api/1/users/profile.json?accesskey=FALSEKEY", :status => ["401", "Unauthorized"])
10
+
11
+ it "throws an error if not authenticated" do
12
+
13
+ begin
14
+ Math::API.new( accesskey: 'FALSEKEY', user_id: 'FALSEUSERID', math_url: API_ROOT )
15
+ rescue Math::API::Error => e
16
+ true
17
+ end
18
+
19
+ false
20
+
21
+ end
22
+
23
+
24
+ describe "create_records" do
25
+
26
+ before(:all) do
27
+ FakeWeb.clean_registry
28
+
29
+ FakeWeb.register_uri(:get, "#{API_ROOT}/api/1/users/profile.json", :status => ["200"])
30
+ FakeWeb.register_uri(:post, "#{API_ROOT}/api/1/users/#{USER_ID}/records.json", :status => ["201"])
31
+
32
+ @api = Math::API.new( accesskey: ACCESSKEY, user_id: USER_ID, math_url: API_ROOT )
33
+ end
34
+
35
+
36
+ it "returns a response after creating records" do
37
+
38
+ resp = @api.create_records({ timestamp: "12/12/2012", item_name: "item", amount: "1" })
39
+
40
+ resp.code.should == 201
41
+
42
+ end
43
+
44
+
45
+ it 'throws an error if item_name is not specified' do
46
+
47
+ expect do
48
+
49
+ @api.create_records({ timestamp: "12/12/2012", amount: "1" })
50
+
51
+ end.to raise_error(ArgumentError)
52
+
53
+ end
54
+
55
+
56
+ it 'throws an error if amount is not specified' do
57
+
58
+ expect do
59
+
60
+ @api.create_records({ timestamp: "12/12/2012", item_name: "item" })
61
+
62
+ end.to raise_error(ArgumentError)
63
+
64
+ end
65
+
66
+ end
67
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1,2 @@
1
+ --colour
2
+ --backtrace
@@ -0,0 +1,4 @@
1
+ $:.push File.expand_path("../lib", __FILE__)
2
+ require 'math-api'
3
+
4
+ require 'fakeweb'
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: math-api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Neil Pullman
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-08-18 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: ! ' API wrapper for Mathematics.io '
15
+ email:
16
+ - neil@descend.org
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - LICENSE.txt
24
+ - README.md
25
+ - Rakefile
26
+ - lib/math-api.rb
27
+ - math-api.gemspec
28
+ - spec/mathapi_spec.rb
29
+ - spec/spec.opts
30
+ - spec/spec_helper.rb
31
+ homepage: http://github.com/neiltron/math-api
32
+ licenses: []
33
+ post_install_message:
34
+ rdoc_options: []
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ! '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ requirements: []
50
+ rubyforge_project:
51
+ rubygems_version: 1.8.10
52
+ signing_key:
53
+ specification_version: 3
54
+ summary: API wrapper for Mathematics.io
55
+ test_files:
56
+ - spec/mathapi_spec.rb
57
+ - spec/spec.opts
58
+ - spec/spec_helper.rb