Class: RatingsAggregator::Base

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

Instance Method Summary collapse

Instance Method Details

#apiObject



27
28
29
# File 'lib/ratings_aggregator/base.rb', line 27

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
# File 'lib/ratings_aggregator/base.rb', line 7

def search(search_title)
  response = api.call(t: search_title, apikey: @key)
  # 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



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

def set_key(key)
  @key = key
end