Class: CS::Api
Instance Method Summary collapse
- #friends(user = @uid) ⇒ Object
-
#initialize(username, password) ⇒ Api
constructor
A new instance of Api.
- #photos(user = @uid) ⇒ Object
- #profile(user = @uid) ⇒ Object
- #references(user = @uid) ⇒ Object
- #request(id) ⇒ Object
- #requests(limit = 10) ⇒ Object
- #search(options) ⇒ Object
- #userdata ⇒ Object
Constructor Details
#initialize(username, password) ⇒ Api
Returns a new instance of Api.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/csapi.rb', line 22 def initialize(username, password) @username = username r = self.class.post('/sessions', body:{username: username, password: password}.to_json) raise CS::AuthError.new("Could not login") if r.code != 200 @cookies = [] r.headers['Set-Cookie'].split(/, (?!\d)/).each do || key, value = .split(';')[0].split('=') @cookies = "#{key}=#{value}" end data = JSON.parse r.body @uid = data['url'].gsub(/[^\d]/, '') @profile = data.keep_if {|k,v| ['realname', 'username', 'profile_image', 'gender', 'address'].include?(k)} @profile['uid'] = @uid self.class.headers 'Cookie' => @cookies @@instance = self end |
Instance Method Details
#friends(user = @uid) ⇒ Object
84 85 86 87 88 |
# File 'lib/csapi.rb', line 84 def friends(user=@uid) url = "/users/#{user}/friends" r = self.class.get(url) JSON.parse r.body end |
#photos(user = @uid) ⇒ Object
78 79 80 81 82 |
# File 'lib/csapi.rb', line 78 def photos(user=@uid) url = "/users/#{user}/photos" r = self.class.get(url) JSON.parse r.body end |
#profile(user = @uid) ⇒ Object
72 73 74 75 76 |
# File 'lib/csapi.rb', line 72 def profile(user=@uid) url = "/users/#{user}/profile" r = self.class.get(url) JSON.parse r.body end |
#references(user = @uid) ⇒ Object
90 91 92 93 94 |
# File 'lib/csapi.rb', line 90 def references(user=@uid) url = "/users/#{user}/references" r = self.class.get(url) JSON.parse r.body end |
#request(id) ⇒ Object
62 63 64 65 66 |
# File 'lib/csapi.rb', line 62 def request(id) url = "/couchrequests/#{id}" r = self.class.get(url) JSON.parse r.body end |
#requests(limit = 10) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/csapi.rb', line 46 def requests(limit=10) url = "/users/#{@uid}/couchrequests" q = { limit: limit } r = self.class.get(url, query:q) requests = {} response = JSON.parse r.body response['object'].each do |req| key = req.gsub(/[^\d]/, '') requests[key] = self.request(key) end requests end |
#search(options) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/csapi.rb', line 96 def search() defaults = { location: nil, gender: nil, :'has-photo' => nil, :'member-type' => 'host' , vouched: nil, verified: nil, network: nil, :'min-age' => nil, :'max-age' => nil, :platform => 'android' } = defaults.merge() html = self.class.get('/msearch', :query => ) doc = Nokogiri::HTML(html); users = {} statuses = { 'M' => 'maybe', 'T' => 'travelling', 'Y' => 'available', 'N' => 'unavailable' } doc.xpath('//article').each do |article| id = article.at_css('a').attr('href').split('/').last user = { name: article.children.at_css("h2").content, location: article.children.at_css("div.location").content, status: statuses[article['class'].match(/couch-([A-Z])/)[1]], pic: article.at_css('img').attr('src') } users[id] = user end users; end |
#userdata ⇒ Object
68 69 70 |
# File 'lib/csapi.rb', line 68 def userdata @profile end |