Module: Peekapp::Ratings

Defined in:
lib/peekapp/ratings.rb

Class Method Summary collapse

Class Method Details

.from_app(id, stores, options = {}) ⇒ Object

{{{



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/peekapp/ratings.rb', line 5

def self.from_app id, stores, options = {} # {{{
  rating = Array.new
  stores.each do |s|
    rating << parse({
      :dom => Peekapp::query({
        :url => $peekapp_config[:ratings_url],
        :page => 1,
        :app_id => id,
        :store_id => s,
        :app_version => (options[:app_version] ? options[:app_version] : "all")
      }),
      :store_id => s
    })
  end
  rating
end

.parse(data) ⇒ Object

}}}



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/peekapp/ratings.rb', line 22

def self.parse data # {{{
  rating = Rating.new :store_id => data[:store_id]
  dom = Nokogiri::HTML.parse(data[:dom])
  nb_ratings_section = dom.css("div.ratings-histogram").count

  dom.css("div.ratings-histogram").each_with_index do |r,i|
    data = Hash.new
    result = true
    r.css("div.vote").each_with_index{ |v,j| data.merge!({(5-j) => v.css("span.total").children.to_s.to_i }) }

    if nb_ratings_section === 1 or i === 1
      rating.set :key => :all, :value => data
      rating.set :key => :current, :value => data if nb_ratings_section === 1
    else
      rating.set :key => :current, :value => data
    end
  end

  rating.set :key => :all, :value => {1 => 0, 2 => 0, 3 => 0, 4 => 0, 5 => 0} if nb_ratings_section < 1
  rating
end