Class: CS::Api

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/csapi.rb

Instance Method Summary collapse

Constructor Details

#initialize(username, password) ⇒ Api

Returns a new instance of Api.

Raises:



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# 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 = []
  @cookies=r.headers['Set-Cookie'].split(/;/).first
  #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



109
110
111
112
113
# File 'lib/csapi.rb', line 109

def friends(user=@uid)
  url = "/users/#{user}/friends"
  r = self.class.get(url)
  JSON.parse r.body
end

#message(url) ⇒ Object



88
89
90
91
# File 'lib/csapi.rb', line 88

def message(url)
  r = self.class.get(url)
  JSON.parse r.body
end

#messages(type = 'inbox', limit = 5, start = nil) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/csapi.rb', line 67

def messages(type='inbox', limit=5, start=nil)
  
  types = ['inbox', 'sent'];
  throw ArgumentError.new("Can't fetch messages of kind #{type}") if !types.include? type;
  
  url = "/users/#{@uid}/messages"
  q = {
    type: type,
    limit: limit
  }
  
  if (start)
    q[:start] = start
  end
  
  r = self.class.get(url, query:q);
  object = JSON.parse r.body
  
  return CS::Messages.new(object, q, self);      
end

#photos(user = @uid) ⇒ Object



103
104
105
106
107
# File 'lib/csapi.rb', line 103

def photos(user=@uid)
  url = "/users/#{user}/photos"
  r = self.class.get(url)
  JSON.parse r.body
end

#profile(user = @uid) ⇒ Object



97
98
99
100
101
# File 'lib/csapi.rb', line 97

def profile(user=@uid)
  url = "/users/#{user}/profile"
  r = self.class.get(url)
  JSON.parse r.body
end

#references(user = @uid) ⇒ Object



115
116
117
118
119
# File 'lib/csapi.rb', line 115

def references(user=@uid)
  url = "/users/#{user}/references"
  r = self.class.get(url)
  JSON.parse r.body
end

#request(id) ⇒ Object



60
61
62
63
64
# File 'lib/csapi.rb', line 60

def request(id)
  url = "/couchrequests/#{id}"
  r = self.class.get(url)
  JSON.parse r.body
end

#requests(limit = 10) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/csapi.rb', line 44

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



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/csapi.rb', line 121

def search(options)

  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'
  }

  options = defaults.merge(options)
  html = self.class.get('/msearch', :query => options)
  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

#userdataObject



93
94
95
# File 'lib/csapi.rb', line 93

def userdata
  @profile
end