Sha256: 1af1e5fbf1ccb78a2f6e85524679a6d33e618fb52b14f9956bc775b70589f5a0

Contents?: true

Size: 1.73 KB

Versions: 2

Compression:

Stored size: 1.73 KB

Contents

$:.unshift(File.dirname(__FILE__)) unless $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))

gem 'relax' #, '~>0.1.0'
require 'relax'

require 'page_glimpse/api'

module PageGlimpse
  
  ##
  # Sets the developer key to use with the requests.
  # 
  def self.developer_key=(key)
    @@developer_key = key
  end
  
  ##
  # Returns +true+ if the thumbnail exists on Page Glimpse, +false+ otherwise.
  # 
  def self.exist?(url, options = {})
    options[:url] = url
    response = api.exist?(options)
    response.kind_of?(Array) && response.size == 2 && response[1] == API::THUMBNAIL_EXISTS
  rescue RestClient::ResourceNotFound
    return false
  rescue RestClient::RequestFailed
    handle_failure($!)
  end
  
  ##
  # Returns the binary data for the thumbnail requested.
  # 
  def self.get(url, options = {})
    options[:url] = url
    exist?(url, options) ? api.thumbnail(options) : nil
  rescue RestClient::RequestFailed
    handle_failure($!)
  end
  
  ##
  # Instructs Page Glimpse to enqueue the thumbnailing of a specific URL.
  # 
  def self.queue(url)
    response = api.queue(:url => url)
    response.kind_of?(Array) && response.size == 2 && response[1] == API::QUEUE_SUCCESS
  rescue RestClient::RequestFailed
    handle_failure($!)
  end
  
  
  private
  
  
  def self.developer_key #:nodoc:
    @@developer_key
  end
  
  def self.api #:nodoc:
    @@api ||= API.new(:devkey => self.developer_key)
  end
  
  def self.handle_failure(exception)
    raise exception unless exception.respond_to?(:http_code)
    case exception.http_code
    when 403
      raise InvalidDeveloperKeyError, "Check your developer key: it was either mistyped or has been invalidated"
    else
      raise exception
    end
  end
  
end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
nbibler-page_glimpse-0.0.2 lib/page_glimpse.rb
page_glimpse-0.0.2 lib/page_glimpse.rb