Module: LeBonCoin::Search::SearchItems

Defined in:
lib/leboncoin/items.rb

Class Method Summary collapse

Class Method Details

.createItem(item = Hash.new) ⇒ Object

Create a new item



17
18
19
20
# File 'lib/leboncoin/items.rb', line 17

def createItem item = Hash.new
  @items.push item
  return item
end

.eachObject



22
23
24
25
26
# File 'lib/leboncoin/items.rb', line 22

def each
  @items.each do |item|
    yield item if block_given?
  end
end

.new(link) ⇒ Object

Default constructor



9
10
11
12
13
# File 'lib/leboncoin/items.rb', line 9

def new link
  @link = link
  @items = Array.new
  return self
end

.sizeObject



28
29
30
# File 'lib/leboncoin/items.rb', line 28

def size
  return @items.size
end

.to_jsonObject



32
33
34
35
# File 'lib/leboncoin/items.rb', line 32

def to_json
  require 'json'
  return JSON.pretty_generate(@items)
end

.to_rssObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/leboncoin/items.rb', line 37

def to_rss
  require 'rss/maker'

  content = RSS::Maker.make("2.0") do |m|
    m.channel.title = "leboncoin.fr"
    m.channel.link = @link
    m.channel.description = "leboncoin.fr"
    m.items.do_sort = true
    @items.each do |item|
      price = ""
      if item["price"] != nil
        price = item["price"].to_s + " " + item["currency"]
      end

      postcode = ""
      if item["postcode"] != nil
        postcode = item["postcode"]
      end

      i = m.items.new_item
      i.title = item["title"]
      i.link = item["link"]
      begin
        i.description = LeBonCoin::HTMLUtils.convert("<img src='" + item["image"] + "'/><br/>" \
          + "<b>Ville</b> : " + item["city"] + "<br/>" \
          + "<b>Code postal</b> : " + postcode + "<br/>" \
          + "<p>" + item["description"] + "</p><hr/>" \
          + "<b>Prix</b> : " + price + "<hr/>")
      rescue    
        require 'json'
        puts ">>>> ERROR >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n"
        puts JSON.pretty_generate(item)
        puts "\n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"
      end
      i.date = Time.now #item["date"]
    end
  end
end