Class: CS::CouchSearch

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

Constant Summary collapse

@@instance =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ CouchSearch

Returns a new instance of CouchSearch.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/csapi/search.rb', line 13

def initialize 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 = options.merge(defaults)
  @@instance = self
  
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



4
5
6
# File 'lib/csapi/search.rb', line 4

def options
  @options
end

#resultsObject

Returns the value of attribute results.



4
5
6
# File 'lib/csapi/search.rb', line 4

def results
  @results
end

Class Method Details

.instanceObject



9
10
11
# File 'lib/csapi/search.rb', line 9

def self.instance
  @@instance
end

Instance Method Details

#executeObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/csapi/search.rb', line 33

def execute
  html = HTTP.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 = {
      string_id: article.attr('rel'),
      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
  
  @results = CS::SearchResults.new(users)
  @results
end

#moreObject



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

def more
  @options[:page] = (@options[:page]||0)+1
  @options[:exclude_ids] = results.collect {|k, u| u[:string_id]}
  results
end