CSApi 0.0.2 → 0.0.3
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/examples/{example/example.rb → example.rb} +2 -3
- data/lib/csapi.rb +108 -101
- data/lib/csapi/version.rb +1 -1
- data/readme.md +1 -1
- metadata +8 -9
- data/.box +0 -4
@@ -4,7 +4,7 @@
|
|
4
4
|
require_relative '../lib/csapi'
|
5
5
|
|
6
6
|
begin
|
7
|
-
api = CS.new('username','password')
|
7
|
+
api = CS::Api.new('username','password')
|
8
8
|
rescue CS::AuthError
|
9
9
|
puts "Incorrect username or password"
|
10
10
|
exit
|
@@ -15,6 +15,7 @@ end
|
|
15
15
|
# ===
|
16
16
|
profile = api.profile('205974')
|
17
17
|
|
18
|
+
exit;
|
18
19
|
# ===
|
19
20
|
# Get a user's photos, by default ours
|
20
21
|
# ===
|
@@ -39,7 +40,6 @@ requests = api.requests(limit)
|
|
39
40
|
# ===
|
40
41
|
# Create a new Couch Request
|
41
42
|
# ===
|
42
|
-
=begin
|
43
43
|
details = {
|
44
44
|
subject: 'This is my request subject',
|
45
45
|
number: 1, #How many people travel with you
|
@@ -52,7 +52,6 @@ details = {
|
|
52
52
|
message: 'This is my request message' #I've yet to figure out how to do the multi-part requests
|
53
53
|
}
|
54
54
|
couch_request = CS::Request.new(details)
|
55
|
-
=end
|
56
55
|
|
57
56
|
#api.requests(1).each do |key, value|
|
58
57
|
# pp value
|
data/lib/csapi.rb
CHANGED
@@ -10,124 +10,130 @@ Copyright (c) 2012 Partido Surrealista Mexicano
|
|
10
10
|
require 'httparty'
|
11
11
|
require 'json'
|
12
12
|
require 'nokogiri'
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
@cookies =
|
13
|
+
module CS
|
14
|
+
|
15
|
+
class Api
|
16
|
+
include HTTParty
|
17
|
+
base_uri 'https://api.couchsurfing.org'
|
18
|
+
headers "Content-Type" => 'application/json'
|
19
|
+
follow_redirects false
|
20
|
+
@uid = '0'
|
21
|
+
|
22
|
+
def initialize(username, password)
|
23
|
+
@username = username
|
24
|
+
r = self.class.post('/sessions', body:{username: username, password: password}.to_json)
|
25
|
+
|
26
|
+
raise CS::AuthError.new("Could not login") if r.code != 200
|
27
|
+
|
28
|
+
@cookies = []
|
29
|
+
r.headers['Set-Cookie'].split(/, (?!\d)/).each do |cookie|
|
30
|
+
key, value = cookie.split(';')[0].split('=')
|
31
|
+
@cookies = "#{key}=#{value}"
|
32
|
+
end
|
33
|
+
|
34
|
+
data = JSON.parse r.body
|
35
|
+
@uid = data['url'].gsub(/[^\d]/, '')
|
36
|
+
@profile = data.keep_if {|k,v| ['realname', 'username', 'profile_image', 'gender', 'address'].include?(k)}
|
37
|
+
@profile['uid'] = @uid
|
38
|
+
self.class.headers 'Cookie' => @cookies
|
39
|
+
@@instance = self
|
29
40
|
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
41
|
|
38
|
-
|
39
|
-
|
40
|
-
|
42
|
+
def CS::instance
|
43
|
+
@@instance
|
44
|
+
end
|
41
45
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
46
|
+
def requests(limit=10)
|
47
|
+
url = "/users/#{@uid}/couchrequests"
|
48
|
+
q = {
|
49
|
+
limit: limit
|
50
|
+
}
|
51
|
+
r = self.class.get(url, query:q)
|
52
|
+
requests = {}
|
53
|
+
response = JSON.parse r.body
|
54
|
+
response['object'].each do |req|
|
55
|
+
key = req.gsub(/[^\d]/, '')
|
56
|
+
requests[key] = self.request(key)
|
57
|
+
end
|
58
|
+
requests
|
53
59
|
end
|
54
|
-
requests
|
55
|
-
end
|
56
60
|
|
57
61
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
62
|
+
def request(id)
|
63
|
+
url = "/couchrequests/#{id}"
|
64
|
+
r = self.class.get(url)
|
65
|
+
JSON.parse r.body
|
66
|
+
end
|
63
67
|
|
64
|
-
|
65
|
-
|
66
|
-
|
68
|
+
def userdata
|
69
|
+
@profile
|
70
|
+
end
|
67
71
|
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
72
|
+
def profile(user=@uid)
|
73
|
+
url = "/users/#{user}/profile"
|
74
|
+
r = self.class.get(url)
|
75
|
+
JSON.parse r.body
|
76
|
+
end
|
73
77
|
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
78
|
+
def photos(user=@uid)
|
79
|
+
url = "/users/#{user}/photos"
|
80
|
+
r = self.class.get(url)
|
81
|
+
JSON.parse r.body
|
82
|
+
end
|
79
83
|
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
84
|
+
def friends(user=@uid)
|
85
|
+
url = "/users/#{user}/friends"
|
86
|
+
r = self.class.get(url)
|
87
|
+
JSON.parse r.body
|
88
|
+
end
|
85
89
|
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
90
|
+
def references(user=@uid)
|
91
|
+
url = "/users/#{user}/references"
|
92
|
+
r = self.class.get(url)
|
93
|
+
JSON.parse r.body
|
94
|
+
end
|
91
95
|
|
92
|
-
|
96
|
+
def search(options)
|
93
97
|
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
98
|
+
defaults = {
|
99
|
+
location: nil,
|
100
|
+
gender: nil,
|
101
|
+
:'has-photo' => nil,
|
102
|
+
:'member-type' => 'host' ,
|
103
|
+
vouched: nil,
|
104
|
+
verified: nil,
|
105
|
+
network: nil,
|
106
|
+
:'min-age' => nil,
|
107
|
+
:'max-age' => nil,
|
108
|
+
:platform => 'android'
|
109
|
+
}
|
106
110
|
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
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')
|
111
|
+
options = defaults.merge(options)
|
112
|
+
html = self.class.get('/msearch', :query => options)
|
113
|
+
doc = Nokogiri::HTML(html);
|
114
|
+
users = {}
|
115
|
+
statuses = {
|
116
|
+
'M' => 'maybe',
|
117
|
+
'T' => 'travelling',
|
118
|
+
'Y' => 'available',
|
119
|
+
'N' => 'unavailable'
|
124
120
|
}
|
125
|
-
|
126
|
-
|
121
|
+
doc.xpath('//article').each do |article|
|
122
|
+
id = article.at_css('a').attr('href').split('/').last
|
123
|
+
user = {
|
124
|
+
name: article.children.at_css("h2").content,
|
125
|
+
location: article.children.at_css("div.location").content,
|
126
|
+
status: statuses[article['class'].match(/couch-([A-Z])/)[1]],
|
127
|
+
pic: article.at_css('img').attr('src')
|
128
|
+
}
|
129
|
+
users[id] = user
|
130
|
+
end
|
127
131
|
|
128
|
-
|
132
|
+
users;
|
133
|
+
end
|
129
134
|
end
|
130
|
-
|
135
|
+
|
136
|
+
|
131
137
|
class AuthError < StandardError
|
132
138
|
end
|
133
139
|
|
@@ -170,4 +176,5 @@ class CS
|
|
170
176
|
|
171
177
|
end
|
172
178
|
end
|
179
|
+
|
173
180
|
end
|
data/lib/csapi/version.rb
CHANGED
data/readme.md
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: CSApi
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ date: 2012-10-03 00:00:00.000000000Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: httparty
|
16
|
-
requirement: &
|
16
|
+
requirement: &2161153440 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *2161153440
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: json
|
27
|
-
requirement: &
|
27
|
+
requirement: &2161152880 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *2161152880
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: nokogiri
|
38
|
-
requirement: &
|
38
|
+
requirement: &2161152300 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,7 +43,7 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :runtime
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *2161152300
|
47
47
|
description: Simple wrapper of couchsurfing.org API for accessing profile, friends,
|
48
48
|
searching, etc.
|
49
49
|
email:
|
@@ -52,13 +52,12 @@ executables: []
|
|
52
52
|
extensions: []
|
53
53
|
extra_rdoc_files: []
|
54
54
|
files:
|
55
|
-
- .box
|
56
55
|
- .gitignore
|
57
56
|
- Gemfile
|
58
57
|
- LICENSE.txt
|
59
58
|
- Rakefile
|
60
59
|
- csapi.gemspec
|
61
|
-
- examples/example
|
60
|
+
- examples/example.rb
|
62
61
|
- lib/csapi.rb
|
63
62
|
- lib/csapi/version.rb
|
64
63
|
- readme.md
|