class Morguefile
Constants
- VERSION
Attributes
key[RW]
secret[RW]
Public Class Methods
find_image(term)
click to toggle source
# File lib/morguefile.rb, line 11 def self.find_image(term) client = new(key || ENV.fetch('MORGUEFILE_KEY'), secret || ENV.fetch('MORGUEFILE_SECRET')) return unless result = client.search(term)['doc'].first result.fetch('file_path_large') end
new(key, secret)
click to toggle source
# File lib/morguefile.rb, line 17 def initialize(key, secret) @key = key @secret = secret end
Public Instance Methods
search(term)
click to toggle source
# File lib/morguefile.rb, line 22 def search(term) response = get("/image/json?terms=#{CGI.escape(term)}&sort=pop&af=morgueFile") JSON.parse(response) end
Private Instance Methods
get(path)
click to toggle source
# File lib/morguefile.rb, line 29 def get(path) signed = path.split('?').first.split("/").join("") signature = OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha256'), @key, signed) url = "http://morguefile.com#{path}&key=#{@key}&sig=#{signature}" open(url).read end