terracycle 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/AUTHORS +9 -0
- data/History.mkd +3 -0
- data/Licence.mkd +20 -0
- data/Manifest +15 -0
- data/Notes.txt +25 -0
- data/README.rdoc +135 -0
- data/Rakefile +39 -0
- data/VERSION.yml +4 -0
- data/add +0 -0
- data/lib/terracycle/base.rb +149 -0
- data/lib/terracycle/httpauth.rb +27 -0
- data/lib/terracycle/oauth.rb +34 -0
- data/lib/terracycle/request.rb +102 -0
- data/lib/terracycle.rb +64 -0
- data/terracycle.gemspec +29 -0
- metadata +92 -0
data/AUTHORS
ADDED
data/History.mkd
ADDED
data/Licence.mkd
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 TerraCycle Inc.
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Manifest
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
AUTHORS
|
2
|
+
History.mkd
|
3
|
+
Licence.mkd
|
4
|
+
Manifest
|
5
|
+
Notes.txt
|
6
|
+
README.rdoc
|
7
|
+
Rakefile
|
8
|
+
VERSION.yml
|
9
|
+
add
|
10
|
+
lib/terracycle.rb
|
11
|
+
lib/terracycle/base.rb
|
12
|
+
lib/terracycle/httpauth.rb
|
13
|
+
lib/terracycle/oauth.rb
|
14
|
+
lib/terracycle/request.rb
|
15
|
+
terracycle.gemspec
|
data/Notes.txt
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
*****************************************************************
|
2
|
+
* From Terracycle API Docs *
|
3
|
+
*****************************************************************
|
4
|
+
|
5
|
+
http://api.terracycle.net
|
6
|
+
|
7
|
+
200 OK: everything went awesome.
|
8
|
+
304 Not Modified: there was no new data to return.
|
9
|
+
400 Bad Request: your request is invalid, and we'll return an error message that tells you why. This is the status code returned if you've exceeded the rate limit (see below).
|
10
|
+
401 Not Authorized: either you need to provide authentication credentials, or the credentials provided aren't valid.
|
11
|
+
403 Forbidden: we understand your request, but are refusing to fulfill it. An accompanying error message should explain why.
|
12
|
+
404 Not Found: either you're requesting an invalid URI or the resource in question doesn't exist (ex: no such user).
|
13
|
+
500 Internal Server Error: we did something wrong. Please post to the group about it and the Terracycle team will investigate.
|
14
|
+
502 Bad Gateway: returned if Terracycle is down or being upgraded.
|
15
|
+
503 Service Unavailable: the Terracycle servers are up, but are overloaded with requests. Try again later.
|
16
|
+
|
17
|
+
**********
|
18
|
+
* Errors *
|
19
|
+
**********
|
20
|
+
|
21
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
22
|
+
<hash>
|
23
|
+
<request>/direct_messages/destroy/456.xml</request>
|
24
|
+
<error>No direct message with that ID found.</error>
|
25
|
+
</hash>
|
data/README.rdoc
ADDED
@@ -0,0 +1,135 @@
|
|
1
|
+
== The Terracycle Ruby Gem
|
2
|
+
|
3
|
+
A Ruby wrapper for the Terracycle REST APIs
|
4
|
+
|
5
|
+
== Installation
|
6
|
+
|
7
|
+
gem install terracycle
|
8
|
+
|
9
|
+
== What's new in 0.1.0?
|
10
|
+
|
11
|
+
This gem is the first version 0.1.0. Thanks to [contributions from Albert Mckeever
|
12
|
+
and Billy Catherall](http://github.net/terracycle/HISTORY.mkd).
|
13
|
+
This write creates the <tt>Terracycle::Client</tt> class.
|
14
|
+
|
15
|
+
* **-0.0.1**
|
16
|
+
Terracycle::Client.new.user("akmckeever").name
|
17
|
+
|
18
|
+
The error classes is consistent with [Terracycle's documented
|
19
|
+
response codes](http://api.terracycle.net/api_home/responses_errors). These changes should make it easier to
|
20
|
+
rescue from specific errors and take action accordingly.
|
21
|
+
|
22
|
+
<table>
|
23
|
+
<thead>
|
24
|
+
<tr>
|
25
|
+
<th>Response Code</th>
|
26
|
+
<th>-0.0.1</th>
|
27
|
+
</tr>
|
28
|
+
</thead>
|
29
|
+
<tbody>
|
30
|
+
<tr>
|
31
|
+
<td><tt>401</tt></td>
|
32
|
+
<td><tt>Terracycle::Unauthorized</tt></td>
|
33
|
+
<td><tt>Terracycle::Unauthorized</tt></td>
|
34
|
+
</tr>
|
35
|
+
<tr>
|
36
|
+
<td><tt>403</tt></td>
|
37
|
+
<td><tt>Terracycle::General</tt></td>
|
38
|
+
<td><tt>Terracycle::Forbidden</tt></td>
|
39
|
+
</tr>
|
40
|
+
<tr>
|
41
|
+
<td><tt>404</tt></td>
|
42
|
+
<td><tt>Terracycle::NotFound</tt></td>
|
43
|
+
<td><tt>Terracycle::NotFound</tt></td>
|
44
|
+
</tr>
|
45
|
+
<tr>
|
46
|
+
<td><tt>406</tt></td>
|
47
|
+
<td>N/A</td>
|
48
|
+
<td><tt>Terracycle::NotAcceptable</tt></td>
|
49
|
+
</tr>
|
50
|
+
<tr>
|
51
|
+
<td><tt>420</tt></td>
|
52
|
+
<td>N/A</td>
|
53
|
+
<td><tt>Terracycle::EnhanceYourCalm</tt></td>
|
54
|
+
</tr>
|
55
|
+
<tr>
|
56
|
+
<td><tt>500</tt></td>
|
57
|
+
<td><tt>Terracycle::InformTerracycle</tt></td>
|
58
|
+
<td><tt>Terracycle::InternalServerError</tt></td>
|
59
|
+
</tr>
|
60
|
+
<tr>
|
61
|
+
<td><tt>502</tt></td>
|
62
|
+
<td><tt>Terracycle::Unavailable</tt></td>
|
63
|
+
<td><tt>Terracycle::BadGateway</tt></td>
|
64
|
+
</tr>
|
65
|
+
<tr>
|
66
|
+
<td><tt>503</tt></td>
|
67
|
+
<td><tt>Terracycle::Unavailable</tt></td>
|
68
|
+
<td><tt>Terracycle::ServiceUnavailable</tt></td>
|
69
|
+
</tr>
|
70
|
+
</tbody>
|
71
|
+
</table>
|
72
|
+
|
73
|
+
The public APIs defined in version 0.0.1 of this gem will maintain backwards compatibility in
|
74
|
+
the next major version, following the best practice of [Semantic Versioning](http://semver.org/).
|
75
|
+
You are free to continue using the 0.1.0 series of the gem.
|
76
|
+
|
77
|
+
Here are a some aditional info:
|
78
|
+
|
79
|
+
* Ruby 1.8.6 compatibility: All code and specs now work in the latest version of Ruby!
|
80
|
+
* Support for HTTP proxies: Access Terracycle from UK, Canada, or inside your office firewall!
|
81
|
+
* Support for multiple HTTP adapters: NetHttp (default) and Typhoeus.
|
82
|
+
* Support for multiple request formats: JSON (default) or XML
|
83
|
+
* More flexible: Parse JSON or XML with the engine of your choosing.
|
84
|
+
* More RESTful: Uses HTTP DELETE (instead of POST) when requesting destructive resources
|
85
|
+
* SSL: On by default for increased [speed](http://gist.github.net/652330) and security!
|
86
|
+
|
87
|
+
examples of the gem's usage below.
|
88
|
+
|
89
|
+
Help! I'm getting: "Did not recognize your engine specification. Please specify either a symbol or a class. (RuntimeError)"
|
90
|
+
---------------------------------------------------------------------------------------------------------------------------
|
91
|
+
|
92
|
+
If you're using the JSON request format (i.e., the default), you'll need to
|
93
|
+
explicitly require a JSON library. We recommend [yajl-ruby](http://github.net/brianmario/yajl-ruby).
|
94
|
+
If you're using the XML request format, we recommend requiring [libxml-ruby](http://github.net/dvdplm/libxml-ruby) for dramatically improved performance over REXML.
|
95
|
+
|
96
|
+
== Documentation
|
97
|
+
|
98
|
+
Usage Examples
|
99
|
+
--------------
|
100
|
+
require "rubygems"
|
101
|
+
require "terracycle"
|
102
|
+
|
103
|
+
# Get a brigades info
|
104
|
+
puts Terracycle.brigade("6").info
|
105
|
+
|
106
|
+
# Certain methods require authentication. To get your Terracycle credentials,
|
107
|
+
# register an app at http://api.terracycle.net
|
108
|
+
Terracycle.configure do |config|
|
109
|
+
config.api_key = YOUR_API_KEY
|
110
|
+
end
|
111
|
+
|
112
|
+
# Initialize your Terracycle client
|
113
|
+
client = Terracycle::Client.new
|
114
|
+
|
115
|
+
== Contributing
|
116
|
+
|
117
|
+
In the spirit of [free software](http://www.fsf.org/licensing/essays/free-sw.html), **everyone** is encouraged to help improve this project.
|
118
|
+
|
119
|
+
Here are some ways *you* can contribute:
|
120
|
+
|
121
|
+
* by using alpha, beta, and prerelease versions
|
122
|
+
* by reporting bugs
|
123
|
+
* by suggesting new features
|
124
|
+
* by writing or editing documentation
|
125
|
+
* by writing specifications
|
126
|
+
* by writing code (**no patch is too small**: fix typos, add comments, clean up inconsistent whitespace)
|
127
|
+
* by refactoring code
|
128
|
+
* by reviewing patches
|
129
|
+
|
130
|
+
All contributors will be added to the [HISTORY](https://github.net/terracycle/tc-api-gem/HISTORY.mkd)
|
131
|
+
file and will receive the respect and gratitude of the community.
|
132
|
+
|
133
|
+
== Copyright
|
134
|
+
Copyright (c) 2011 TerraCycle Inc..
|
135
|
+
See [LICENSE](https://github.com/terracycle/tc-api-gem/LICENSE.mkd) for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# echoe - used to release the gem
|
2
|
+
require 'rubygems'
|
3
|
+
require 'rake'
|
4
|
+
require 'echoe'
|
5
|
+
|
6
|
+
Echoe.new('terracycle', '0.1.0') do |p|
|
7
|
+
p.description = 'A Ruby wrapper for the Terracycle REST APIs'
|
8
|
+
p.url = 'http://github.com/terracycle/tc-api-gem'
|
9
|
+
p.author ='Albert Mckeever'
|
10
|
+
p.email ='albert.mckeever@terracycle.net'
|
11
|
+
p.ignore_pattern = ['temp/*', 'script/*']
|
12
|
+
p.development_dependencies = []
|
13
|
+
end
|
14
|
+
|
15
|
+
Dir['#{File.dirname(__FILE__)}/tasks/*.rake'].sort.each { |ext| load ext }
|
16
|
+
|
17
|
+
# Bundler
|
18
|
+
#require 'bundler'
|
19
|
+
#Bundler::GemHelper.install_tasks
|
20
|
+
|
21
|
+
#Rspec
|
22
|
+
require 'rspec/core/rake_task'
|
23
|
+
RSpec::Core::RakeTask.new(:spec)
|
24
|
+
|
25
|
+
task :default => :spec
|
26
|
+
|
27
|
+
namespace :doc do
|
28
|
+
require 'yard'
|
29
|
+
YARD::Rake::YardocTask.new do |task|
|
30
|
+
task.files = ['HISTORY.mkd', 'LICENSE.mkd', 'lib/**/*.rb']
|
31
|
+
task.options = [
|
32
|
+
'--protected',
|
33
|
+
'--output-dir', 'doc/yard',
|
34
|
+
'--tag', 'format:Supported formats',
|
35
|
+
'--tag', 'authenticated:Requires Authentication',
|
36
|
+
'--markup', 'markdown',
|
37
|
+
]
|
38
|
+
end
|
39
|
+
end
|
data/VERSION.yml
ADDED
data/add
ADDED
File without changes
|
@@ -0,0 +1,149 @@
|
|
1
|
+
module Terracycle
|
2
|
+
class Base
|
3
|
+
extend Forwardable
|
4
|
+
|
5
|
+
def_delegators :client, :get, :post
|
6
|
+
|
7
|
+
attr_reader :client
|
8
|
+
|
9
|
+
def initialize(client)
|
10
|
+
@client = client
|
11
|
+
end
|
12
|
+
|
13
|
+
# Signs up a new user
|
14
|
+
#
|
15
|
+
# @format :json, :xml
|
16
|
+
# @authenticated false
|
17
|
+
# @see http://api.Terracycle.net/api_home/update_user
|
18
|
+
# @example Update the authenticating user's profile
|
19
|
+
# @URL - http://api.terracycle.net/api_home/sign_up
|
20
|
+
# One of the following must be present:
|
21
|
+
# first name, last name - see http://api.terracycle.net/api_home/sign_up for complete list
|
22
|
+
def signup_create(user, email, first_mame, last_name, login, default_charity, password, password_confirmation, minimum_registration_age, terms)
|
23
|
+
perform_post("/users/create.json", :body => {:user => user, :email => email, :first_mame => first_mame, :last_name => last_name, :login => login,
|
24
|
+
:default_charity => default_charity, :password => password, :password_confirmation => password_confirmation,
|
25
|
+
:minimum_registration_age => minimum_registration_age, :terms => terms })
|
26
|
+
end
|
27
|
+
|
28
|
+
# Returns extended information of a given user
|
29
|
+
#
|
30
|
+
# @todo Overload the method to allow fetching of the authenticated user's user name from configuration.
|
31
|
+
# @format :json, :xml
|
32
|
+
# @authenticated true
|
33
|
+
# @param user [Integer, String] A Terracycle user ID or user name.
|
34
|
+
# @see http://api.terracycle.net/api_home/users
|
35
|
+
# @example Return extended information
|
36
|
+
# Terracycle.user("Johndoe33")
|
37
|
+
# Terracycle.user(7505382) # Same as above
|
38
|
+
def user_info(username, format)
|
39
|
+
perform_get("/users/#{username}.#{format}", :mash => false)
|
40
|
+
end
|
41
|
+
|
42
|
+
# Updates the authenticating user's profile
|
43
|
+
#
|
44
|
+
# @note This method asynchronously processes the uploaded file before updating the user's profile.
|
45
|
+
# @format :json, :xml
|
46
|
+
# @authenticated true
|
47
|
+
# @see http://api.Terracycle.net/api_home/update_user
|
48
|
+
# @example Update the authenticating user's profile
|
49
|
+
# @URL - http://api.terracycle.net/users/update/username.format
|
50
|
+
# One of the following must be present:
|
51
|
+
# first name, last name - see http://api.Terracycle.net/api_home/update_use for complete list
|
52
|
+
def update_user(username, body={})
|
53
|
+
perform_post("/users/update/#{username}.json", :body => body)
|
54
|
+
end
|
55
|
+
|
56
|
+
|
57
|
+
# Returns a user's collection
|
58
|
+
#
|
59
|
+
# @overload collections(options={})
|
60
|
+
# @param options [Hash] A customizable set of options.
|
61
|
+
# @option options [Integer] :cursor (-1) Breaks the results into pages. Provide values as returned in the response objects's next_cursor and previous_cursor attributes to page back and forth in the list.
|
62
|
+
# @return [Hashie::Mash]
|
63
|
+
# @example Return the authenticated user's collections
|
64
|
+
# Terracycle.collections
|
65
|
+
# @overload collections(user, options={})
|
66
|
+
# @param user [Integer, String] A Terracycle user ID or user name.
|
67
|
+
# @param options [Hash] A customizable set of options.
|
68
|
+
# @option options [Integer] :cursor (-1) Breaks the results into pages. Provide values as returned in the response objects's next_cursor and previous_cursor attributes to page back and forth in the list.
|
69
|
+
# @return [Hashie::Mash]
|
70
|
+
# @example Return @johndoe33's collections
|
71
|
+
# Terracycle.collections("johndoe33")
|
72
|
+
# Terracycle.collections(7505382) # Same as above
|
73
|
+
# @see http://api.terracycle.net/api_home/get/collections
|
74
|
+
# @format :json, :xml
|
75
|
+
# @authenticated true
|
76
|
+
|
77
|
+
def user_collection(username, id, format)
|
78
|
+
perform_get("/users/#{username}/collections/#{id}.#{format}", :mash => false)
|
79
|
+
end
|
80
|
+
|
81
|
+
# Returns the specified brigade
|
82
|
+
#
|
83
|
+
# @format :json, :xml
|
84
|
+
# @authenticated true
|
85
|
+
# @param brigade_id [Integer, String] The id or slug of the brigade.
|
86
|
+
# @option options [Integer] :cursor (-1) Breaks the results into pages. Provide values as returned in the response objects's next_cursor and previous_cursor attributes to page back and forth in the brigade.
|
87
|
+
# @return [Array]
|
88
|
+
# @see http://api.Terracycle.com/api_home/brigade_signup
|
89
|
+
# @example Return the brigade minimum waste collected
|
90
|
+
# Terracycle.brigade_lists("chip bag brigade", "energy bar wrapper brigade")
|
91
|
+
def brigade(id, format)
|
92
|
+
perform_get("/brigades/#{id}.#{format}", :mash => true)
|
93
|
+
end
|
94
|
+
|
95
|
+
# Returns the subscribers of the specified label Requests
|
96
|
+
#
|
97
|
+
# @format :json, :xml
|
98
|
+
# @authenticated true
|
99
|
+
# @param id [Integer] The collection id of the Label Requests
|
100
|
+
# @param options [Hash] A customizable set of options.
|
101
|
+
# @return [Array] The subscribers of the specified collection.
|
102
|
+
# @see http://api.terracycle.net/api_home/labels_request
|
103
|
+
# @example Return the label request of user
|
104
|
+
# Terracycle.collection("chip bag", "Toasted chip")
|
105
|
+
def label_requests_show(id, format)
|
106
|
+
perform_get("/label_requests/#{id}.#{format}", :mash => true)
|
107
|
+
end
|
108
|
+
|
109
|
+
# Returns the subscribers of the specified labels
|
110
|
+
#
|
111
|
+
# @format :json, :xml
|
112
|
+
# @authenticated true
|
113
|
+
# @param id [Integer] The collection id of the label.
|
114
|
+
# @param options [Hash] A customizable set of options.
|
115
|
+
# @return [Array] The subscribers of the specified collection.
|
116
|
+
# @see http://api.terracycle.net/api_home/labels
|
117
|
+
# @example Return the labels of user
|
118
|
+
# Terracycle.collection("chip bag", "Toasted chip")
|
119
|
+
def label_show(id, format)
|
120
|
+
perform_get("/labels/#{id}.#{format}")
|
121
|
+
end
|
122
|
+
|
123
|
+
|
124
|
+
# Brigade/collection sign up
|
125
|
+
#
|
126
|
+
# @note Brigade sign up is unlimited.
|
127
|
+
# @format :json, :xml
|
128
|
+
# @authenticated true.
|
129
|
+
# @param brigade_id [Integer, String] The id or slug of the brigade.
|
130
|
+
# @param id [Integer] The user id of the brigade.
|
131
|
+
# @see http://api.Terracycle.com/api_home/brigae_signup
|
132
|
+
# Terracycle.brigade_lists("chip bag brigade", "energy bar wrapper brigade")
|
133
|
+
def collection_create(body = {})
|
134
|
+
|
135
|
+
perform_post("/collections/create.json", :body => body)
|
136
|
+
end
|
137
|
+
|
138
|
+
|
139
|
+
private
|
140
|
+
def perform_get(path, options={})
|
141
|
+
Terracycle::Request.get(self, path, options)
|
142
|
+
|
143
|
+
end
|
144
|
+
|
145
|
+
def perform_post(path, options={})
|
146
|
+
Terracycle::Request.post(self, path, options)
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Terracycle
|
2
|
+
class HTTPAuth
|
3
|
+
include HTTParty
|
4
|
+
format :plain
|
5
|
+
|
6
|
+
attr_reader :username, :password, :options
|
7
|
+
|
8
|
+
def initialize(username, password, options={})
|
9
|
+
@username, @password = username, password
|
10
|
+
@options = {:ssl => false}.merge(options)
|
11
|
+
self.class.base_uri "http#{'s' if options[:ssl]}://api.terracycle.net"
|
12
|
+
end
|
13
|
+
|
14
|
+
def get(uri, headers={})
|
15
|
+
self.class.get(uri, :headers => headers, :basic_auth => basic_auth)
|
16
|
+
end
|
17
|
+
|
18
|
+
def post(uri, body={}, headers={})
|
19
|
+
self.class.post(uri, :body => body, :headers => headers, :basic_auth => basic_auth)
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
def basic_auth
|
24
|
+
@basic_auth ||= {:username => @username, :password => @password}
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module Terracycle
|
2
|
+
class OAuth
|
3
|
+
extend Forwardable
|
4
|
+
def_delegators :access_token, :get, :post
|
5
|
+
|
6
|
+
attr_reader :ctoken, :csecret
|
7
|
+
|
8
|
+
def initialize(ctoken, csecret)
|
9
|
+
@ctoken, @csecret = ctoken, csecret
|
10
|
+
end
|
11
|
+
|
12
|
+
def consumer
|
13
|
+
@consumer ||= ::OAuth::Consumer.new(@ctoken, @csecret, {:site => 'http://api.terracycle.net'})
|
14
|
+
end
|
15
|
+
|
16
|
+
def request_token
|
17
|
+
@request_token ||= consumer.get_request_token
|
18
|
+
end
|
19
|
+
|
20
|
+
def authorize_from_request(rtoken, rsecret)
|
21
|
+
request_token = ::OAuth::RequestToken.new(consumer, rtoken, rsecret)
|
22
|
+
access_token = request_token.get_access_token
|
23
|
+
@atoken, @asecret = access_token.token, access_token.secret
|
24
|
+
end
|
25
|
+
|
26
|
+
def access_token
|
27
|
+
@access_token ||= ::OAuth::AccessToken.new(consumer, @atoken, @asecret)
|
28
|
+
end
|
29
|
+
|
30
|
+
def authorize_from_access(atoken, asecret)
|
31
|
+
@atoken, @asecret = atoken, asecret
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,102 @@
|
|
1
|
+
module Terracycle
|
2
|
+
class Request
|
3
|
+
extend Forwardable
|
4
|
+
|
5
|
+
def self.get(client, path, options={})
|
6
|
+
new(client, :get, path, options).perform
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.post(client, path, options={})
|
10
|
+
new(client, :post, path, options).perform
|
11
|
+
end
|
12
|
+
|
13
|
+
attr_reader :client, :method, :path, :options
|
14
|
+
|
15
|
+
def_delegators :client, :get, :post
|
16
|
+
|
17
|
+
def initialize(client, method, path, options={})
|
18
|
+
@client, @method, @path, @options = client, method, path, {:mash => true}.merge(options)
|
19
|
+
end
|
20
|
+
|
21
|
+
def uri
|
22
|
+
@uri ||= begin
|
23
|
+
uri = URI.parse(path)
|
24
|
+
|
25
|
+
if options[:query] && options[:query] != {}
|
26
|
+
uri.query = to_query(options[:query])
|
27
|
+
end
|
28
|
+
|
29
|
+
uri.to_s
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def perform
|
34
|
+
make_friendly(send("perform_#{method}"))
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
def perform_get
|
39
|
+
send(:get, uri, options[:headers])
|
40
|
+
end
|
41
|
+
|
42
|
+
def perform_post
|
43
|
+
send(:post, uri, options[:body], options[:headers])
|
44
|
+
end
|
45
|
+
|
46
|
+
def make_friendly(response)
|
47
|
+
raise_errors(response)
|
48
|
+
data = parse(response)
|
49
|
+
options[:mash] ? mash(data) : data
|
50
|
+
end
|
51
|
+
|
52
|
+
def raise_errors(response)
|
53
|
+
case response.code.to_i
|
54
|
+
when 400
|
55
|
+
data = parse(response)
|
56
|
+
raise RateLimitExceeded.new(data), "(#{response.code}): #{response.message} - #{data['error'] if data}"
|
57
|
+
when 401
|
58
|
+
data = parse(response)
|
59
|
+
raise Unauthorized.new(data), "(#{response.code}): #{response.message} - #{data['error'] if data}"
|
60
|
+
when 403
|
61
|
+
data = parse(response)
|
62
|
+
raise General.new(data), "(#{response.code}): #{response.message} - #{data['error'] if data}"
|
63
|
+
when 404
|
64
|
+
raise NotFound, "(#{response.code}): #{response.message}"
|
65
|
+
when 500
|
66
|
+
raise InformTerracycle, "Terracycle had an internal error. Please let them know in the group. (#{response.code}): #{response.message}"
|
67
|
+
when 502..503
|
68
|
+
raise Unavailable, "(#{response.code}): #{response.message}"
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def parse(response)
|
73
|
+
Crack::JSON.parse(response.body)
|
74
|
+
end
|
75
|
+
|
76
|
+
def mash(obj)
|
77
|
+
if obj.is_a?(Array)
|
78
|
+
obj.map { |item| make_mash_with_consistent_hash(item) }
|
79
|
+
elsif obj.is_a?(Hash)
|
80
|
+
make_mash_with_consistent_hash(obj)
|
81
|
+
else
|
82
|
+
obj
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
# Lame workaround for the fact that mash doesn't hash correctly
|
87
|
+
def make_mash_with_consistent_hash(obj)
|
88
|
+
m = Mash.new(obj)
|
89
|
+
def m.hash
|
90
|
+
inspect.hash
|
91
|
+
end
|
92
|
+
return m
|
93
|
+
end
|
94
|
+
|
95
|
+
def to_query(options)
|
96
|
+
options.inject([]) do |collection, opt|
|
97
|
+
collection << "#{opt[0]}=#{opt[1]}"
|
98
|
+
collection
|
99
|
+
end * '&'
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
data/lib/terracycle.rb
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
require 'forwardable'
|
2
|
+
require 'rubygems'
|
3
|
+
|
4
|
+
gem 'oauth', '0.3.2'
|
5
|
+
require 'oauth'
|
6
|
+
|
7
|
+
gem 'mash', '0.0.3'
|
8
|
+
require 'mash'
|
9
|
+
|
10
|
+
gem 'httparty', '0.4.3'
|
11
|
+
require 'httparty'
|
12
|
+
|
13
|
+
module Terracycle
|
14
|
+
class TerracycleError < StandardError
|
15
|
+
attr_reader :data
|
16
|
+
|
17
|
+
def initialize(data)
|
18
|
+
@data = data
|
19
|
+
super
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
class RateLimitExceeded < TerracycleError; end
|
24
|
+
class Unauthorized < TerracycleError; end
|
25
|
+
class General < TerracycleError; end
|
26
|
+
|
27
|
+
class Unavailable < StandardError; end
|
28
|
+
class InformTerracycle < StandardError; end
|
29
|
+
class NotFound < StandardError; end
|
30
|
+
|
31
|
+
|
32
|
+
def self.label(id)
|
33
|
+
response = HTTParty.get("http://api.terracycle.net/labels/#{id}.json", :format => :json)
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.user_info(username)
|
37
|
+
HTTParty.get("http://api.terracycle.net/users/#{username}.json", :format => :json)
|
38
|
+
end
|
39
|
+
|
40
|
+
def self.brigade(id)
|
41
|
+
HTTParty.get("http://api.terracycle.net/brigades/#{id}.xml", :format => :xml)
|
42
|
+
end
|
43
|
+
|
44
|
+
def self.collection(id)
|
45
|
+
HTTParty.get("http://api.terracycle.net/collections/#{id}.json", :format => :json)
|
46
|
+
end
|
47
|
+
|
48
|
+
def self.label_requests(id)
|
49
|
+
HTTParty.get("http://api.terracycle.net/users/#{username}/collections/#{:id}.json", :format => :json)
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
def self.user_collection(username, id)
|
54
|
+
HTTParty.get("http://api.terracycle.net/label_requests/#{id}.json", :format => :json)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
directory = File.dirname(__FILE__)
|
59
|
+
$:.unshift(directory) unless $:.include?(directory)
|
60
|
+
|
61
|
+
require 'terracycle/oauth'
|
62
|
+
require 'terracycle/httpauth'
|
63
|
+
require 'terracycle/request'
|
64
|
+
require 'terracycle/base'
|
data/terracycle.gemspec
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{terracycle}
|
5
|
+
s.version = "0.1.0"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["Albert Mckeever"]
|
9
|
+
s.date = %q{2011-01-10}
|
10
|
+
s.description = %q{A Ruby wrapper for the Terracycle REST APIs}
|
11
|
+
s.email = %q{albert.mckeever@terracycle.net}
|
12
|
+
s.extra_rdoc_files = ["README.rdoc", "lib/terracycle.rb", "lib/terracycle/base.rb", "lib/terracycle/httpauth.rb", "lib/terracycle/oauth.rb", "lib/terracycle/request.rb"]
|
13
|
+
s.files = ["AUTHORS", "History.mkd", "Licence.mkd", "Manifest", "Notes.txt", "README.rdoc", "Rakefile", "VERSION.yml", "add", "lib/terracycle.rb", "lib/terracycle/base.rb", "lib/terracycle/httpauth.rb", "lib/terracycle/oauth.rb", "lib/terracycle/request.rb", "terracycle.gemspec"]
|
14
|
+
s.homepage = %q{http://github.com/terracycle/tc-api-gem}
|
15
|
+
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Terracycle", "--main", "README.rdoc"]
|
16
|
+
s.require_paths = ["lib"]
|
17
|
+
s.rubyforge_project = %q{terracycle}
|
18
|
+
s.rubygems_version = %q{1.4.2}
|
19
|
+
s.summary = %q{A Ruby wrapper for the Terracycle REST APIs}
|
20
|
+
|
21
|
+
if s.respond_to? :specification_version then
|
22
|
+
s.specification_version = 3
|
23
|
+
|
24
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
25
|
+
else
|
26
|
+
end
|
27
|
+
else
|
28
|
+
end
|
29
|
+
end
|
metadata
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: terracycle
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Albert Mckeever
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-01-10 00:00:00 -05:00
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description: A Ruby wrapper for the Terracycle REST APIs
|
23
|
+
email: albert.mckeever@terracycle.net
|
24
|
+
executables: []
|
25
|
+
|
26
|
+
extensions: []
|
27
|
+
|
28
|
+
extra_rdoc_files:
|
29
|
+
- README.rdoc
|
30
|
+
- lib/terracycle.rb
|
31
|
+
- lib/terracycle/base.rb
|
32
|
+
- lib/terracycle/httpauth.rb
|
33
|
+
- lib/terracycle/oauth.rb
|
34
|
+
- lib/terracycle/request.rb
|
35
|
+
files:
|
36
|
+
- AUTHORS
|
37
|
+
- History.mkd
|
38
|
+
- Licence.mkd
|
39
|
+
- Manifest
|
40
|
+
- Notes.txt
|
41
|
+
- README.rdoc
|
42
|
+
- Rakefile
|
43
|
+
- VERSION.yml
|
44
|
+
- add
|
45
|
+
- lib/terracycle.rb
|
46
|
+
- lib/terracycle/base.rb
|
47
|
+
- lib/terracycle/httpauth.rb
|
48
|
+
- lib/terracycle/oauth.rb
|
49
|
+
- lib/terracycle/request.rb
|
50
|
+
- terracycle.gemspec
|
51
|
+
has_rdoc: true
|
52
|
+
homepage: http://github.com/terracycle/tc-api-gem
|
53
|
+
licenses: []
|
54
|
+
|
55
|
+
post_install_message:
|
56
|
+
rdoc_options:
|
57
|
+
- --line-numbers
|
58
|
+
- --inline-source
|
59
|
+
- --title
|
60
|
+
- Terracycle
|
61
|
+
- --main
|
62
|
+
- README.rdoc
|
63
|
+
require_paths:
|
64
|
+
- lib
|
65
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
66
|
+
none: false
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
hash: 3
|
71
|
+
segments:
|
72
|
+
- 0
|
73
|
+
version: "0"
|
74
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
75
|
+
none: false
|
76
|
+
requirements:
|
77
|
+
- - ">="
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
hash: 11
|
80
|
+
segments:
|
81
|
+
- 1
|
82
|
+
- 2
|
83
|
+
version: "1.2"
|
84
|
+
requirements: []
|
85
|
+
|
86
|
+
rubyforge_project: terracycle
|
87
|
+
rubygems_version: 1.4.2
|
88
|
+
signing_key:
|
89
|
+
specification_version: 3
|
90
|
+
summary: A Ruby wrapper for the Terracycle REST APIs
|
91
|
+
test_files: []
|
92
|
+
|