Class: RatingsAggregator::Base

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

Instance Method Summary collapse

Instance Method Details

#apiObject



32
33
34
# File 'lib/ratings_aggregator/base.rb', line 32

def api
  @api ||= RatingsAggregator::Api.new
end

#search(search_title) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/ratings_aggregator/base.rb', line 7

def search(search_title)
  begin
    response = api.call(t: search_title, apikey: @key)
  rescue Exception => e
    puts "Please set the API key"
    return
  end
  # puts response
  if response[:data]["Response"] == "False"
    nil
  else
    # pulls out the strings that represent the rating scores
    rating_scores = response[:data]["Ratings"].map {|x| x["Value"]}

    sum = 0.0
    rating_scores.each do |score|
      to_add = score.to_f
      if /.+\/10$/.match score
        to_add *= 10
      end
      sum += to_add
    end
    aggregate_score = (sum/rating_scores.length).round
  end
end

#set_key(key) ⇒ Object



36
37
38
# File 'lib/ratings_aggregator/base.rb', line 36

def set_key(key)
  @key = key
end