Class: Tinplate::TinEye

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

Constant Summary collapse

SORTS =
["score", "size", "crawl_date"]
ORDERS =
["asc", "desc"]

Instance Method Summary collapse

Instance Method Details

#image_countObject



36
37
38
# File 'lib/tinplate/tineye.rb', line 36

def image_count
  request("image_count")["results"]
end

#remaining_searchesObject



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/tinplate/tineye.rb', line 24

def remaining_searches
  results = request("remaining_searches")["results"]

  bundles = results["bundles"].map do |bundle|
    OpenStruct.new(remaining_searches: bundle["remaining_searches"],
                   start_date:  Time.parse(bundle["start_date"]),
                   expire_date: Time.parse(bundle["expire_date"]))
  end

  OpenStruct.new(total_remaining_searches: results["total_remaining_searches"], bundles: bundles)
end

#search(image_path: nil, image_url: nil, offset: 0, limit: 100, sort: "score", order: "desc") ⇒ Object

Raises:

  • (ArgumentError)


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/tinplate/tineye.rb', line 6

def search(image_path: nil, image_url: nil, offset: 0, limit: 100, sort: "score", order: "desc")
  raise ArgumentError.new("You must supply an image or image_url") if !image_url && !image_path
  raise ArgumentError.new("sort must be one of #{SORTS.join(', ')}") if !SORTS.include?(sort)
  raise ArgumentError.new("order must be one of #{ORDERS.join(', ')}") if !ORDERS.include?(order)

  options = {
    offset: offset.to_s,
    limit: limit.to_s,
    sort: sort,
    order: order
  }
  img = image_url ? { image_url: image_url } : { image_path: image_path }

  response = request("search", options.merge(img))

  Tinplate::SearchResults.new(response)
end