class GcsApiGem::SearchApi

SearchApi

Constants

SEARCH_RANGE

Decide to what range of page you want to acquire

Public Class Methods

new() click to toggle source
# File lib/gcs_api_gem/search_api.rb, line 7
def initialize
  @items = []
  @search_result = nil
  @keyword = nil
  @start_index = nil
end

Public Instance Methods

random_image(keyword) click to toggle source

Select one piece randomly from images searched by keyword

# File lib/gcs_api_gem/search_api.rb, line 15
def random_image(keyword)
  return { error: 'Please set KEY to environment variable.' } if key_is_ng?
  return { error: 'Please set CSE_ID to environment variable.' } if cse_id_is_ng?

  choice(keyword)
end

Private Instance Methods

blank?(obj) click to toggle source
# File lib/gcs_api_gem/search_api.rb, line 87
def blank?(obj)
  obj.nil? || obj.empty?
end
check_error(res) click to toggle source
# File lib/gcs_api_gem/search_api.rb, line 73
def check_error(res)
  raise res['error'].to_s unless blank?(res['error'])
  raise 'Search result was 0.' if count_zero?(res)
end
choice(keyword) click to toggle source
# File lib/gcs_api_gem/search_api.rb, line 53
def choice(keyword)
  if @keyword != keyword
    @keyword = keyword
    pull_data
  end
  formatted_item(select_item)
rescue => e
  return { error: e.message }
end
count_zero?(res) click to toggle source
# File lib/gcs_api_gem/search_api.rb, line 101
def count_zero?(res)
  res['searchInformation']['totalResults'].to_i.zero?
end
cse_id_is_ng?() click to toggle source
# File lib/gcs_api_gem/search_api.rb, line 28
def cse_id_is_ng?
  blank?(ENV['CSE_ID'])
end
fetch(keyword) click to toggle source
# File lib/gcs_api_gem/search_api.rb, line 47
def fetch(keyword)
  client = HTTPClient.new
  res = client.get(url, query(keyword))
  JSON.parse res.body
end
formatted_item(item) click to toggle source
# File lib/gcs_api_gem/search_api.rb, line 91
def formatted_item(item)
  {
    keyword: @keyword,
    title: item['title'],
    image: item['link'],
    search_result: @search_result,
    start_index: @start_index
  }
end
key_is_ng?() click to toggle source
# File lib/gcs_api_gem/search_api.rb, line 24
def key_is_ng?
  blank?(ENV['KEY'])
end
pull_data() click to toggle source
# File lib/gcs_api_gem/search_api.rb, line 63
def pull_data
  p 'do pull_data'
  res = fetch(@keyword)
  check_error(res)

  @items = res['items']
  @search_result = res['searchInformation']['totalResults'].to_i
  @start_index = res['queries']['request'][0]['startIndex']
end
query(keyword) click to toggle source
# File lib/gcs_api_gem/search_api.rb, line 36
def query(keyword)
  {
    'key' => ENV['KEY'],
    'cx' => ENV['CSE_ID'],
    'searchType' => 'image',
    'q' => keyword,
    'start' => rand(SEARCH_RANGE) + 1,
    'imgType' => 'photo'
  }
end
select_item() click to toggle source
# File lib/gcs_api_gem/search_api.rb, line 78
def select_item
  pull_data if @items.count.zero?

  index = [*(0..@items.count - 1)].sample
  item = @items[index]
  @items.delete_at(index)
  item
end
url() click to toggle source
# File lib/gcs_api_gem/search_api.rb, line 32
def url
  'https://www.googleapis.com/customsearch/v1'
end