Class: CS::Api

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username, password) ⇒ Api

Returns a new instance of Api.

Raises:



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/csapi.rb', line 45

def initialize(username, password)
  @username = username
  r = HTTP.post('/sessions', body:{username: username, password: password}.to_json)

  raise CS::APIError.new('Unsupported API version') if r.headers['X-CS-Error'] == 'clientVersionNotSupportedAnymore'
  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
  HTTP.headers 'Cookie' => @cookies
  CS.instance = self
end

Instance Attribute Details

#uidObject

Returns the value of attribute uid.



42
43
44
# File 'lib/csapi.rb', line 42

def uid
  @uid
end

Instance Method Details

#friends(user = @uid) ⇒ Object



118
119
120
121
122
# File 'lib/csapi.rb', line 118

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

#message(url) ⇒ Object



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

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

#messages(*args) ⇒ Object



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

def messages(*args)
  CS::Messages.getMessages(*args)
end

#photos(user = @uid) ⇒ Object



111
112
113
114
115
# File 'lib/csapi.rb', line 111

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

#profile(user = @uid) ⇒ Object



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

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

#references(user = @uid) ⇒ Object



125
126
127
128
129
# File 'lib/csapi.rb', line 125

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

#request(id) ⇒ Object



81
82
83
84
85
# File 'lib/csapi.rb', line 81

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

#requests(limit = 10) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/csapi.rb', line 65

def requests(limit=10)
  url = "/users/#{@uid}/couchrequests"
  q = {
      limit: limit
  }
  r = HTTP.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



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/csapi.rb', line 132

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


end

#userdataObject



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

def userdata
  @profile
end