greengoose 0.0.1.pre1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/README.md +22 -0
- data/Rakefile +5 -0
- data/greengoose.gemspec +28 -0
- data/lib/greengoose.rb +8 -0
- data/lib/greengoose/client.rb +47 -0
- data/lib/greengoose/credentials.rb +1 -0
- data/lib/greengoose/credentials/basic.rb +16 -0
- data/lib/greengoose/middleware.rb +1 -0
- data/lib/greengoose/middleware/json.rb +32 -0
- data/lib/greengoose/version.rb +3 -0
- data/spec/greengoose/client_spec.rb +25 -0
- data/spec/greengoose/credentials/basic_spec.rb +20 -0
- data/spec/greengoose/middleware/json_spec.rb +45 -0
- data/spec/spec_helper.rb +14 -0
- metadata +146 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# GreenGoose
|
2
|
+
|
3
|
+
Ruby wrapper for the [GreenGoose](http://www.greengoose.com) [API](http://apiwiki.greengoose.com/index.php/REST_API).
|
4
|
+
|
5
|
+
**WIP:** This gem is still a work in progress. It's not fully functional
|
6
|
+
yet.
|
7
|
+
|
8
|
+
## Quick Start
|
9
|
+
|
10
|
+
If using Bundler (recommended), add to Gemfile:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem 'greengoose'
|
14
|
+
```
|
15
|
+
|
16
|
+
Create a client object and authenticate with a username and password
|
17
|
+
([signup](http://www.greengoose.com/signup))
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
client = Greengoose::Client.new
|
21
|
+
client.authenticate "username", "password"
|
22
|
+
```
|
data/Rakefile
ADDED
data/greengoose.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "greengoose/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "greengoose"
|
7
|
+
s.version = GreenGoose::VERSION
|
8
|
+
s.authors = ["Andy Lindeman"]
|
9
|
+
s.email = ["alindeman@gmail.com"]
|
10
|
+
s.homepage = "https://github.com/alindeman/greengoose"
|
11
|
+
s.summary = %q{Provides a Ruby interface to the GreenGoose REST API}
|
12
|
+
s.description = %q{The GreenGoose API enables developers to build fun applications that interact with the GreenGoose real-world sensor platform
|
13
|
+
}
|
14
|
+
|
15
|
+
s.files = `git ls-files`.split("\n")
|
16
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
17
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
18
|
+
s.require_paths = ["lib"]
|
19
|
+
|
20
|
+
s.add_development_dependency "rspec", "~>2.7.0"
|
21
|
+
s.add_development_dependency "mocha", "~>0.10.0"
|
22
|
+
s.add_development_dependency "webmock", "~>1.7.8"
|
23
|
+
s.add_development_dependency "pry"
|
24
|
+
s.add_development_dependency "rake"
|
25
|
+
|
26
|
+
s.add_runtime_dependency "faraday", "~>0.7"
|
27
|
+
s.add_runtime_dependency "multi_json", "~>1.0"
|
28
|
+
end
|
data/lib/greengoose.rb
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
require "faraday"
|
2
|
+
require "multi_json"
|
3
|
+
|
4
|
+
require File.expand_path("greengoose/version", File.dirname(__FILE__))
|
5
|
+
|
6
|
+
require File.expand_path("greengoose/client", File.dirname(__FILE__))
|
7
|
+
require File.expand_path("greengoose/credentials", File.dirname(__FILE__))
|
8
|
+
require File.expand_path("greengoose/middleware", File.dirname(__FILE__))
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module GreenGoose
|
2
|
+
# Green Goose API client
|
3
|
+
class Client
|
4
|
+
extend Forwardable
|
5
|
+
|
6
|
+
# Defines get, post, etc...
|
7
|
+
def_delegators :connection, *Faraday::Connection::METHODS
|
8
|
+
|
9
|
+
attr_reader :credentials
|
10
|
+
|
11
|
+
API_URL = "http://api.greengoose.com"
|
12
|
+
API_VERSION = 1
|
13
|
+
|
14
|
+
# Creates a new API client
|
15
|
+
def initialize
|
16
|
+
end
|
17
|
+
|
18
|
+
# Authenticates against the API using a username and password
|
19
|
+
#
|
20
|
+
# @param [String] username API Username
|
21
|
+
# @param [String] password API Password
|
22
|
+
def authenticate(username, password)
|
23
|
+
@credentials = Credentials::Basic.new(username, password)
|
24
|
+
@credentials.configure_connection(connection)
|
25
|
+
|
26
|
+
self
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def connection
|
32
|
+
@connection ||= Faraday.new api_url,
|
33
|
+
:headers => { "User-Agent" => user_agent } do |builder|
|
34
|
+
builder.use Middleware::JSON
|
35
|
+
builder.adapter ::Faraday.default_adapter
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def api_url
|
40
|
+
"#{API_URL}/v#{API_VERSION}"
|
41
|
+
end
|
42
|
+
|
43
|
+
def user_agent
|
44
|
+
"Ruby:GreenGoose:#{VERSION}"
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require File.expand_path("credentials/basic", File.dirname(__FILE__))
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module GreenGoose
|
2
|
+
module Credentials
|
3
|
+
class Basic
|
4
|
+
attr_reader :username, :password
|
5
|
+
|
6
|
+
def initialize(username, password)
|
7
|
+
@username = username
|
8
|
+
@password = password
|
9
|
+
end
|
10
|
+
|
11
|
+
def configure_connection(connection)
|
12
|
+
connection.basic_auth(username, password)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require File.expand_path("middleware/json", File.dirname(__FILE__))
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module GreenGoose
|
2
|
+
module Middleware
|
3
|
+
class JSON < ::Faraday::Request::UrlEncoded
|
4
|
+
self.mime_type = "application/json".freeze
|
5
|
+
|
6
|
+
dependency 'multi_json'
|
7
|
+
|
8
|
+
def call(env)
|
9
|
+
match_content_type(env) do |data|
|
10
|
+
env[:body] = ::MultiJson.encode(data)
|
11
|
+
end
|
12
|
+
|
13
|
+
@app.call(env).on_complete do
|
14
|
+
env[:body] = parse(env[:body])
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def parse(body)
|
19
|
+
case body
|
20
|
+
when ''
|
21
|
+
nil
|
22
|
+
when 'true'
|
23
|
+
true
|
24
|
+
when 'false'
|
25
|
+
false
|
26
|
+
else
|
27
|
+
::MultiJson.decode(body)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe GreenGoose::Client do
|
4
|
+
it "sets the User Agent as requested by GreenGoose" do
|
5
|
+
# http://apiwiki.greengoose.com/index.php/User_Agents
|
6
|
+
req = stub_request(:get, "http://api.greengoose.com").with do |req|
|
7
|
+
req.headers["User-Agent"] =~ /Ruby:GreenGoose:#{GreenGoose::VERSION}/
|
8
|
+
end
|
9
|
+
|
10
|
+
res = subject.get '/'
|
11
|
+
end
|
12
|
+
|
13
|
+
describe "authentication" do
|
14
|
+
context "username and password" do
|
15
|
+
before { subject.authenticate("andy", "1234") }
|
16
|
+
|
17
|
+
it "authenticates against the API with the provided credentials" do
|
18
|
+
req = stub_request(:get, "http://andy:1234@api.greengoose.com/")
|
19
|
+
|
20
|
+
res = subject.get '/'
|
21
|
+
req.should have_been_requested
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe GreenGoose::Credentials::Basic do
|
4
|
+
it "accepts and stores a username and password" do
|
5
|
+
credentials = described_class.new("andy", "1234")
|
6
|
+
|
7
|
+
credentials.username.should == "andy"
|
8
|
+
credentials.password.should == "1234"
|
9
|
+
end
|
10
|
+
|
11
|
+
describe "#configure_connection" do
|
12
|
+
it "configures the Faraday connection to use the credentials" do
|
13
|
+
connection = stub
|
14
|
+
connection.expects(:basic_auth).with("andy", "1234")
|
15
|
+
|
16
|
+
credentials = described_class.new("andy", "1234")
|
17
|
+
credentials.configure_connection(connection)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe GreenGoose::Middleware::JSON do
|
4
|
+
let(:connection) {
|
5
|
+
Faraday.new("http://api.greengoose.com") do |builder|
|
6
|
+
builder.use described_class
|
7
|
+
builder.adapter Faraday.default_adapter
|
8
|
+
end
|
9
|
+
}
|
10
|
+
|
11
|
+
describe "request" do
|
12
|
+
it "sends the content type as application/json" do
|
13
|
+
req = stub_request(:post, "api.greengoose.com").with do |req|
|
14
|
+
req.headers["Content-Type"] == "application/json"
|
15
|
+
end
|
16
|
+
|
17
|
+
res = connection.post "/" do |req|
|
18
|
+
req.body = { }
|
19
|
+
end
|
20
|
+
|
21
|
+
req.should have_been_requested
|
22
|
+
end
|
23
|
+
|
24
|
+
it "sends the request body encoded as JSON" do
|
25
|
+
req = stub_request(:post, "api.greengoose.com").
|
26
|
+
with(:body => "{\"foo\":\"bar\"}")
|
27
|
+
|
28
|
+
res = connection.post "/" do |req|
|
29
|
+
req.body = { :foo => "bar" }
|
30
|
+
end
|
31
|
+
|
32
|
+
req.should have_been_requested
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
describe "response" do
|
37
|
+
it "parses the body as JSON" do
|
38
|
+
req = stub_request(:get, "api.greengoose.com").
|
39
|
+
to_return(:body => "{\"foo\":\"bar\"}")
|
40
|
+
|
41
|
+
res = connection.get "/"
|
42
|
+
res.body.should == { "foo" => "bar" }
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require File.expand_path("../lib/greengoose", File.dirname(__FILE__))
|
2
|
+
|
3
|
+
require "mocha"
|
4
|
+
require "webmock/rspec"
|
5
|
+
require "pry"
|
6
|
+
|
7
|
+
RSpec.configure do |c|
|
8
|
+
c.mock_with :mocha
|
9
|
+
|
10
|
+
c.before(:each) do
|
11
|
+
WebMock.enable!
|
12
|
+
WebMock.disable_net_connect!
|
13
|
+
end
|
14
|
+
end
|
metadata
ADDED
@@ -0,0 +1,146 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: greengoose
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1.pre1
|
5
|
+
prerelease: 6
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Andy Lindeman
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-12-31 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: &70322071249240 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 2.7.0
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70322071249240
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: mocha
|
27
|
+
requirement: &70322071208960 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ~>
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 0.10.0
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70322071208960
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: webmock
|
38
|
+
requirement: &70322071208400 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ~>
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 1.7.8
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *70322071208400
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: pry
|
49
|
+
requirement: &70322071207600 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *70322071207600
|
58
|
+
- !ruby/object:Gem::Dependency
|
59
|
+
name: rake
|
60
|
+
requirement: &70322071206760 !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
type: :development
|
67
|
+
prerelease: false
|
68
|
+
version_requirements: *70322071206760
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: faraday
|
71
|
+
requirement: &70322071205600 !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ~>
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0.7'
|
77
|
+
type: :runtime
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: *70322071205600
|
80
|
+
- !ruby/object:Gem::Dependency
|
81
|
+
name: multi_json
|
82
|
+
requirement: &70322071204560 !ruby/object:Gem::Requirement
|
83
|
+
none: false
|
84
|
+
requirements:
|
85
|
+
- - ~>
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '1.0'
|
88
|
+
type: :runtime
|
89
|
+
prerelease: false
|
90
|
+
version_requirements: *70322071204560
|
91
|
+
description: ! "The GreenGoose API enables developers to build fun applications that
|
92
|
+
interact with the GreenGoose real-world sensor platform\n "
|
93
|
+
email:
|
94
|
+
- alindeman@gmail.com
|
95
|
+
executables: []
|
96
|
+
extensions: []
|
97
|
+
extra_rdoc_files: []
|
98
|
+
files:
|
99
|
+
- .gitignore
|
100
|
+
- Gemfile
|
101
|
+
- README.md
|
102
|
+
- Rakefile
|
103
|
+
- greengoose.gemspec
|
104
|
+
- lib/greengoose.rb
|
105
|
+
- lib/greengoose/client.rb
|
106
|
+
- lib/greengoose/credentials.rb
|
107
|
+
- lib/greengoose/credentials/basic.rb
|
108
|
+
- lib/greengoose/middleware.rb
|
109
|
+
- lib/greengoose/middleware/json.rb
|
110
|
+
- lib/greengoose/version.rb
|
111
|
+
- spec/greengoose/client_spec.rb
|
112
|
+
- spec/greengoose/credentials/basic_spec.rb
|
113
|
+
- spec/greengoose/middleware/json_spec.rb
|
114
|
+
- spec/spec_helper.rb
|
115
|
+
homepage: https://github.com/alindeman/greengoose
|
116
|
+
licenses: []
|
117
|
+
post_install_message:
|
118
|
+
rdoc_options: []
|
119
|
+
require_paths:
|
120
|
+
- lib
|
121
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
122
|
+
none: false
|
123
|
+
requirements:
|
124
|
+
- - ! '>='
|
125
|
+
- !ruby/object:Gem::Version
|
126
|
+
version: '0'
|
127
|
+
segments:
|
128
|
+
- 0
|
129
|
+
hash: -1129379011405296523
|
130
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
131
|
+
none: false
|
132
|
+
requirements:
|
133
|
+
- - ! '>'
|
134
|
+
- !ruby/object:Gem::Version
|
135
|
+
version: 1.3.1
|
136
|
+
requirements: []
|
137
|
+
rubyforge_project:
|
138
|
+
rubygems_version: 1.8.10
|
139
|
+
signing_key:
|
140
|
+
specification_version: 3
|
141
|
+
summary: Provides a Ruby interface to the GreenGoose REST API
|
142
|
+
test_files:
|
143
|
+
- spec/greengoose/client_spec.rb
|
144
|
+
- spec/greengoose/credentials/basic_spec.rb
|
145
|
+
- spec/greengoose/middleware/json_spec.rb
|
146
|
+
- spec/spec_helper.rb
|