cocoafish 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +12 -0
- data/Gemfile.lock +28 -0
- data/LICENSE.txt +20 -0
- data/README.md +184 -0
- data/Rakefile +53 -0
- data/VERSION +1 -0
- data/cocoafish.gemspec +83 -0
- data/lib/cocoafish/client.rb +82 -0
- data/lib/cocoafish/cocoafish_object.rb +26 -0
- data/lib/cocoafish/connection.rb +106 -0
- data/lib/cocoafish/endpoint.rb +13 -0
- data/lib/cocoafish/objectified_hash.rb +26 -0
- data/lib/cocoafish.rb +34 -0
- data/test/helper.rb +18 -0
- data/test/test_cocoafish.rb +7 -0
- metadata +179 -0
data/Gemfile
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
|
3
|
+
gem "rest-client", "~> 1.6.3"
|
4
|
+
gem "json_pure", "~> 1.4.6"
|
5
|
+
gem 'simple_oauth', "~> 0.1.4", :require => 'simple_oauth'
|
6
|
+
|
7
|
+
group :development do
|
8
|
+
gem "shoulda", ">= 0"
|
9
|
+
gem "bundler", "~> 1.0.0"
|
10
|
+
gem "jeweler", "~> 1.5.2"
|
11
|
+
gem "rcov", ">= 0"
|
12
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
git (1.2.5)
|
5
|
+
jeweler (1.5.2)
|
6
|
+
bundler (~> 1.0.0)
|
7
|
+
git (>= 1.2.5)
|
8
|
+
rake
|
9
|
+
json_pure (1.4.6)
|
10
|
+
mime-types (1.16)
|
11
|
+
rake (0.9.2)
|
12
|
+
rcov (0.9.9)
|
13
|
+
rest-client (1.6.3)
|
14
|
+
mime-types (>= 1.16)
|
15
|
+
shoulda (2.11.3)
|
16
|
+
simple_oauth (0.1.5)
|
17
|
+
|
18
|
+
PLATFORMS
|
19
|
+
ruby
|
20
|
+
|
21
|
+
DEPENDENCIES
|
22
|
+
bundler (~> 1.0.0)
|
23
|
+
jeweler (~> 1.5.2)
|
24
|
+
json_pure (~> 1.4.6)
|
25
|
+
rcov
|
26
|
+
rest-client (~> 1.6.3)
|
27
|
+
shoulda
|
28
|
+
simple_oauth (~> 0.1.4)
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Wei
|
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/README.md
ADDED
@@ -0,0 +1,184 @@
|
|
1
|
+
# Cocoafish Ruby Client
|
2
|
+
|
3
|
+
This is a Ruby client for the Cocoafish server backand that can be used in your Ruby scripts and Ruby on Rails apps. For full documentation about the API methods that can be used through this gem, see the [Cocoafish REST API documentation](http://cocoafish.com/docs/rest). This has been developed and tested with Ruby 1.8.7/1.9.2 and Rails 3.0.5. Earlier versions of Ruby or Rails may not work.
|
4
|
+
|
5
|
+
## Setup
|
6
|
+
|
7
|
+
Cocoafish is currently in Private Beta. Before your app can access the Cocoafish API servers, you must:
|
8
|
+
|
9
|
+
1. Request a Beta invitation code at http://cocoafish.com/beta/signup
|
10
|
+
2. Use the Beta invitation code to create an account at http://cocoafish.com/signup
|
11
|
+
3. Register your app at http://cocoafish.com/apps to generate an app key, OAuth consumer key, and OAuth secret
|
12
|
+
|
13
|
+
## Installation
|
14
|
+
|
15
|
+
### Rails
|
16
|
+
|
17
|
+
For Rails 3, add the Cocoafish gem to your Gemfile:
|
18
|
+
|
19
|
+
gem 'cocoafish'
|
20
|
+
|
21
|
+
and install with bundler:
|
22
|
+
|
23
|
+
# bundle install
|
24
|
+
|
25
|
+
### Ruby
|
26
|
+
|
27
|
+
For plain Ruby manually install the Cocoafish gem:
|
28
|
+
|
29
|
+
# sudo gem install cocoafish
|
30
|
+
|
31
|
+
Then include it in your scripts:
|
32
|
+
|
33
|
+
require 'rubygems'
|
34
|
+
require 'cocoafish'
|
35
|
+
|
36
|
+
## Usage
|
37
|
+
|
38
|
+
### Authenticating with Ccooafish
|
39
|
+
|
40
|
+
Find the OAuth consumer key and secret for your app on the [Cocoafish app management site](http://cocoafish.com/apps) and use them to set the credentials so that your app can talk to the Cocoafish API servers.
|
41
|
+
|
42
|
+
Cocoafish::Client.set_credentials('key', 'secret')
|
43
|
+
|
44
|
+
### Making Requests
|
45
|
+
|
46
|
+
Use the Cocoafish client http methods to store and retrive data with Cocoafish by passing a url path and optional parameters hash:
|
47
|
+
|
48
|
+
result = Cocoafish::Client.post(path, params = {})
|
49
|
+
result = Cocoafish::Client.get(path, params = {})
|
50
|
+
result = Cocoafish::Client.put(path, params = {})
|
51
|
+
result = Cocoafish::Client.delete(path, params = {})
|
52
|
+
|
53
|
+
The url path is the suffix of a [Cocoafish REST API](http://cocoafish.com/docs/rest) call such as:
|
54
|
+
|
55
|
+
result = Cocoafish::Client.get("places/show.json", {:place_id => "4e10f444d0afbe4156000019"})
|
56
|
+
result = Cocoafish::Client.post("users/login.json", {:login => "jane@cocoafish.com", :password => "cocoafish"})
|
57
|
+
|
58
|
+
### Processing Responses
|
59
|
+
|
60
|
+
Cocoafish API servers return data in JSON format:
|
61
|
+
|
62
|
+
{
|
63
|
+
"meta": {
|
64
|
+
"status": "ok",
|
65
|
+
"code": 200,
|
66
|
+
"method_name": "createUser",
|
67
|
+
"session_id": "6z4xBTs-cg1fL7U7RGVDSw_9vC8"
|
68
|
+
},
|
69
|
+
"response": {
|
70
|
+
"users": [
|
71
|
+
{
|
72
|
+
"id": "4ec4c607356f4e12c8000035",
|
73
|
+
"first_name": "Jane",
|
74
|
+
"last_name": "User",
|
75
|
+
"created_at": "2011-11-17T08:29:59+0000",
|
76
|
+
"updated_at": "2011-11-17T08:29:59+0000",
|
77
|
+
"email": "jane@cocoafish.com"
|
78
|
+
}
|
79
|
+
]
|
80
|
+
}
|
81
|
+
}
|
82
|
+
|
83
|
+
The Ruby client library provides the response wrapped in an object accessible through dot notation:
|
84
|
+
|
85
|
+
result = Cocoafish::Client.post("users/login.json", {:login => "jane@cocoafish.com", :password => "cocoafish"})
|
86
|
+
puts result.response.users[0].id
|
87
|
+
=> 4ec4c870356f4e12c8000038
|
88
|
+
puts result.response.users[0].first_name
|
89
|
+
=> Jane
|
90
|
+
|
91
|
+
Or, the original JSON reponse string can be accessed:
|
92
|
+
|
93
|
+
result = Cocoafish::Client.post("users/login.json", {:login => "jane@cocoafish.com", :password => "cocoafish"})
|
94
|
+
json_result = JSON.parse(result.json)
|
95
|
+
puts result.response.users[0].id
|
96
|
+
=> 4ec4c870356f4e12c8000038
|
97
|
+
puts json_result["response"]["users"][0]['first_name']
|
98
|
+
=> Jane
|
99
|
+
|
100
|
+
The response code and message can be retreived through the meta:
|
101
|
+
|
102
|
+
puts result.meta.status
|
103
|
+
=> ok
|
104
|
+
puts result.meta.code
|
105
|
+
=> 200
|
106
|
+
puts result.meta.method_name
|
107
|
+
=> createUser
|
108
|
+
|
109
|
+
### Handling Errors
|
110
|
+
|
111
|
+
Errors returned from the Cocoafish API will be thrown as an exception. Wrap Cocoaifsh client calls in a begin/rescue block to capture and handle them gracefully.
|
112
|
+
|
113
|
+
begin
|
114
|
+
result = Cocoafish::Client.post("users/login.json", {:login => "jane@cocoafish.com", :password => "cocoafish"})
|
115
|
+
rescue Cocoafish::CocoafishError => e
|
116
|
+
puts "Login failed: #{e.message}"
|
117
|
+
end
|
118
|
+
|
119
|
+
## Code Samples
|
120
|
+
|
121
|
+
### Checkin App Example
|
122
|
+
|
123
|
+
The following script shows how to perform actions required for a checkin app: create users, create places, checkin to places with photos, and search for checkins by location.
|
124
|
+
|
125
|
+
require 'rubygems'
|
126
|
+
require 'cocoafish'
|
127
|
+
|
128
|
+
# set Oauth consumer key and secret from your app
|
129
|
+
Cocoafish::Client.set_credentials("7z5w8Jcg3iUemGab9ugty15oacju8fSF", "Jxc18og2eq1G0Mg9jAFoE3CUfCUdFS6L")
|
130
|
+
|
131
|
+
# create a user which logs her in automatically
|
132
|
+
result = Cocoafish::Client.post("users/create.json", {:email => "jane5@cocoafish.com", :password => "cocoafish", :password_confirmation => "cocoafish", :first_name => "Jane", :last_name => "User"})
|
133
|
+
|
134
|
+
# create a place
|
135
|
+
result = Cocoafish::Client.post("places/create.json", {:name => "Cocoafish HQ", :address => "156 2nd St", :city => "San Francisco", :state => "CA", :postal_code => "94107", :latitude => 37.787099, :longitude => -122.399101})
|
136
|
+
place_1_id = result.response.places[0].id
|
137
|
+
|
138
|
+
# checkin to the place with a photo and message
|
139
|
+
result = Cocoafish::Client.post("checkins/create.json", {:message => "Working hard!", :photo => "/Users/mgoff/Desktop/atwork.jpg", "photo_sync_sizes[]" => "large_1024", :place_id => place_1_id})
|
140
|
+
|
141
|
+
# create another place
|
142
|
+
result = Cocoafish::Client.post("places/create.json", {:name => "Kingdom of Dumpling", :address => "1713 Taraval St", :city => "San Francisco", :state => "CA", :postal_code => "94116", :latitude => 37.742640, :longitude => -122.484597})
|
143
|
+
place_2_id = result.response.places[0].id
|
144
|
+
|
145
|
+
# checkin to the place with a photo and message
|
146
|
+
result = Cocoafish::Client.post("checkins/create.json", {:message => "Eating some awesome Chinese dumplings!", :photo => "/Users/mgoff/Desktop/dumplings.jpg", "photo_sync_sizes[]" => "large_1024", :place_id => place_2_id})
|
147
|
+
|
148
|
+
# search for checkins from my current location in Union Square
|
149
|
+
result = Cocoafish::Client.get("checkins/search.json", {:latitude => 37.787930, :longitude => -122.407499})
|
150
|
+
|
151
|
+
# loop through the results and print them out
|
152
|
+
result.response.checkins.each_with_index do |checkin, index|
|
153
|
+
puts "Checkin #{index}"
|
154
|
+
puts " User: #{checkin.user.first_name} #{checkin.user.last_name}"
|
155
|
+
puts " Place: #{checkin.place.name}"
|
156
|
+
puts " Message: #{checkin.message}"
|
157
|
+
puts " Photo: #{checkin.photo.urls.large_1024}\n\n"
|
158
|
+
end
|
159
|
+
|
160
|
+
Running this script performs the actions and shows the nearby checkins along with URLs for the uploaded checkin photos:
|
161
|
+
|
162
|
+
Checkin 0
|
163
|
+
User: Jane User
|
164
|
+
Place: Cocoafish HQ
|
165
|
+
Message: Working hard!
|
166
|
+
Photo: http://storage.cocoafish.com/13B5z6WqEMRUVsqdmn1vudovz3RTexmn/photos/c9/39/4ec4e189356f4e12c80000c2/atwork_large_1024.jpg
|
167
|
+
|
168
|
+
Checkin 1
|
169
|
+
User: Jane User
|
170
|
+
Place: Kingdom of Dumpling
|
171
|
+
Message: Eating some awesome Chinese dumplings!
|
172
|
+
Photo: http://storage.cocoafish.com/13B5z6WqEMRUVsqdmn1vudovz3RTexmn/photos/c9/39/4ec4db91356f4e12c8000055/dumplings_large_1024.jpg
|
173
|
+
|
174
|
+
## Additional APIs
|
175
|
+
|
176
|
+
For more examples of API usage see the [Cocoafish REST API](http://cocoafish.com/docs/rest) documentation.
|
177
|
+
|
178
|
+
## Copyright
|
179
|
+
|
180
|
+
Copyright (c) 2011 Cocoafish. See LICENSE.txt for further details.
|
181
|
+
|
182
|
+
## Support
|
183
|
+
|
184
|
+
For questions or comments about the Cocoafish Ruby client gem, or about Cocoafish in general, contact <info@cocoafish.com>.
|
data/Rakefile
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
begin
|
4
|
+
Bundler.setup(:default, :development)
|
5
|
+
rescue Bundler::BundlerError => e
|
6
|
+
$stderr.puts e.message
|
7
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
+
exit e.status_code
|
9
|
+
end
|
10
|
+
require 'rake'
|
11
|
+
|
12
|
+
require 'jeweler'
|
13
|
+
Jeweler::Tasks.new do |gem|
|
14
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
15
|
+
gem.name = "cocoafish"
|
16
|
+
gem.homepage = "http://github.com/cocoafish"
|
17
|
+
gem.license = "MIT"
|
18
|
+
gem.summary = %Q{A Cocoafish Ruby Client}
|
19
|
+
gem.description = "Ruby client to access Cocoafish"%
|
20
|
+
gem.email = "wei@cocoafish.com"
|
21
|
+
gem.authors = ["Wei Kong"]
|
22
|
+
gem.files = Dir["{lib}/**/*", "./*"]
|
23
|
+
gem.add_runtime_dependency 'rest-client', '~> 1.6.3'
|
24
|
+
gem.add_runtime_dependency 'json_pure', '~> 1.4.6'
|
25
|
+
gem.add_runtime_dependency 'simple_oauth', '~> 0.1.4'
|
26
|
+
end
|
27
|
+
Jeweler::RubygemsDotOrgTasks.new
|
28
|
+
|
29
|
+
require 'rake/testtask'
|
30
|
+
Rake::TestTask.new(:test) do |test|
|
31
|
+
test.libs << 'lib' << 'test'
|
32
|
+
test.pattern = 'test/**/test_*.rb'
|
33
|
+
test.verbose = true
|
34
|
+
end
|
35
|
+
|
36
|
+
require 'rcov/rcovtask'
|
37
|
+
Rcov::RcovTask.new do |test|
|
38
|
+
test.libs << 'test'
|
39
|
+
test.pattern = 'test/**/test_*.rb'
|
40
|
+
test.verbose = true
|
41
|
+
end
|
42
|
+
|
43
|
+
task :default => :test
|
44
|
+
|
45
|
+
require 'rake/rdoctask'
|
46
|
+
Rake::RDocTask.new do |rdoc|
|
47
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
48
|
+
|
49
|
+
rdoc.rdoc_dir = 'rdoc'
|
50
|
+
rdoc.title = "cocoafish #{version}"
|
51
|
+
rdoc.rdoc_files.include('README*')
|
52
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
53
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.4
|
data/cocoafish.gemspec
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = "cocoafish"
|
8
|
+
s.version = "0.1.4"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Wei Kong"]
|
12
|
+
s.date = "2011-11-17"
|
13
|
+
s.description = "Ruby client to access Cocoafish"
|
14
|
+
s.email = "wei@cocoafish.com"
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE.txt",
|
17
|
+
"README.md"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
"./Gemfile",
|
21
|
+
"./Gemfile.lock",
|
22
|
+
"./LICENSE.txt",
|
23
|
+
"./README.md",
|
24
|
+
"./Rakefile",
|
25
|
+
"./VERSION",
|
26
|
+
"./cocoafish.gemspec",
|
27
|
+
"lib/cocoafish.rb",
|
28
|
+
"lib/cocoafish/client.rb",
|
29
|
+
"lib/cocoafish/cocoafish_object.rb",
|
30
|
+
"lib/cocoafish/connection.rb",
|
31
|
+
"lib/cocoafish/endpoint.rb",
|
32
|
+
"lib/cocoafish/objectified_hash.rb"
|
33
|
+
]
|
34
|
+
s.homepage = "http://github.com/cocoafish"
|
35
|
+
s.licenses = ["MIT"]
|
36
|
+
s.require_paths = ["lib"]
|
37
|
+
s.rubygems_version = "1.8.10"
|
38
|
+
s.summary = "A Cocoafish Ruby Client"
|
39
|
+
s.test_files = [
|
40
|
+
"test/helper.rb",
|
41
|
+
"test/test_cocoafish.rb"
|
42
|
+
]
|
43
|
+
|
44
|
+
if s.respond_to? :specification_version then
|
45
|
+
s.specification_version = 3
|
46
|
+
|
47
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
48
|
+
s.add_runtime_dependency(%q<rest-client>, ["~> 1.6.3"])
|
49
|
+
s.add_runtime_dependency(%q<json_pure>, ["~> 1.4.6"])
|
50
|
+
s.add_runtime_dependency(%q<simple_oauth>, ["~> 0.1.4"])
|
51
|
+
s.add_development_dependency(%q<shoulda>, [">= 0"])
|
52
|
+
s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
|
53
|
+
s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
|
54
|
+
s.add_development_dependency(%q<rcov>, [">= 0"])
|
55
|
+
s.add_runtime_dependency(%q<rest-client>, ["~> 1.6.3"])
|
56
|
+
s.add_runtime_dependency(%q<json_pure>, ["~> 1.4.6"])
|
57
|
+
s.add_runtime_dependency(%q<simple_oauth>, ["~> 0.1.4"])
|
58
|
+
else
|
59
|
+
s.add_dependency(%q<rest-client>, ["~> 1.6.3"])
|
60
|
+
s.add_dependency(%q<json_pure>, ["~> 1.4.6"])
|
61
|
+
s.add_dependency(%q<simple_oauth>, ["~> 0.1.4"])
|
62
|
+
s.add_dependency(%q<shoulda>, [">= 0"])
|
63
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
64
|
+
s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
|
65
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
66
|
+
s.add_dependency(%q<rest-client>, ["~> 1.6.3"])
|
67
|
+
s.add_dependency(%q<json_pure>, ["~> 1.4.6"])
|
68
|
+
s.add_dependency(%q<simple_oauth>, ["~> 0.1.4"])
|
69
|
+
end
|
70
|
+
else
|
71
|
+
s.add_dependency(%q<rest-client>, ["~> 1.6.3"])
|
72
|
+
s.add_dependency(%q<json_pure>, ["~> 1.4.6"])
|
73
|
+
s.add_dependency(%q<simple_oauth>, ["~> 0.1.4"])
|
74
|
+
s.add_dependency(%q<shoulda>, [">= 0"])
|
75
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
76
|
+
s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
|
77
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
78
|
+
s.add_dependency(%q<rest-client>, ["~> 1.6.3"])
|
79
|
+
s.add_dependency(%q<json_pure>, ["~> 1.4.6"])
|
80
|
+
s.add_dependency(%q<simple_oauth>, ["~> 0.1.4"])
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
@@ -0,0 +1,82 @@
|
|
1
|
+
module Cocoafish
|
2
|
+
|
3
|
+
class Client
|
4
|
+
|
5
|
+
@@connection = nil
|
6
|
+
@@debug = false
|
7
|
+
@@realm = "http://api.cocoafish.com"
|
8
|
+
|
9
|
+
class << self
|
10
|
+
|
11
|
+
def set_credentials(token, secret, options = {})
|
12
|
+
@@realm = options[:hostname] || @@realm
|
13
|
+
@@connection = Connection.new(token, secret, options)
|
14
|
+
@@connection.debug = @@debug
|
15
|
+
end
|
16
|
+
|
17
|
+
def set_session_id(session_id = nil)
|
18
|
+
@@connection.session_id = session_id
|
19
|
+
end
|
20
|
+
|
21
|
+
def debug=(debug_flag)
|
22
|
+
@@debug = debug_flag
|
23
|
+
@@connection.debug = @@debug if @@connection
|
24
|
+
end
|
25
|
+
|
26
|
+
def debug
|
27
|
+
@@debug
|
28
|
+
end
|
29
|
+
|
30
|
+
def get(endpoint, data=nil)
|
31
|
+
raise NoConnectionEstablished if @@connection.nil?
|
32
|
+
json_hash = @@connection.get Cocoafish::Endpoint.url(@@realm, endpoint), data
|
33
|
+
parse_response(json_hash)
|
34
|
+
end
|
35
|
+
|
36
|
+
def delete(endpoint, data=nil)
|
37
|
+
raise NoConnectionEstablished if @@connection.nil?
|
38
|
+
json_hash = @@connection.delete Cocoafish::Endpoint.url(@@realm, endpoint), data
|
39
|
+
parse_response(json_hash)
|
40
|
+
end
|
41
|
+
|
42
|
+
def post(endpoint, data=nil)
|
43
|
+
raise NoConnectionEstablished if @@connection.nil?
|
44
|
+
json_hash = @@connection.post Cocoafish::Endpoint.url(@@realm, endpoint), data
|
45
|
+
parse_response(json_hash)
|
46
|
+
end
|
47
|
+
|
48
|
+
def put(endpoint, data=nil)
|
49
|
+
raise NoConnectionEstablished if @@connection.nil?
|
50
|
+
json_hash = @@connection.put Cocoafish::Endpoint.url(@@realm, endpoint), data
|
51
|
+
parse_response(json_hash)
|
52
|
+
end
|
53
|
+
|
54
|
+
def parse_response(json_hash)
|
55
|
+
CocoafishObject.new(json_hash)
|
56
|
+
end
|
57
|
+
|
58
|
+
def get_paginated_array(response)
|
59
|
+
|
60
|
+
# return nil for empty json
|
61
|
+
if response.json == nil
|
62
|
+
return nil
|
63
|
+
end
|
64
|
+
|
65
|
+
# make sure we have only 1 top-level object in the response (users, places, etc.)
|
66
|
+
if response.response.instance_variables.count != 1
|
67
|
+
return nil
|
68
|
+
end
|
69
|
+
|
70
|
+
# get the response array
|
71
|
+
orig_array = response.response.instance_variable_get(response.response.instance_variables.first)
|
72
|
+
|
73
|
+
# create the paginated object
|
74
|
+
if response.meta.page && orig_array
|
75
|
+
WillPaginate::Collection.create(response.meta.page, response.meta.per_page, response.meta.total_results) do |pager|
|
76
|
+
pager.replace(orig_array)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module Cocoafish
|
2
|
+
|
3
|
+
class CocoafishObject
|
4
|
+
def initialize(hash)
|
5
|
+
hash.each do |k,v|
|
6
|
+
if k == "ratings_summary"
|
7
|
+
# ratings_summary is a hash with keys as integers
|
8
|
+
next
|
9
|
+
end
|
10
|
+
v = CocoafishObject.new(v) if v.is_a?(Hash)
|
11
|
+
v = v.map {|arrayvalue| arrayvalue.is_a?(Hash) ? CocoafishObject.new(arrayvalue) : arrayvalue} if v.is_a?(Array)
|
12
|
+
begin
|
13
|
+
self.instance_variable_set("@#{k}", v) ## create and initialize an instance variable for this key/value pair
|
14
|
+
self.class.send(:define_method, k, proc{self.instance_variable_get("@#{k}")}) ## create the getter that returns the instance variable
|
15
|
+
self.class.send(:define_method, "#{k}=", proc{|v| self.instance_variable_set("@#{k}", v)}) ## create the setter that sets the instance variable
|
16
|
+
rescue
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def method_missing key
|
22
|
+
nil
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
@@ -0,0 +1,106 @@
|
|
1
|
+
module Cocoafish
|
2
|
+
class Connection
|
3
|
+
|
4
|
+
attr_accessor :session_id, :debug
|
5
|
+
|
6
|
+
def initialize(token, secret, options = {})
|
7
|
+
@key = token
|
8
|
+
@secret = secret
|
9
|
+
@cookies = Hash.new
|
10
|
+
@options = options
|
11
|
+
end
|
12
|
+
|
13
|
+
def get(endpoint, data=nil)
|
14
|
+
request :get, endpoint, data
|
15
|
+
end
|
16
|
+
|
17
|
+
def delete(endpoint, data=nil)
|
18
|
+
request :delete, endpoint, data
|
19
|
+
end
|
20
|
+
|
21
|
+
def post(endpoint, data=nil)
|
22
|
+
request :post, endpoint, data
|
23
|
+
end
|
24
|
+
|
25
|
+
def put(endpoint, data=nil)
|
26
|
+
request :put, endpoint, data
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def request(method, endpoint, data)
|
32
|
+
|
33
|
+
# set the cookies to send with the header
|
34
|
+
if !@options.has_key?(:manage_cookies) || @options[:manage_cookies] != false
|
35
|
+
@header_cookies = @cookies
|
36
|
+
else
|
37
|
+
@header_cookies = {}
|
38
|
+
end
|
39
|
+
|
40
|
+
# set the session id
|
41
|
+
if @session_id
|
42
|
+
@header_cookies[:_session_id] = @session_id
|
43
|
+
end
|
44
|
+
|
45
|
+
headers = { 'User-Agent' => "Cocoafish Ruby Client v#{CLIENT_VERSION}", :cookies => @header_cookies }
|
46
|
+
|
47
|
+
oauth_options = {
|
48
|
+
:consumer_key => @key,
|
49
|
+
:consumer_secret => @secret,
|
50
|
+
:token => "",
|
51
|
+
:token_secret => "",
|
52
|
+
:nonce => rand(10 ** 30).to_s.rjust(30,'0'),
|
53
|
+
:timestamp => Time.now.to_i
|
54
|
+
}
|
55
|
+
|
56
|
+
case method
|
57
|
+
when :get, :delete
|
58
|
+
|
59
|
+
oauth_header = SimpleOAuth::Header.new(method, endpoint, data, oauth_options)
|
60
|
+
options = headers.merge(:params => data, :authorization => oauth_header)
|
61
|
+
begin
|
62
|
+
response = RestClient.send(method, endpoint, options)
|
63
|
+
rescue RestClient::Exception => e
|
64
|
+
raise CocoafishError.new(e), nil, caller[5..-1]
|
65
|
+
end
|
66
|
+
|
67
|
+
when :post, :put
|
68
|
+
|
69
|
+
data.each do |key,value|
|
70
|
+
if key.to_s == "photo" || key.to_s =~ /photos\[\d+\]/
|
71
|
+
data[key] = File.new(value, 'rb')
|
72
|
+
end
|
73
|
+
end
|
74
|
+
data.merge!(:multipart => true)
|
75
|
+
|
76
|
+
oauth_header = SimpleOAuth::Header.new(method, endpoint, nil, oauth_options)
|
77
|
+
options = headers.merge(:authorization => oauth_header)
|
78
|
+
|
79
|
+
begin
|
80
|
+
response = RestClient.send(method, endpoint, data, options)
|
81
|
+
rescue RestClient::Exception => e
|
82
|
+
raise CocoafishError.new(e), nil, caller[3..-1]
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
# grab the cookies from the response
|
87
|
+
@cookies.merge!(response.cookies)
|
88
|
+
|
89
|
+
if response.body.empty?
|
90
|
+
content = nil
|
91
|
+
else
|
92
|
+
begin
|
93
|
+
content = JSON.parse(response.body)
|
94
|
+
rescue JSON::ParserError
|
95
|
+
raise DecodeError, "content: <#{response.body}>"
|
96
|
+
end
|
97
|
+
# add json data
|
98
|
+
content[:json] = response.body
|
99
|
+
end
|
100
|
+
|
101
|
+
return content
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module Cocoafish
|
2
|
+
|
3
|
+
class ObjectifiedHash
|
4
|
+
|
5
|
+
def initialize hash
|
6
|
+
@data = hash.inject({}) do |data, (key,value)|
|
7
|
+
if value.kind_of? Hash
|
8
|
+
value = ObjectifiedHash.new value
|
9
|
+
elsif value.kind_of? Array
|
10
|
+
value = value.map {|arrayvalue| ObjectifiedHash.new arrayvalue}
|
11
|
+
end
|
12
|
+
data[key.to_s] = value
|
13
|
+
data
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def method_missing key
|
18
|
+
if @data.key? key.to_s
|
19
|
+
@data[key.to_s]
|
20
|
+
else
|
21
|
+
nil
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
end
|
data/lib/cocoafish.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'uri'
|
2
|
+
require 'json'
|
3
|
+
require 'rest_client'
|
4
|
+
require 'simple_oauth'
|
5
|
+
|
6
|
+
require 'cocoafish/connection'
|
7
|
+
require 'cocoafish/endpoint'
|
8
|
+
require 'cocoafish/client'
|
9
|
+
require 'cocoafish/cocoafish_object'
|
10
|
+
require 'cocoafish/objectified_hash'
|
11
|
+
|
12
|
+
module Cocoafish
|
13
|
+
API_VERSION = "v1"
|
14
|
+
CLIENT_VERSION = File.read(File.join(File.dirname(__FILE__), '..', 'VERSION'))
|
15
|
+
|
16
|
+
class CocoafishError < StandardError
|
17
|
+
def initialize(e)
|
18
|
+
@re = e
|
19
|
+
end
|
20
|
+
|
21
|
+
def to_s
|
22
|
+
content = JSON.parse(@re.response)
|
23
|
+
"#{@re.message} - #{content['meta']['message']}"
|
24
|
+
end
|
25
|
+
|
26
|
+
def http_code
|
27
|
+
@re.http_code
|
28
|
+
end
|
29
|
+
|
30
|
+
def response
|
31
|
+
@re.response
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
data/test/helper.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
begin
|
4
|
+
Bundler.setup(:default, :development)
|
5
|
+
rescue Bundler::BundlerError => e
|
6
|
+
$stderr.puts e.message
|
7
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
+
exit e.status_code
|
9
|
+
end
|
10
|
+
require 'test/unit'
|
11
|
+
require 'shoulda'
|
12
|
+
|
13
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
14
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
15
|
+
require 'cocoafish'
|
16
|
+
|
17
|
+
class Test::Unit::TestCase
|
18
|
+
end
|
metadata
ADDED
@@ -0,0 +1,179 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cocoafish
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.4
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Wei Kong
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-11-17 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rest-client
|
16
|
+
requirement: &70257427969120 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 1.6.3
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70257427969120
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: json_pure
|
27
|
+
requirement: &70257427967800 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ~>
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 1.4.6
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70257427967800
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: simple_oauth
|
38
|
+
requirement: &70257427967040 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ~>
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 0.1.4
|
44
|
+
type: :runtime
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *70257427967040
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: shoulda
|
49
|
+
requirement: &70257427966300 !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: *70257427966300
|
58
|
+
- !ruby/object:Gem::Dependency
|
59
|
+
name: bundler
|
60
|
+
requirement: &70257427964200 !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ~>
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: 1.0.0
|
66
|
+
type: :development
|
67
|
+
prerelease: false
|
68
|
+
version_requirements: *70257427964200
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: jeweler
|
71
|
+
requirement: &70257427960520 !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ~>
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: 1.5.2
|
77
|
+
type: :development
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: *70257427960520
|
80
|
+
- !ruby/object:Gem::Dependency
|
81
|
+
name: rcov
|
82
|
+
requirement: &70257427957560 !ruby/object:Gem::Requirement
|
83
|
+
none: false
|
84
|
+
requirements:
|
85
|
+
- - ! '>='
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
type: :development
|
89
|
+
prerelease: false
|
90
|
+
version_requirements: *70257427957560
|
91
|
+
- !ruby/object:Gem::Dependency
|
92
|
+
name: rest-client
|
93
|
+
requirement: &70257427929720 !ruby/object:Gem::Requirement
|
94
|
+
none: false
|
95
|
+
requirements:
|
96
|
+
- - ~>
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: 1.6.3
|
99
|
+
type: :runtime
|
100
|
+
prerelease: false
|
101
|
+
version_requirements: *70257427929720
|
102
|
+
- !ruby/object:Gem::Dependency
|
103
|
+
name: json_pure
|
104
|
+
requirement: &70257427927480 !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ~>
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: 1.4.6
|
110
|
+
type: :runtime
|
111
|
+
prerelease: false
|
112
|
+
version_requirements: *70257427927480
|
113
|
+
- !ruby/object:Gem::Dependency
|
114
|
+
name: simple_oauth
|
115
|
+
requirement: &70257427925240 !ruby/object:Gem::Requirement
|
116
|
+
none: false
|
117
|
+
requirements:
|
118
|
+
- - ~>
|
119
|
+
- !ruby/object:Gem::Version
|
120
|
+
version: 0.1.4
|
121
|
+
type: :runtime
|
122
|
+
prerelease: false
|
123
|
+
version_requirements: *70257427925240
|
124
|
+
description: Ruby client to access Cocoafish
|
125
|
+
email: wei@cocoafish.com
|
126
|
+
executables: []
|
127
|
+
extensions: []
|
128
|
+
extra_rdoc_files:
|
129
|
+
- LICENSE.txt
|
130
|
+
- README.md
|
131
|
+
files:
|
132
|
+
- ./Gemfile
|
133
|
+
- ./Gemfile.lock
|
134
|
+
- ./LICENSE.txt
|
135
|
+
- ./README.md
|
136
|
+
- ./Rakefile
|
137
|
+
- ./VERSION
|
138
|
+
- ./cocoafish.gemspec
|
139
|
+
- lib/cocoafish.rb
|
140
|
+
- lib/cocoafish/client.rb
|
141
|
+
- lib/cocoafish/cocoafish_object.rb
|
142
|
+
- lib/cocoafish/connection.rb
|
143
|
+
- lib/cocoafish/endpoint.rb
|
144
|
+
- lib/cocoafish/objectified_hash.rb
|
145
|
+
- LICENSE.txt
|
146
|
+
- README.md
|
147
|
+
- test/helper.rb
|
148
|
+
- test/test_cocoafish.rb
|
149
|
+
homepage: http://github.com/cocoafish
|
150
|
+
licenses:
|
151
|
+
- MIT
|
152
|
+
post_install_message:
|
153
|
+
rdoc_options: []
|
154
|
+
require_paths:
|
155
|
+
- lib
|
156
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
157
|
+
none: false
|
158
|
+
requirements:
|
159
|
+
- - ! '>='
|
160
|
+
- !ruby/object:Gem::Version
|
161
|
+
version: '0'
|
162
|
+
segments:
|
163
|
+
- 0
|
164
|
+
hash: -2104059489818044473
|
165
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
166
|
+
none: false
|
167
|
+
requirements:
|
168
|
+
- - ! '>='
|
169
|
+
- !ruby/object:Gem::Version
|
170
|
+
version: '0'
|
171
|
+
requirements: []
|
172
|
+
rubyforge_project:
|
173
|
+
rubygems_version: 1.8.10
|
174
|
+
signing_key:
|
175
|
+
specification_version: 3
|
176
|
+
summary: A Cocoafish Ruby Client
|
177
|
+
test_files:
|
178
|
+
- test/helper.rb
|
179
|
+
- test/test_cocoafish.rb
|