Class: AlleJest::Reader

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

Constant Summary collapse

END_POINT =
"http://allegro.pl/rss.php?"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Reader

Returns a new instance of Reader.



73
74
75
# File 'lib/allejest.rb', line 73

def initialize(config)
  self.config = config
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



71
72
73
# File 'lib/allejest.rb', line 71

def config
  @config
end

Class Method Details

.build_url(query = {}) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/allejest.rb', line 77

def self.build_url(query = {})
  return if query.blank?

  q = END_POINT.dup # dup, because q is appended to

  params = {}
  if query.has_key?(:text)
    params[:string] = query[:text]
    params[:feed] = "search"
    if !query.has_key?(:category)
      params[:category] = 0
    end
  end
  if query.has_key?(:category)
    if query.has_key?(:text)
      params[:category] = query[:category]
    else # Category only queries
      params[:feed] = "cat"
      params[:id] = query[:category]
    end
  end

  # Using CGI instead of URI, as CGI produced + for space instead of %20 (that's what we need!)
  # See http://www.listable.org/show/difference-between-cgiescape-and-uriescape-in-ruby
  params.each do |k,v|
    q << "#{k.to_s}=#{CGI.escape(v.to_s)}&"
  end

  return q[0, q.length - 1] if q[q.length - 1] == ?& # remove trailing '&' if exists
  q
end

Instance Method Details

#get_feed(query) ⇒ Object

build_url



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

def get_feed(query)
  url = AlleJest::Reader.build_url(query)
  content = ""
  open(url) {|s| content = s.read }
  FeedMe.parse(content)
end

#map_feeds_to_queriesObject



116
117
118
119
120
121
122
# File 'lib/allejest.rb', line 116

def map_feeds_to_queries
  return nil if config.nil? || config[:queries].nil?

  config[:queries].map do |query|
    [query, get_feed(query)]
  end
end