summaryrefslogtreecommitdiff
path: root/app/models/search.rb
blob: 6ef04840997638088459391b179098c284fe351a (plain)
1
2
3
4
5
6
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
class Search
  class Yelp
    attr_reader :client

    def initialize(client = ::Yelp.client)
      @client = client
    end

    def for(q, city, categories = [], page = 1, per_page = 20, &block)
      return paginate([]) if city.blank?

      cache(key: key_for(q, city, page, per_page)) do
        paginate(results_for(q, city, categories, page, per_page).map(&block))
      end
    end

    def for_business(yelp_id)
      client.business(yelp_id).try(:business)
    end

    private

    def key_for(*args)
      args.join("-")
    end

    def results_for(q, city, categories, page, per_page)
      client.search(
        city,
        category_filter: categories.join(","),
        limit: per_page,
        offset: offset_for(page, per_page),
        term: q,
      ).businesses
    end

    def paginate(results)
      Kaminari.paginate_array(results)
    end

    def cache(key:)
      Rails.cache.fetch(key, expires_in: 1.hour) do
        yield
      end
    end

    def offset_for(page, per_page)
      (page * per_page) - per_page
    end
  end

  def self.yelp
    @yelp ||= Yelp.new
  end
end