class Flickrage::Service::Search

Constants

FLICKR_SIZES

Attributes

search_params[R]

Public Class Methods

new() click to toggle source
# File lib/flickrage/service/search.rb, line 11
def initialize
  @tagged_search = Flickrage.config.tagged_search
  @search_params = Flickrage.config.search_params
end

Public Instance Methods

run(keyword) click to toggle source
# File lib/flickrage/service/search.rb, line 16
def run(keyword)
  result = search(keyword)

  return if result.size < 1

  image(result.first, keyword)
rescue StandardError => e
  logger.debug(e)
  nil
end

Private Instance Methods

get_image_size(result) click to toggle source
# File lib/flickrage/service/search.rb, line 52
def get_image_size(result)
  FLICKR_SIZES.detect { |t| result.respond_to?(:"url_#{t}") }
end
image(result, keyword) click to toggle source
# File lib/flickrage/service/search.rb, line 29
def image(result, keyword)
  size = get_image_size(result)
  return unless size

  Flickrage::Entity::Image.new(
    id:      result.id,
    title:   title(result.title),
    keyword: keyword,
    url:     result.send(:"url_#{size}"),
    width:   result.send(:"width_#{size}"),
    height:  result.send(:"height_#{size}"),
  )
end
params(opts = {}) click to toggle source
# File lib/flickrage/service/search.rb, line 56
def params(opts = {})
  {
    extras: 'url_m, url_z, url_c, url_l, url_o',
    sort: 'interestingness-desc',
    per_page: 1,
    pages: 1,
    media: 'photos',
    accuracy: 1
  }.merge(opts).merge(search_params)
end
search_query(keyword) click to toggle source
# File lib/flickrage/service/search.rb, line 67
def search_query(keyword)
  tagged_search ? {tags: [keyword]} : {text: keyword}
end
title(text) click to toggle source
# File lib/flickrage/service/search.rb, line 47
def title(text)
  return text if text.nil? || text.size < 50
  text[0..50] + '...'
end