Skip to content

Commit

Permalink
switched to version 4 of the API
Browse files Browse the repository at this point in the history
  • Loading branch information
wout fierens committed Dec 1, 2011
1 parent 89bd1f9 commit d369b2c
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 15 deletions.
2 changes: 1 addition & 1 deletion lib/url2png/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def self.api_url protocol
end

def self.api_version
@url2png_version ||= 'v3'
@url2png_version ||= 'v4'
end

# public key setter and getter
Expand Down
17 changes: 12 additions & 5 deletions lib/url2png/dimensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@ module Url2png
module Dimensions

def self.parse options
size = options[:size] || Url2png::Config.default_size

# convert tumbnail to size to ensure compatibility with older gem versions
options[:size] ||= options[:thumbnail]

# distill image size
size = (options[:size] || Url2png::Config.default_size).split('x')
width = options[:width] || size[0]
height = options[:height] || size[1]

# return dimensions hash
{
:size => size,
:width => options[:width] || size.split('x')[0],
:height => options[:height] || size.split('x')[1]
:size => "#{ width }x#{ height }",
:width => width,
:height => height
}
end

Expand Down
41 changes: 34 additions & 7 deletions lib/url2png/helpers/common.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,36 @@
module Url2png
module Helpers
module Common


# complete image tag
def site_image_tag url, options = {}
# parse size
dim = Url2png::Dimensions.parse(options)


# ensure image alt
alt = options.key?(:alt) ? options.delete(:alt) : url

# filter options
url2png_options = {}
[:size, :thumbnail, :browser_size, :delay, :fullscreen].each do |key|
url2png_options[key] = options.delete(key) if options.key?(key)
end

# build image tag
img = '<img'
img << " src='#{ site_image_url(url, options) }'"
img << " alt='#{ options[:alt] || url }'"
img << " src='#{ site_image_url(url, url2png_options) }'"
img << " alt='#{ alt }'"
img << " width='#{ dim[:width] }'"
img << " height='#{ dim[:height] }'"
options.each_pair do |k, v|
img << " #{ k }='#{ v }'" unless v.nil? || v == ''
end
img << ' />'
img.html_safe
end

# only the url for the image
def site_image_url url, options = {}
# parse size
dim = Url2png::Dimensions.parse(options)

case Url2png::Config.mode
Expand Down Expand Up @@ -60,12 +72,27 @@ def site_image_url url, options = {}
# generate token
token = Digest::MD5.hexdigest("#{ Url2png::Config.shared_secret }+#{ safe_url }")

# build options portion of URL
url_options = []
url_options << "t#{ dim[:size] }" if dim[:size]
url_options << "s#{ options[:browser_size] }" if options[:browser_size]
url_options << "d#{ options[:delay] }" if options[:delay]
url_options << "FULL" if options[:fullscreen]
url_options_string = url_options.join('-')

# build image url
File.join(Url2png::Config.api_url(options[:protocol]), Url2png::Config.api_version, Url2png::Config.public_key, token, dim[:size], safe_url)
File.join(
Url2png::Config.api_url(options[:protocol]),
Url2png::Config.api_version,
Url2png::Config.public_key,
token,
url_options_string,
safe_url
)
end

end

end
end
end
30 changes: 28 additions & 2 deletions spec/helpers/common_rspec.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,38 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
require File.expand_path(File.dirname(__FILE__) + '/../../lib/url2png/helpers/common.rb')
require 'spec_helper'
require 'url2png/helpers/common'

describe Url2png do
include Url2png::Helpers::Common
before(:all) do
Url2png::Config.public_key = "P4DBEC0200EE98"
Url2png::Config.shared_secret = "S169B0E051EAC6794B3EC9F385866187R"
end

describe "site_image_tag" do
it "should return the url of the http://pasparout.com image" do
site_image_tag('http://pasparout.com').should_not be_nil
end
end

describe "site_image_url" do
it "should encode the thumbnail size when provided" do
site_image_url('http://www.nytimes.com/', :thumbnail => '200x300').should =~ %r{t200x300}
end

it "should encode the initial browser size when provided" do
site_image_url('http://www.nytimes.com/', :browser_size => '200x300').should =~ %r{s200x300}
end

it "should encode the delay when provided" do
site_image_url('http://www.nytimes.com/', :delay => 3).should =~ %r{d3}
end

it "should encode the fullscreen capture flag when provided" do
site_image_url('http://www.nytimes.com/', :fullscreen => true).should =~ %r{FULL}
end

it "should concatenate options by using hyphens" do
site_image_url('http://www.nytimes.com/', :delay => 3, :fullscreen => true).should =~ %r{d3-FULL}
end
end
end

0 comments on commit d369b2c

Please sign in to comment.