CSApi 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/.box +4 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/Rakefile +1 -0
- data/csapi.gemspec +26 -0
- data/examples/example/example.rb +78 -0
- data/lib/csapi.rb +173 -0
- data/lib/csapi/version.rb +3 -0
- data/readme.md +58 -0
- metadata +89 -0
data/.box
ADDED
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Roberto Hidalgo
|
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/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/csapi.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'csapi/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "CSApi"
|
8
|
+
gem.version = CS::VERSION
|
9
|
+
gem.authors = ["Roberto Hidalgo"]
|
10
|
+
gem.email = ["un@rob.mx"]
|
11
|
+
gem.description = "Simple wrapper of couchsurfing.org API for accessing profile, friends, searching, etc."
|
12
|
+
gem.summary = "I have no idea what is this"
|
13
|
+
gem.homepage = "https://github.com/unRob/CouchSurfing-API"
|
14
|
+
gem.has_rdoc = false
|
15
|
+
|
16
|
+
gem.files = `git ls-files`.split($/)
|
17
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
18
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
19
|
+
gem.require_paths = ["lib"]
|
20
|
+
|
21
|
+
|
22
|
+
gem.add_runtime_dependency 'httparty'
|
23
|
+
gem.add_runtime_dependency 'json'
|
24
|
+
gem.add_runtime_dependency 'nokogiri'
|
25
|
+
|
26
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#encoding: utf-8
|
3
|
+
|
4
|
+
require_relative '../lib/csapi'
|
5
|
+
|
6
|
+
begin
|
7
|
+
api = CS.new('username','password')
|
8
|
+
rescue CS::AuthError
|
9
|
+
puts "Incorrect username or password"
|
10
|
+
exit
|
11
|
+
end
|
12
|
+
|
13
|
+
# ===
|
14
|
+
# Get a user's info, by default ours
|
15
|
+
# ===
|
16
|
+
profile = api.profile('205974')
|
17
|
+
|
18
|
+
# ===
|
19
|
+
# Get a user's photos, by default ours
|
20
|
+
# ===
|
21
|
+
photos = api.photos('205974')
|
22
|
+
|
23
|
+
# ===
|
24
|
+
# Get a user's friends, by default ours
|
25
|
+
# ===
|
26
|
+
friends = api.friends('205974')
|
27
|
+
|
28
|
+
# ===
|
29
|
+
# Get a user's references, by default ours
|
30
|
+
# ===
|
31
|
+
references = api.references('205974')
|
32
|
+
|
33
|
+
# ===
|
34
|
+
# Get the current user's recent requests
|
35
|
+
# ===
|
36
|
+
limit = 10
|
37
|
+
requests = api.requests(limit)
|
38
|
+
|
39
|
+
# ===
|
40
|
+
# Create a new Couch Request
|
41
|
+
# ===
|
42
|
+
=begin
|
43
|
+
details = {
|
44
|
+
subject: 'This is my request subject',
|
45
|
+
number: 1, #How many people travel with you
|
46
|
+
arrival: 1339543920, #a Unix Timestamp with your arrival date
|
47
|
+
departure: 1339643920, #a Unix Timestamp with your departure date
|
48
|
+
arrival_flexible: true,
|
49
|
+
departure_flexible: false,
|
50
|
+
is_open_couchrequest: false,
|
51
|
+
to: 12345, #a numeric user id, I guess, have yet to figure this out ,
|
52
|
+
message: 'This is my request message' #I've yet to figure out how to do the multi-part requests
|
53
|
+
}
|
54
|
+
couch_request = CS::Request.new(details)
|
55
|
+
=end
|
56
|
+
|
57
|
+
#api.requests(1).each do |key, value|
|
58
|
+
# pp value
|
59
|
+
#end
|
60
|
+
|
61
|
+
# ===
|
62
|
+
# Search for people in a city with various search constraints
|
63
|
+
# ===
|
64
|
+
options = {
|
65
|
+
location: 'venice',
|
66
|
+
gender: 'female',
|
67
|
+
:'has-photo' => false,
|
68
|
+
:'member-type' => 'host' ,
|
69
|
+
vouched: nil,
|
70
|
+
verified: nil,
|
71
|
+
network: nil,
|
72
|
+
:'min-age' => nil,
|
73
|
+
:'max-age' => nil,
|
74
|
+
}
|
75
|
+
results = api.search(options)
|
76
|
+
results.each do |id, user|
|
77
|
+
puts "Found (UID:#{id}) #{user[:name]} in #{user[:location]} with a couch status of #{user[:status]} and a photo #{user[:pic]}\n\n"
|
78
|
+
end
|
data/lib/csapi.rb
ADDED
@@ -0,0 +1,173 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#encoding: utf-8
|
3
|
+
|
4
|
+
=begin
|
5
|
+
|
6
|
+
Licensed under The MIT License
|
7
|
+
Copyright (c) 2012 Partido Surrealista Mexicano
|
8
|
+
=end
|
9
|
+
|
10
|
+
require 'httparty'
|
11
|
+
require 'json'
|
12
|
+
require 'nokogiri'
|
13
|
+
|
14
|
+
class CS
|
15
|
+
include HTTParty
|
16
|
+
base_uri 'https://api.couchsurfing.org'
|
17
|
+
headers "Content-Type" => 'application/json'
|
18
|
+
follow_redirects false
|
19
|
+
@uid = '0'
|
20
|
+
|
21
|
+
def initialize(username, password)
|
22
|
+
@username = username
|
23
|
+
r = self.class.post('/sessions', body:{username: username, password: password}.to_json)
|
24
|
+
raise CS::AuthError.new("Could not login") if r.code != 200
|
25
|
+
@cookies = []
|
26
|
+
r.headers['Set-Cookie'].split(/, (?!\d)/).each do |cookie|
|
27
|
+
key, value = cookie.split(';')[0].split('=')
|
28
|
+
@cookies = "#{key}=#{value}"
|
29
|
+
end
|
30
|
+
data = JSON.parse r.body
|
31
|
+
@uid = data['url'].gsub(/[^\d]/, '')
|
32
|
+
@profile = data.keep_if {|k,v| ['realname', 'username', 'profile_image', 'gender', 'address'].include?(k)}
|
33
|
+
@profile['uid'] = @uid
|
34
|
+
self.class.headers 'Cookie' => @cookies
|
35
|
+
@@instance = self
|
36
|
+
end
|
37
|
+
|
38
|
+
def CS::instance
|
39
|
+
@@instance
|
40
|
+
end
|
41
|
+
|
42
|
+
def requests(limit=10)
|
43
|
+
url = "/users/#{@uid}/couchrequests"
|
44
|
+
q = {
|
45
|
+
limit: limit
|
46
|
+
}
|
47
|
+
r = self.class.get(url, query:q)
|
48
|
+
requests = {}
|
49
|
+
response = JSON.parse r.body
|
50
|
+
response['object'].each do |req|
|
51
|
+
key = req.gsub(/[^\d]/, '')
|
52
|
+
requests[key] = self.request(key)
|
53
|
+
end
|
54
|
+
requests
|
55
|
+
end
|
56
|
+
|
57
|
+
|
58
|
+
def request(id)
|
59
|
+
url = "/couchrequests/#{id}"
|
60
|
+
r = self.class.get(url)
|
61
|
+
JSON.parse r.body
|
62
|
+
end
|
63
|
+
|
64
|
+
def userdata
|
65
|
+
@profile
|
66
|
+
end
|
67
|
+
|
68
|
+
def profile(user=@uid)
|
69
|
+
url = "/users/#{user}/profile"
|
70
|
+
r = self.class.get(url)
|
71
|
+
JSON.parse r.body
|
72
|
+
end
|
73
|
+
|
74
|
+
def photos(user=@uid)
|
75
|
+
url = "/users/#{user}/photos"
|
76
|
+
r = self.class.get(url)
|
77
|
+
JSON.parse r.body
|
78
|
+
end
|
79
|
+
|
80
|
+
def friends(user=@uid)
|
81
|
+
url = "/users/#{user}/friends"
|
82
|
+
r = self.class.get(url)
|
83
|
+
JSON.parse r.body
|
84
|
+
end
|
85
|
+
|
86
|
+
def references(user=@uid)
|
87
|
+
url = "/users/#{user}/references"
|
88
|
+
r = self.class.get(url)
|
89
|
+
JSON.parse r.body
|
90
|
+
end
|
91
|
+
|
92
|
+
def search(options)
|
93
|
+
|
94
|
+
defaults = {
|
95
|
+
location: nil,
|
96
|
+
gender: nil,
|
97
|
+
:'has-photo' => nil,
|
98
|
+
:'member-type' => 'host' ,
|
99
|
+
vouched: nil,
|
100
|
+
verified: nil,
|
101
|
+
network: nil,
|
102
|
+
:'min-age' => nil,
|
103
|
+
:'max-age' => nil,
|
104
|
+
:platform => 'android'
|
105
|
+
}
|
106
|
+
|
107
|
+
options = defaults.merge(options)
|
108
|
+
html = self.class.get('/msearch', :query => options)
|
109
|
+
doc = Nokogiri::HTML(html);
|
110
|
+
users = {}
|
111
|
+
statuses = {
|
112
|
+
'M' => 'maybe',
|
113
|
+
'T' => 'travelling',
|
114
|
+
'Y' => 'available',
|
115
|
+
'N' => 'unavailable'
|
116
|
+
}
|
117
|
+
doc.xpath('//article').each do |article|
|
118
|
+
id = article.at_css('a').attr('href').split('/').last
|
119
|
+
user = {
|
120
|
+
name: article.children.at_css("h2").content,
|
121
|
+
location: article.children.at_css("div.location").content,
|
122
|
+
status: statuses[article['class'].match(/couch-([A-Z])/)[1]],
|
123
|
+
pic: article.at_css('img').attr('src')
|
124
|
+
}
|
125
|
+
users[id] = user
|
126
|
+
end
|
127
|
+
|
128
|
+
users;
|
129
|
+
end
|
130
|
+
|
131
|
+
class AuthError < StandardError
|
132
|
+
end
|
133
|
+
|
134
|
+
class APIError < StandardError
|
135
|
+
end
|
136
|
+
|
137
|
+
class Request
|
138
|
+
@api = nil
|
139
|
+
|
140
|
+
def initialize(options={})
|
141
|
+
if options[:username] && options[:password]
|
142
|
+
api = CS.new(options[:username], options[:password])
|
143
|
+
options.del(:username)
|
144
|
+
options.del(:password)
|
145
|
+
else
|
146
|
+
api = CS::instance
|
147
|
+
if api==nil
|
148
|
+
raise CS::APIError('You have not authenticated with the service or did not provide a :username and :password')
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
#pp api.userdata
|
153
|
+
options[:subject] = options[:subject] || "#{api.userdata['realname']} from #{api.userdata['address']['country']} sent you a new CouchRequest!"
|
154
|
+
options[:number] = options[:number] || 1
|
155
|
+
options[:arrival_flexible] = options[:arrival_flexible] || false
|
156
|
+
options[:departure_flexible] = options[:departure_flexible] || false
|
157
|
+
options[:is_open_couchrequest] = options[:is_open_couchrequest] || false
|
158
|
+
options[:from] = api.userdata['uid']
|
159
|
+
options[:to] = options[:to] || api.userdata['uid']
|
160
|
+
options[:arrival] = Time.at(options[:arrival]).strftime("%FT%TZ") || (Time.now()+86400).strftime("%FT%TZ")
|
161
|
+
options[:departure] = Time.at(options[:departure]).strftime("%FT%TZ") || (Time.now()+86400*3).strftime("%FT%TZ")
|
162
|
+
options[:message] = options[:message] || "I'm to lazy to write a proper couch request. HOST ME PLZ?"
|
163
|
+
#puts options.to_json
|
164
|
+
|
165
|
+
url = "/couchrequests"
|
166
|
+
api.post(url, body:options.to_json)
|
167
|
+
|
168
|
+
#pp response.code
|
169
|
+
#pp response.body
|
170
|
+
|
171
|
+
end
|
172
|
+
end
|
173
|
+
end
|
data/readme.md
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
This is a simple CouchSurfing API
|
2
|
+
=================================
|
3
|
+
|
4
|
+
This used to be a PHP app, but I just changed my mind and went with Ruby instead. I'll probably make a PHP version soon
|
5
|
+
|
6
|
+
##Example
|
7
|
+
See [example.rb](https://github.com/unRob/CouchSurfing-API/blob/master/examples/example.rb)
|
8
|
+
|
9
|
+
##Requirements
|
10
|
+
* HTTParty
|
11
|
+
* Nokogiri
|
12
|
+
|
13
|
+
##Installation
|
14
|
+
|
15
|
+
As a gem:
|
16
|
+
|
17
|
+
gem install csapi
|
18
|
+
|
19
|
+
Or by hand:
|
20
|
+
|
21
|
+
gem install httparty
|
22
|
+
gem install nokogiri
|
23
|
+
git clone https://github.com/unRob/Couchsurfing-API
|
24
|
+
|
25
|
+
|
26
|
+
##Currently implemented features
|
27
|
+
|
28
|
+
* Look up you current requests
|
29
|
+
* Get a user's profile, pictures, friends, references
|
30
|
+
* Create a new couchrequest, but **I have yet to figure out the correct way to address it! The request will most likely go to Casey, so BEWARE**
|
31
|
+
* Search for users meeting specific criteria (location, can host, has photo, verified, minimum & maximum age, etc.)
|
32
|
+
|
33
|
+
##Roadmap
|
34
|
+
|
35
|
+
* Reply to CouchRequests
|
36
|
+
|
37
|
+
## Contributors
|
38
|
+
|
39
|
+
* [Peter Nosko](https://github.com/pnosko) - Search
|
40
|
+
|
41
|
+
|
42
|
+
##License
|
43
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software
|
44
|
+
Licensed under The MIT License and associated documentation files (the "Software"), to deal in
|
45
|
+
the Software without restriction, including without limitation the rights to use, copy, modify,
|
46
|
+
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
|
47
|
+
to whom the Software is furnished to do so, subject to the following conditions:
|
48
|
+
|
49
|
+
The above copyright notice and this permission notice shall be included in all copies or
|
50
|
+
substantial portions of the Software.
|
51
|
+
|
52
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
|
53
|
+
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
54
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
|
55
|
+
OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
56
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
57
|
+
|
58
|
+
Redistributions of files must retain the above copyright notice.
|
metadata
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: CSApi
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Roberto Hidalgo
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-10-03 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: httparty
|
16
|
+
requirement: &2165348420 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *2165348420
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: json
|
27
|
+
requirement: &2165347880 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *2165347880
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: nokogiri
|
38
|
+
requirement: &2165347320 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :runtime
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *2165347320
|
47
|
+
description: Simple wrapper of couchsurfing.org API for accessing profile, friends,
|
48
|
+
searching, etc.
|
49
|
+
email:
|
50
|
+
- un@rob.mx
|
51
|
+
executables: []
|
52
|
+
extensions: []
|
53
|
+
extra_rdoc_files: []
|
54
|
+
files:
|
55
|
+
- .box
|
56
|
+
- .gitignore
|
57
|
+
- Gemfile
|
58
|
+
- LICENSE.txt
|
59
|
+
- Rakefile
|
60
|
+
- csapi.gemspec
|
61
|
+
- examples/example/example.rb
|
62
|
+
- lib/csapi.rb
|
63
|
+
- lib/csapi/version.rb
|
64
|
+
- readme.md
|
65
|
+
homepage: https://github.com/unRob/CouchSurfing-API
|
66
|
+
licenses: []
|
67
|
+
post_install_message:
|
68
|
+
rdoc_options: []
|
69
|
+
require_paths:
|
70
|
+
- lib
|
71
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ! '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
78
|
+
none: false
|
79
|
+
requirements:
|
80
|
+
- - ! '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
requirements: []
|
84
|
+
rubyforge_project:
|
85
|
+
rubygems_version: 1.8.5
|
86
|
+
signing_key:
|
87
|
+
specification_version: 3
|
88
|
+
summary: I have no idea what is this
|
89
|
+
test_files: []
|