DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone >

Ruby Library For Web Site Thumbnailer Service

Snippets Manager user avatar by
Snippets Manager
·
Mar. 03, 07 · · Code Snippet
Like (0)
Save
Tweet
588 Views

Join the DZone community and get the full member experience.

Join For Free
Today I found a really cool and flexible Web page thumbnailing service at http://bluga.net/webthumb/ with a nice API.. so I wrote a Ruby library for it. It works well. The service lets you do 250 free requests per month, but over that the price seems very reasonable (compared with the dire alternatives I found).

require 'net/http'
require 'rubygems'
require 'xmlsimple'

class Nailer

  @@api_baseurl = 'http://webthumb.bluga.net/api.php'
  @@api_key = 'PUT YOUR API KEY HERE'
  
  attr_accessor :collection_time, :job_id, :ok

  def initialize(url, width = 1024, height = 768)
    api_request = %Q{#{@@api_key}#{url}#{width}#{height}}

    result = do_request(api_request)

    if result.class == Net::HTTPOK
      result_data = XmlSimple.xml_in(result.body)
      @job_id = result_data['jobs'].first['job'].first['content']
      @collection_time = Time.now.to_i + result_data['jobs'].first['job'].first['estimate'].to_i
      @ok = true
    else
      @ok = false
    end
  end

  def retrieve(size = :small)
    api_request = %Q{#{@@api_key}#{@job_id}#{size.to_s}}
    result = do_request(api_request)
    result.body
  end

  def retrieve_to_file(filename, size = :small)
    File.new(filename, 'w+').write(retrieve(size.to_s))
  end

  def ready?
    return unless Time.now.to_i >= @collection_time

    api_request = %Q{#{@@api_key}#{@job_id}}
    result = do_request(api_request)

    if result.class == Net::HTTPOK
      @ok = true
      result_data = XmlSimple.xml_in(result.body)
      begin
        @result_url = result_data['jobStatus'].first['status'].first['pickup']
        @completion_time = result_data['jobStatus'].first['status'].first['completionTime']
      rescue
        @collection_time += 60 
	      return false
      end
    else
      @ok = false
    end

    true
  end

  def ok?
    @ok == true
  end

  def wait_until_ready
    sleep 1 until ready?
  end

  private

  def do_request(body)
    api_url = URI.parse(@@api_baseurl)
    request = Net::HTTP::Post.new(api_url.path)
    request.body = body
    Net::HTTP.new(api_url.host, api_url.port).start {|h| h.request(request) }
  end
end
  


url = 'http://www.rubyinside.com/'
t = Nailer.new(url)

if t.ok?
  t.wait_until_ready
  t.retrieve_to_file('out1.jpg', :small)
  t.retrieve_to_file('out2.jpg', :medium)
  t.retrieve_to_file('out3.jpg', :medium2)
  t.retrieve_to_file('out4.jpg', :large)
  puts "Thumbnails saved"
else
  puts "Error"
end
Web Service Library

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Choosing Between GraphQL Vs REST
  • Memory Debugging and Watch Annotations
  • Top Six Kubernetes Best Practices for Fleet Management
  • Modernize Legacy Code in Production: Rebuild Your Airplane Midflight Without Crashing

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo