url2png-dc 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use 1.9.2-p290 --create
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem 'httparty'
4
+
5
+ group :development do
6
+ gem 'minitest'
7
+ gem 'turn'
8
+
9
+ gem 'jeweler'
10
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,31 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ ansi (1.4.3)
5
+ git (1.2.5)
6
+ httparty (0.8.3)
7
+ multi_json (~> 1.0)
8
+ multi_xml
9
+ jeweler (1.8.4)
10
+ bundler (~> 1.0)
11
+ git (>= 1.2.5)
12
+ rake
13
+ rdoc
14
+ json (1.7.5)
15
+ minitest (3.3.0)
16
+ multi_json (1.3.6)
17
+ multi_xml (0.5.1)
18
+ rake (0.9.2.2)
19
+ rdoc (3.12)
20
+ json (~> 1.4)
21
+ turn (0.9.6)
22
+ ansi
23
+
24
+ PLATFORMS
25
+ ruby
26
+
27
+ DEPENDENCIES
28
+ httparty
29
+ jeweler
30
+ minitest
31
+ turn
data/Rakefile ADDED
@@ -0,0 +1,42 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "url2png-dc"
18
+ gem.homepage = "http://github.com/nowk/url2png-dc"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Url2Png Gem .v6}
21
+ gem.description = %Q{Gem to use Url2Png}
22
+ gem.email = "yung.kwon@damncarousel.com"
23
+ gem.authors = ["Yung Hwa Kwon"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rake/testtask'
29
+ Rake::TestTask.new(:test) do |test|
30
+ test.libs << 'lib' << 'test'
31
+ test.pattern = 'test/*_test.rb'
32
+ test.verbose = !!ENV["VERBOSE"]
33
+ test.warning = !!ENV["WARNING"]
34
+ end
35
+ Rake::TestTask.new('test:rl') do |t|
36
+ t.libs << 'lib' << 'test'
37
+ t.pattern = 'test/really/*_test.rb'
38
+ t.verbose = !!ENV["VERBOSE"]
39
+ t.warning = !!ENV["WARNING"]
40
+ end
41
+
42
+ task :default => :test
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,22 @@
1
+ module Url2PngDc
2
+ # TODO Rename this, "Cache" sounds a bit misleading in this context
3
+ #
4
+ class Cache
5
+ def initialize(json_payload)
6
+ @payload = json_payload
7
+ end
8
+
9
+ def url
10
+ parse_out_cache_url_from_payload
11
+ end
12
+
13
+
14
+ private
15
+
16
+ def parse_out_cache_url_from_payload
17
+ _json = JSON.parse(@payload)
18
+ _png = _json['png']
19
+ _png if _png && !_png.empty?
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,28 @@
1
+ module Url2PngDc
2
+ class Configuration
3
+ URL2PNG_API_URL = 'http://beta.url2png.com'
4
+ URL2PNG_API_VERSION = 'v6'
5
+
6
+ attr_accessor :url2png_apikey
7
+ attr_accessor :url2png_secret
8
+ attr_accessor :viewport_dimensions
9
+ attr_accessor :fullpage
10
+ attr_accessor :force_refresh
11
+
12
+ def initialize
13
+ @url2png_apikey = 'PXXXX'
14
+ @url2png_secret = 'SXXXX'
15
+ @viewport_dimensions = '1280x1024'
16
+ @fullpage = false
17
+ @force_refresh = false
18
+ end
19
+
20
+ def url2png_api_url
21
+ URL2PNG_API_URL
22
+ end
23
+
24
+ def url2png_api_version
25
+ URL2PNG_API_VERSION
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,79 @@
1
+ module Url2PngDc
2
+ module UrlHelpers
3
+ def self.included(base)
4
+ base.instance_eval do
5
+ include InstanceMethods
6
+ end
7
+ end
8
+
9
+ module InstanceMethods
10
+ def url_2png(url, options = {})
11
+ url_builder(query_string_builder(url, options))
12
+ end
13
+
14
+ def url_2json(url, options = {})
15
+ url_builder(query_string_builder(url, options), :json)
16
+ end
17
+
18
+ def url_2cache(url, options = {})
19
+ response = ::HTTParty.get(url_2json(url, options))
20
+ _cache = Cache.new response.body
21
+ _cache.url || url_2png(url, options)
22
+ end
23
+
24
+
25
+ private
26
+
27
+ def url2png_conf
28
+ Url2PngDc.configuration
29
+ end
30
+
31
+ def url_builder(query_string, url2png_api_format = :png)
32
+ File.join(
33
+ url2png_conf.url2png_api_url,
34
+ url2png_conf.url2png_api_version,
35
+ url2png_conf.url2png_apikey,
36
+ generate_url2png_token(query_string),
37
+ url2png_api_format.to_s,
38
+ "?#{query_string}"
39
+ )
40
+ end
41
+
42
+ def query_string_builder(url, options = {})
43
+ params = {
44
+ :url => url,
45
+
46
+ :force =>
47
+ int_my_bool(options[:force] || url2png_conf.force_refresh),
48
+ :fullpage =>
49
+ int_my_bool(options[:fullpage] || url2png_conf.fullpage),
50
+
51
+ :thumbnail_max_width => options[:thumbnail_max_width],
52
+ :thumbnail_max_height => options[:thumbnail_max_height],
53
+
54
+ :viewport =>
55
+ options[:viewport] || url2png_conf.viewport_dimensions
56
+ }
57
+
58
+ params.sort_by { |o| o[0] }.inject([]) { |memo, obj|
59
+ k, v = *obj.map(&:to_s)
60
+ memo << [CGI.escape(k), CGI.escape(v)].join("=") if v && !v.empty?
61
+ memo
62
+ }.join("&")
63
+ end
64
+
65
+ def generate_url2png_token(query_string)
66
+ raise ArgumentError, "Query string can't be blank" unless
67
+ query_string && !query_string.empty?
68
+
69
+ Digest::MD5.hexdigest(query_string+url2png_conf.url2png_secret)
70
+ end
71
+
72
+
73
+ def int_my_bool(val)
74
+ return val if val.is_a?(Fixnum)
75
+ val === true ? 1 : 0
76
+ end
77
+ end
78
+ end
79
+ end
data/lib/url2png_dc.rb ADDED
@@ -0,0 +1,24 @@
1
+ require 'digest'
2
+ require 'cgi'
3
+ require 'json'
4
+ require 'httparty'
5
+
6
+ require 'url2png/configuration'
7
+ require 'url2png/url_helper'
8
+ require 'url2png/cache'
9
+
10
+ module Url2PngDc
11
+ include HTTParty
12
+
13
+ class << self
14
+ def configuration
15
+ @_configuration ||= Configuration.new
16
+ end
17
+
18
+ def configure
19
+ yield(configuration)
20
+ configuration
21
+ end
22
+ end
23
+ end
24
+
@@ -0,0 +1,29 @@
1
+ require 'test_helper'
2
+
3
+ module Url2PngDc
4
+ class TestCache < MiniTest::Unit::TestCase
5
+ # This provides a solution to insert images properly for display on
6
+ # Microsoft Outlook email clients
7
+ #
8
+ def test_url_to_return_cached_url
9
+ cache = Cache.new json_payload
10
+ assert_match(/cache(-[\d]+)\.url2png\.com/, cache.url)
11
+ end
12
+
13
+ def test_url_returns_nil_if_png_key_is_empty_or_missing
14
+ json = JSON.parse(json_payload)
15
+ json['png'] = ''
16
+ cache = Cache.new json.to_json
17
+ assert_nil cache.url
18
+
19
+ json.delete('png')
20
+ cache = Cache.new json.to_json
21
+ assert_nil cache.url
22
+ end
23
+
24
+ def test_test_url_returns_nil_if_request_to_url2png_is_not_successful
25
+ flunk "We need to test this"
26
+ end
27
+ end
28
+ end
29
+
@@ -0,0 +1,19 @@
1
+ require 'test_helper'
2
+
3
+ module Url2PngDc
4
+ class TestConfiguration < MiniTest::Unit::TestCase
5
+ def test_configuration_defaults
6
+ configuration = Configuration.new
7
+
8
+ assert_equal 'PXXXX', configuration.url2png_apikey
9
+ assert_equal 'SXXXX', configuration.url2png_secret
10
+ assert_equal 'http://beta.url2png.com', configuration.url2png_api_url
11
+ assert_equal 'v6', configuration.url2png_api_version
12
+
13
+ assert_equal '1280x1024', configuration.viewport_dimensions
14
+ assert_equal false, configuration.fullpage
15
+ assert_equal false, configuration.force_refresh
16
+ end
17
+ end
18
+ end
19
+
@@ -0,0 +1,19 @@
1
+ require 'test_helper'
2
+
3
+ class TestReallyMakeCallToUrl2Png < MiniTest::Unit::TestCase
4
+ def teardown
5
+ Url2PngDc.instance_eval do
6
+ @_configuration = nil
7
+ end
8
+ end
9
+
10
+ def test_retrieving_an_actual_cache_url
11
+ Url2PngDc.configure do |config|
12
+ config.url2png_apikey = ENV['URL2PNG_APIKEY']
13
+ config.url2png_secret = ENV['URL2PNG_SECRET']
14
+ end
15
+
16
+ url = Sandbox.new.url_2cache "http://google.com"
17
+ assert_match /cache(-[\d]+)\.url2png\.com/, url
18
+ end
19
+ end
@@ -0,0 +1,22 @@
1
+ require 'rubygems'
2
+ gem 'minitest'
3
+
4
+ $:.push File.expand_path("../../lib", __FILE__)
5
+ require 'url2png_dc'
6
+
7
+ require 'turn'
8
+ require 'minitest/autorun'
9
+
10
+ def json_payload
11
+ # TODO pay attention to disclaimer
12
+ # TODO sample data comes from: http://beta.url2png.com/v6/P4DE5D1C99D8EF/c9d1c2e545d1280fb6c249aed4031c4b/json/?url=twitter.github.com%2Fbootstrap&viewport=1024x1024&thumbnail_max_width=275
13
+ <<-json
14
+ {"_disclaimer_":"This is a draft format is subject to change. Changes will be announced on twitter @url2png -- dan@url2png.com if you have suggestions or comments. Thanks!","status":"ok","job_id":"5ca8fac5a6c33f62bfb0f5d36a388e6219180414","request":{"submitted":"2012-08-31 21:18:52","viewport":"1024x1024","url":"http:\/\/twitter.github.com\/bootstrap","fullpage":false,"thumbnail_max_height":0,"thumbnail_max_width":275},"png":"http:\/\/cache-01.url2png.com\/P4DE5D1C99D8EF\/12244\/5ca8fac5a6c33f62bfb0f5d36a388e6219180414_1.png","meta":{"title":null,"dimensions_original":"1024x1976","dimensions_final":"1024x1976"},"headers":{"X-submitted":"1346447932","X-Original-Status":"HTTP\/1.1 301 Moved Permanently","Etag":"5ca8fac5a6c33f62bfb0f5d36a388e6219180414","X-Status":"OK"},"debug":["Curl Status is 0"]}
15
+ json
16
+ end
17
+
18
+
19
+ class Sandbox
20
+ include Url2PngDc::UrlHelpers
21
+ end
22
+
@@ -0,0 +1,46 @@
1
+ require 'test_helper'
2
+
3
+ class TestUrl2PngDc < MiniTest::Unit::TestCase
4
+ def teardown
5
+ Url2PngDc.instance_eval do
6
+ @_configuration = nil
7
+ end
8
+ end
9
+
10
+ def test_overriding_configuration_defaults
11
+ # must reset configuration on teardown, or it will act as configuration
12
+ # for test suite
13
+ #
14
+ Url2PngDc.configure do |config|
15
+ config.url2png_apikey = 'abcdefg'
16
+ config.url2png_secret = 'hijklmn'
17
+ config.viewport_dimensions = '1080x720'
18
+ config.fullpage = true
19
+ config.force_refresh = true
20
+ end
21
+
22
+ configuration = Url2PngDc.configuration
23
+
24
+ assert_equal 'abcdefg', configuration.url2png_apikey
25
+ assert_equal 'hijklmn', configuration.url2png_secret
26
+ assert_equal '1080x720', configuration.viewport_dimensions
27
+ assert_equal true, configuration.fullpage
28
+ assert_equal true, configuration.force_refresh
29
+ end
30
+
31
+ def test_cant_override_api_url
32
+ assert_raises NoMethodError do
33
+ Url2PngDc.configure do |config|
34
+ config.url2png_api_url = 'http://google.com'
35
+ end
36
+ end
37
+ end
38
+
39
+ def test_cant_override_api_version
40
+ assert_raises NoMethodError do
41
+ Url2PngDc.configure do |config|
42
+ config.url2png_api_version = 'v3'
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,141 @@
1
+ require 'test_helper'
2
+
3
+ module Url2PngDc
4
+ class TestUrlHelper < MiniTest::Unit::TestCase
5
+ def url2png_apikey
6
+ Url2PngDc.configuration.url2png_apikey
7
+ end
8
+
9
+ def a_url
10
+ "http://google.com/maps/"
11
+ end
12
+
13
+ def query_string_with_defaults
14
+ "force=0&fullpage=0&url=#{CGI.escape(a_url)}&viewport=1280x1024"
15
+ end
16
+
17
+ def token(query_string)
18
+ Digest::MD5.hexdigest(query_string+Url2PngDc.configuration.url2png_secret)
19
+ end
20
+
21
+ def mock_my_httparty!
22
+ # mocky
23
+ HTTParty.instance_eval do
24
+ def get(url)
25
+ obj = Object.new
26
+ def obj.body
27
+ json_payload
28
+ end
29
+ obj
30
+ end
31
+ end
32
+ end
33
+
34
+
35
+ def test_url_2png_generates_valid_url2png_url_for_png
36
+ token = token(query_string_with_defaults)
37
+ url = Sandbox.new.url_2png a_url
38
+
39
+ assert_equal "http://beta.url2png.com/v6/#{url2png_apikey}/#{token}/png/?#{query_string_with_defaults}", url
40
+ end
41
+
42
+ def test_url_2png_generates_valid_url2png_url_for_png_with_options
43
+ query_string_with_options =
44
+ "force=0&fullpage=1&url=#{CGI.escape(a_url)}&viewport=720x480"
45
+ token = token(query_string_with_options)
46
+ url = Sandbox.new.url_2png a_url, {:fullpage => true, :viewport => '720x480'}
47
+
48
+ assert_equal "http://beta.url2png.com/v6/#{url2png_apikey}/#{token}/png/?#{query_string_with_options}", url
49
+ end
50
+
51
+
52
+ def test_url_2json_generates_valid_url2png_url_for_json
53
+ token = token(query_string_with_defaults)
54
+ url = Sandbox.new.url_2json a_url
55
+
56
+ assert_equal "http://beta.url2png.com/v6/#{url2png_apikey}/#{token}/json/?#{query_string_with_defaults}", url
57
+ end
58
+
59
+ def test_url_2json_generates_valid_url2png_url_for_json_with_options
60
+ query_string_with_options =
61
+ "force=0&fullpage=1&url=#{CGI.escape(a_url)}&viewport=1080x720"
62
+ token = token(query_string_with_options)
63
+ url = Sandbox.new.url_2json a_url, {:fullpage => true, :viewport => '1080x720'}
64
+
65
+ assert_equal "http://beta.url2png.com/v6/#{url2png_apikey}/#{token}/json/?#{query_string_with_options}", url
66
+ end
67
+
68
+
69
+ def test_url_2cache_returns_a_url2png_cache_url
70
+ mock_my_httparty!
71
+
72
+ token = token(query_string_with_defaults)
73
+ url = Sandbox.new.url_2cache a_url
74
+ assert_match(/cache(-[\d]+)\.url2png\.com/, url)
75
+ end
76
+
77
+ def test_url_2cache_resolves_to_url_2png_if_cache_url_is_empty_or_nil
78
+ mock_my_httparty!
79
+
80
+ # mocky
81
+ Url2PngDc::Cache.class_eval do
82
+ alias_method :url_original, :url
83
+ def url
84
+ nil
85
+ end
86
+ end
87
+
88
+ resolved_url = Sandbox.new.url_2png a_url
89
+ url = Sandbox.new.url_2cache a_url
90
+
91
+ assert_match resolved_url, url
92
+
93
+ # put things back as they were
94
+ Url2PngDc::Cache.class_eval do
95
+ alias_method :url, :url_original
96
+ end
97
+ end
98
+
99
+
100
+ def test_query_string_builder_builds_query_string_with_configuration
101
+ query_string = Sandbox.new.__send__ :query_string_builder, a_url
102
+ assert_equal query_string_with_defaults, query_string
103
+ end
104
+
105
+ def test_query_string_builder_builds_query_string_override_params
106
+ expected_query_string =
107
+ "force=1&fullpage=1&thumbnail_max_height=300&thumbnail_max_width=400&url=#{CGI.escape(a_url)}&viewport=1080x720"
108
+
109
+ query_string = Sandbox.new.__send__ :query_string_builder, a_url, {
110
+ :force => true,
111
+ :fullpage => true,
112
+ :thumbnail_max_height => '300',
113
+ :thumbnail_max_width => '400',
114
+ :viewport => '1080x720'
115
+ }
116
+ assert_equal expected_query_string, query_string
117
+ end
118
+
119
+ def test_query_string_builder_ignores_non_related_options
120
+ query_string = Sandbox.new.__send__ :query_string_builder, a_url, {
121
+ :not_used_or_need_by_url2png => true
122
+ }
123
+ assert_equal query_string_with_defaults, query_string
124
+ end
125
+
126
+ def test_generate_url2png_token_generates_md5_token
127
+ expected_token = token(query_string_with_defaults)
128
+ query_string = Sandbox.new.__send__ :query_string_builder, a_url
129
+ token = Sandbox.new.__send__ :generate_url2png_token, query_string
130
+
131
+ assert_equal expected_token, token
132
+ end
133
+
134
+ def test_generate_url2png_token_must_have_query_string
135
+ assert_raises ArgumentError do
136
+ Sandbox.new.__send__ :generate_url2png_token, ''
137
+ end
138
+ end
139
+ end
140
+ end
141
+
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: url2png-dc
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Yung Hwa Kwon
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-09-01 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: httparty
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: minitest
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: turn
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: jeweler
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description: Gem to use Url2Png
79
+ email: yung.kwon@damncarousel.com
80
+ executables: []
81
+ extensions: []
82
+ extra_rdoc_files: []
83
+ files:
84
+ - .rvmrc
85
+ - Gemfile
86
+ - Gemfile.lock
87
+ - Rakefile
88
+ - VERSION
89
+ - lib/url2png/cache.rb
90
+ - lib/url2png/configuration.rb
91
+ - lib/url2png/url_helper.rb
92
+ - lib/url2png_dc.rb
93
+ - test/cache_test.rb
94
+ - test/configuration_test.rb
95
+ - test/really/make_call_to_url2png_test.rb
96
+ - test/test_helper.rb
97
+ - test/url2png_dc_test.rb
98
+ - test/url_helper_test.rb
99
+ homepage: http://github.com/nowk/url2png-dc
100
+ licenses:
101
+ - MIT
102
+ post_install_message:
103
+ rdoc_options: []
104
+ require_paths:
105
+ - lib
106
+ required_ruby_version: !ruby/object:Gem::Requirement
107
+ none: false
108
+ requirements:
109
+ - - ! '>='
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ segments:
113
+ - 0
114
+ hash: -110950005258160162
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ none: false
117
+ requirements:
118
+ - - ! '>='
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ requirements: []
122
+ rubyforge_project:
123
+ rubygems_version: 1.8.24
124
+ signing_key:
125
+ specification_version: 3
126
+ summary: Url2Png Gem .v6
127
+ test_files: []