CSApi 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -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
@@ -10,124 +10,130 @@ Copyright (c) 2012 Partido Surrealista Mexicano
10
10
  require 'httparty'
11
11
  require 'json'
12
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}"
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
- def CS::instance
39
- @@instance
40
- end
42
+ def CS::instance
43
+ @@instance
44
+ end
41
45
 
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)
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
- def request(id)
59
- url = "/couchrequests/#{id}"
60
- r = self.class.get(url)
61
- JSON.parse r.body
62
- end
62
+ def request(id)
63
+ url = "/couchrequests/#{id}"
64
+ r = self.class.get(url)
65
+ JSON.parse r.body
66
+ end
63
67
 
64
- def userdata
65
- @profile
66
- end
68
+ def userdata
69
+ @profile
70
+ end
67
71
 
68
- def profile(user=@uid)
69
- url = "/users/#{user}/profile"
70
- r = self.class.get(url)
71
- JSON.parse r.body
72
- end
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
- def photos(user=@uid)
75
- url = "/users/#{user}/photos"
76
- r = self.class.get(url)
77
- JSON.parse r.body
78
- end
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
- def friends(user=@uid)
81
- url = "/users/#{user}/friends"
82
- r = self.class.get(url)
83
- JSON.parse r.body
84
- end
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
- def references(user=@uid)
87
- url = "/users/#{user}/references"
88
- r = self.class.get(url)
89
- JSON.parse r.body
90
- end
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
- def search(options)
96
+ def search(options)
93
97
 
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
- }
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
- 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')
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
- users[id] = user
126
- end
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
- users;
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
@@ -1,3 +1,3 @@
1
1
  module CS
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/readme.md CHANGED
@@ -14,7 +14,7 @@ See [example.rb](https://github.com/unRob/CouchSurfing-API/blob/master/examples/
14
14
 
15
15
  As a gem:
16
16
 
17
- gem install csapi
17
+ gem install CSApi
18
18
 
19
19
  Or by hand:
20
20
 
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.2
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: &2165348420 !ruby/object:Gem::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: *2165348420
24
+ version_requirements: *2161153440
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: json
27
- requirement: &2165347880 !ruby/object:Gem::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: *2165347880
35
+ version_requirements: *2161152880
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: nokogiri
38
- requirement: &2165347320 !ruby/object:Gem::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: *2165347320
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/example.rb
60
+ - examples/example.rb
62
61
  - lib/csapi.rb
63
62
  - lib/csapi/version.rb
64
63
  - readme.md
data/.box DELETED
@@ -1,4 +0,0 @@
1
- php_version: '5.3.6'
2
- php_extensions: [apc, http, session]
3
- php_short_open_tag: On
4
-