url2png 0.2.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -24,7 +24,7 @@ You must define your public key and shared secret, they are required:
24
24
  ##### version
25
25
  You can define the api version if you want to use an different version of the url2png api.
26
26
 
27
- Url2png.api_version = "v4"
27
+ Url2png.api_version = 'v4'
28
28
  g
29
29
  Available versions:
30
30
 
@@ -40,9 +40,16 @@ This is done by setting the mode:
40
40
 
41
41
  Options are:
42
42
 
43
- * :production (default; will use the url2png api to generate images)
44
- * :placehold (will generate images at http://placehold.it)
45
- * :dummy (will give a grey base64 data image)
43
+ * production (default; will use the url2png api to generate images)
44
+ * placehold (will generate images at http://placehold.it)
45
+ * dummy (will give a grey base64 data image)
46
+
47
+ #### default_size
48
+ Default size is set to 400x400. To change the default size:
49
+
50
+ Url2png.default_size = '500x500'
51
+
52
+ <br/>
46
53
 
47
54
  In Rails you probably want to do configuration in an initializer.
48
55
 
@@ -51,19 +58,26 @@ In Rails you probably want to do configuration in an initializer.
51
58
 
52
59
  Generate an image tag:
53
60
 
54
- url2png_image_tag [options]
61
+ url2png_image_tag url, [options]
62
+
63
+ # alias (older version)
64
+ site_image_tag url, [options]
65
+
66
+ To only get the image url:
55
67
 
68
+ site_image_url url, [options]
56
69
 
57
70
  Options differ by version!
58
71
 
59
72
 
73
+
60
74
  ### Version 6
61
75
 
62
76
  ##### Thumbnail
63
77
  Constrain screenshot based on width or height or both.<br>
64
78
  default: no resizing
65
79
 
66
- url2png_image_tag :url => 'http://www.zwartopwit.com',
80
+ url2png_image_tag 'http://www.zwartopwit.com',
67
81
  :thumbnail_max_width => 500,
68
82
  :thumbnail_max_height => 500
69
83
 
@@ -72,27 +86,28 @@ default: no resizing
72
86
  Set viewport dimensions, adjust to your hearts content.<br>
73
87
  default: 1480x1037
74
88
 
75
- url2png_image_tag :url => 'http://www.zwartopwit.com',
76
- :viewport => "1024x900"
89
+ url2png_image_tag 'http://www.zwartopwit.com',
90
+ :viewport => '1024x900'
77
91
 
78
92
 
79
93
  ##### Fullpage
80
94
  Will attempt to capture entire document canvas.<br>
81
95
  default: false
82
96
 
83
- url2png_image_tag :url => 'http://www.zwartopwit.com',
97
+ url2png_image_tag 'http://www.zwartopwit.com',
84
98
  :fullpage => true
85
99
 
86
100
 
87
101
  ##### Delay
88
102
  Extra delay (in seconds) forced between page load and screenshot.<br>
89
103
  default: 1<br>
90
- min: 1<br>
91
- max: 5<br>
104
+
105
+ * min: 1<br>
106
+ * max: 5<br>
92
107
 
93
108
  example:
94
109
 
95
- url2png_image_tag :url => "http://www.zwartopwit.com",
110
+ url2png_image_tag 'http://www.zwartopwit.com',
96
111
  :size => '300x200',
97
112
  :delay => 2
98
113
 
@@ -101,74 +116,90 @@ example:
101
116
  Forces a fresh screenshot with each request, overwriting the previous copy.<br>
102
117
  **You will be charged for every request made using the force option.**
103
118
 
104
- = url2png_image_tag :url => 'http://www.zwartopwit.com',
119
+ url2png_image_tag 'http://www.zwartopwit.com',
105
120
  :force => true
106
121
 
107
122
 
108
123
  ### Version 4
109
124
 
110
- By default the size is set to 400 x 400 px.
111
- To generate an image with a specific size:
112
125
 
113
- url2png_image_tag :url => "http://www.zwartopwit.com",
114
- :size => '300x200'
115
-
116
- To only get the image url:
117
-
118
- site_image_url :url => "http://www.zwartopwit.com",
119
- :size => '300x200'
126
+ ##### Size
127
+ By default the size is set to 400 x 400 px.
120
128
 
121
- Options are:
129
+ This is a proportion bounding box. Thumbnails will be resized to fit within this box.
122
130
 
123
- ##### Size
124
- This is a proportion bounding box.<br>
125
- Thumbnails will be resized to fit within this box.<br>
126
131
  default: 'ORIGINAL'<br>
132
+
127
133
  examples:
128
134
 
129
- url2png_image_tag :url => "http://www.zwartopwit.com",
135
+ url2png_image_tag 'http://www.zwartopwit.com',
130
136
  :size => '500x500'
131
137
 
132
- url2png_image_tag :url => "http://www.zwartopwit.com",
138
+ url2png_image_tag 'http://www.zwartopwit.com',
133
139
  :size => 'ORIGINAL'
134
140
 
135
141
  ##### Thumbnail (alias for :size)
136
142
 
137
143
  ##### Browser size
138
144
  Set the initial browser screen size.<br>
139
- default: '1024x768'<br>
140
- min: '200x200'<br>
141
- max: '4000x4000'<br>
145
+
146
+ * default: '1024x768'<br>
147
+ * min: '200x200'<br>
148
+ * max: '4000x4000'<br>
149
+
142
150
  example:
143
151
 
144
- url2png_image_tag :url => "http://www.zwartopwit.com",
152
+ url2png_image_tag 'http://www.zwartopwit.com',
145
153
  :size => '300x200',
146
154
  :browser_size => '1024x2500'
147
155
 
148
156
  ##### Delay
149
157
  Extra delay (in seconds) forced between page load and screenshot.<br>
150
- default: 1<br>
151
- min: 1<br>
152
- max: 5<br>
158
+
159
+ * default: 1
160
+ * min: 1
161
+ * max: 5
162
+
153
163
  example:
154
164
 
155
- url2png_image_tag :url => "http://www.zwartopwit.com",
165
+ url2png_image_tag 'http://www.zwartopwit.com',
156
166
  :size => '300x200',
157
167
  :delay => 2
158
168
 
159
169
  ##### Fullscreen
160
170
  When true, Will attempt to capture entire document canvas.<br>
161
- Will never return screenshot smaller than "Initial Screen Size".<br>
171
+ Will never return screenshot smaller than 'Initial Screen Size'.<br>
162
172
  default: false<br>
163
173
 
164
174
  example:
165
175
 
166
- url2png_image_tag :url => "http://www.zwartopwit.com",
176
+ url2png_image_tag 'http://www.zwartopwit.com',
167
177
  :size => '300x200',
168
178
  :fullscreen => true
169
179
 
170
180
 
181
+ ### Version 3
182
+
183
+ ##### Size
184
+
185
+ By default the size is set to 400 x 400 px.
186
+ To generate an image with a specific size:
171
187
 
188
+ url2png_image_tag 'http://www.zwartopwit.com',
189
+ :size => '300x200'
190
+
191
+
192
+ ##### Fullscreen
193
+ When true, Will attempt to capture entire document canvas.<br>
194
+ Will never return screenshot smaller than 'Initial Screen Size'.<br>
195
+ default: false<br>
196
+
197
+ example:
198
+
199
+ url2png_image_tag 'http://www.zwartopwit.com',
200
+ :fullscreen => true
201
+
202
+
172
203
  ## Important
173
204
 
174
205
  This gem is written for Rails 3.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.2
1
+ 0.3.0
@@ -5,49 +5,64 @@ module Url2png
5
5
  MODES = %w{production placehold dummy}
6
6
 
7
7
  def config c = {}
8
- # SET
9
- # api_key
10
- # private_key
11
- # mode (production[default] placehold dummy)
12
- # version (v6[default])
8
+ # mandatory
9
+ self.api_key = c[:api_key]
10
+ self.private_key = c[:private_key]
13
11
 
14
- c.each do |key, value|
15
- case key
16
-
17
- when :api_key
18
- @api_key = value
19
- when :private_key
20
- @private_key = value
21
- when :mode
22
- raise "Url2png error: Invalid mode, only #{ MODES.join(', ') } are allowed" unless MODES.include?(value.to_s)
23
- @mode = value
24
- when :api_version
25
- @api_version = value
26
- end
27
- end
12
+ # optional
13
+ self.mode = c[:mode] if c[:mode]
14
+ self.api_version = c[:api_version] if c[:api_version]
15
+ end
16
+
17
+ def api_key=api_key
18
+ @api_key=api_key
28
19
  end
29
20
 
30
-
31
- # GETTERS
32
21
  def api_key
33
22
  raise 'Url2png error: No public key defined!' if @api_key.nil?
34
23
  @api_key
35
24
  end
25
+
26
+ def private_key=private_key
27
+ @private_key = private_key
28
+ end
36
29
 
37
30
  def private_key
38
31
  raise 'Url2png error: No private key defined!' if @private_key.nil?
39
32
  @private_key
40
33
  end
34
+
35
+ def mode=mode
36
+ raise "Url2png error: Invalid mode, only #{ MODES.join(', ') } are allowed" unless MODES.include?(mode.to_s)
37
+ @mode = mode
38
+ end
41
39
 
42
40
  def mode
43
41
  @mode ||= 'production' # default: production
44
42
  end
45
43
 
44
+ def api_version=api_version
45
+ @api_version = api_version || 'v4'
46
+ end
47
+
46
48
  def api_version
47
- @api_version ||= 'v4' #default: v4
49
+ @api_version || 'v4' #default: v4
50
+ end
51
+
52
+ def default_size=default_size
53
+ @default_size = default_size || "400x400"
48
54
  end
49
55
 
50
56
  def default_size
51
- @default_size ||= "400x400"
57
+ @default_size || "400x400"
58
+ end
59
+
60
+ def token param
61
+ case self.api_version
62
+ when 'v6'
63
+ Digest::MD5.hexdigest("#{param}#{self.private_key}")
64
+ when 'v4', 'v3'
65
+ Digest::MD5.hexdigest("#{self.private_key}+#{param}")
66
+ end
52
67
  end
53
68
  end
@@ -2,7 +2,9 @@ module Url2png
2
2
  module Dimensions
3
3
 
4
4
  def self.parse options
5
- options[:size] ||= "#{options[:thumbnail_max_width]}x#{options[:thumbnail_max_height]}"
5
+ if Url2png.api_version == "v6"
6
+ options[:size] ||= "#{options[:thumbnail_max_width]}x#{options[:thumbnail_max_height]}"
7
+ end
6
8
 
7
9
  # distill image size
8
10
  size = (options[:size] || Url2png.default_size).split('x')
@@ -2,20 +2,21 @@ module Url2png
2
2
  module Helpers
3
3
  module Common
4
4
  extend self
5
-
5
+
6
6
  # ------------------
7
7
  # complete image tag
8
8
 
9
- def url2png_image_tag options = {}
9
+
10
+ def url2png_image_tag url, options = {}
10
11
  # parse size
11
12
  dim = Url2png::Dimensions.parse(options)
12
13
 
13
14
  # ensure image alt
14
- alt = options.key?(:alt) ? options.delete(:alt) : options[:url]
15
+ alt = options.key?(:alt) ? options.delete(:alt) : url
15
16
 
16
17
  # build image tag
17
18
  img = '<img'
18
- img << " src='#{ site_image_url(options) }'"
19
+ img << " src='#{ site_image_url(url, options) }'"
19
20
  img << " alt='#{ alt }'"
20
21
  img << " width='#{ dim[:width] }'" if options[:size]
21
22
  img << " height='#{ dim[:height] }'" if options[:size]
@@ -28,20 +29,22 @@ module Url2png
28
29
 
29
30
 
30
31
  def check_options options, options_available
32
+
31
33
  # filter out unavailable options
32
34
  options = options.select do |key, value|
33
35
  if options_available.include? key
34
36
  true
35
37
  else
36
38
  # size is a special option, only usable in the gem
37
- warn "\"#{key}\" is not a valid option" unless key == :size
39
+ homepage = "https://github.com/robinhoudmeyers/url2png-gem"
40
+ raise "\"#{key}\" is not a valid option \nCheck the gem homepage for information about options: #{homepage}" unless key == :size
38
41
  end
39
42
  end
40
43
  end
41
44
 
42
45
  # --------------------------
43
46
  # only the url for the image
44
- def site_image_url options = {}
47
+ def site_image_url url, options = {}
45
48
  # parse size
46
49
  dim = Url2png::Dimensions.parse(options)
47
50
 
@@ -86,17 +89,20 @@ module Url2png
86
89
  ######
87
90
 
88
91
  # check for unavailable options
89
- options_available = [:url, :force, :fullpage, :thumbnail_max_width, :thumbnail_max_height, :viewport]
92
+ options_available = [:force, :fullpage, :thumbnail_max_width, :thumbnail_max_height, :viewport]
90
93
  options = check_options(options, options_available)
91
-
94
+
95
+ # add url to options query
96
+ options[:url] = url
97
+
92
98
  query_string = options.
93
99
  sort_by {|s| s[0].to_s }. # sort query by keys for uniformity
94
100
  select {|s| s[1] }. # skip empty options
95
101
  map {|s| s.map {|v| CGI::escape(v.to_s) }.join('=') }. # escape keys & vals
96
102
  join('&')
97
-
103
+
98
104
  # generate token
99
- token = Digest::MD5.hexdigest(query_string + Url2png.private_key)
105
+ token = Url2png.token query_string
100
106
 
101
107
 
102
108
  "http://beta.url2png.com/v6/#{Url2png.api_key}/#{token}/png/?#{query_string}"
@@ -110,14 +116,14 @@ module Url2png
110
116
  ######
111
117
 
112
118
  # check for unavailable options
113
- options_available = [:url, :size, :thumbnail, :browser_size, :delay, :fullscreen]
119
+ options_available = [:size, :thumbnail, :browser_size, :delay, :fullscreen]
114
120
  options = check_options(options, options_available)
115
121
 
116
122
  # escape the url
117
- safe_url= CGI::escape(options[:url])
123
+ safe_url= CGI::escape(url)
118
124
 
119
125
  # generate token
120
- token = Digest::MD5.hexdigest("#{ Url2png.private_key }+#{ safe_url }")
126
+ token = Url2png.token safe_url
121
127
 
122
128
  # build options portion of URL
123
129
  url_options = []
@@ -136,9 +142,51 @@ module Url2png
136
142
  url_options_string,
137
143
  safe_url
138
144
  )
145
+
146
+ when 'v3'
147
+ ######
148
+ # v3 #
149
+ ######
150
+
151
+ # http://api.url2png.com/v3/api_key/security_hash/bounds/url
152
+
153
+ options_available = [:fullscreen, :size]
154
+ options = check_options(options, options_available)
155
+
156
+ # escape the url
157
+ safe_url= CGI::escape(url)
158
+
159
+ # generate token
160
+ token = Url2png.token safe_url
161
+
162
+ # custom size or default_size
163
+ size = options[:size] || Url2png.default_size
164
+
165
+ # build options portion of URL
166
+ if options[:fullscreen]
167
+ bounds = "FULL"
168
+ else
169
+ bounds = size
170
+ end
171
+
172
+ # build image url
173
+ File.join(
174
+ "http://api.url2png.com",
175
+ Url2png.api_version,
176
+ Url2png.api_key,
177
+ token,
178
+ bounds,
179
+ safe_url
180
+ )
139
181
  end
140
182
  end
141
183
  end
184
+
185
+ # -----
186
+ # Alias
187
+ def site_image_tag url, options = {}
188
+ url2png_image_tag(url, options)
189
+ end
142
190
  end
143
191
  end
144
192
  end
data/spec/url2png_spec.rb CHANGED
@@ -1,7 +1,49 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
3
  describe "Url2png" do
4
- it "fails" do
5
- fail "hey buddy, you should probably rename this file and start specing for real"
4
+ describe "attributes" do
5
+ it "should set mandatory attributes" do
6
+ my_api_key = "my_api_key"
7
+ my_private_key = "my_private_key"
8
+ Url2png.config ({api_key: my_api_key, private_key: my_private_key})
9
+ Url2png.api_key.should eq my_api_key
10
+ Url2png.private_key.should eq my_private_key
11
+ end
12
+
13
+ it "should throw an exception getting nil api_key" do
14
+ Url2png.config({private_key: "private_key"})
15
+ expect {Url2png.api_key}.to raise_error
16
+ end
17
+
18
+ it "should throw an exception getting nil private_key" do
19
+ Url2png.config({api_key: "private_key"})
20
+ expect {Url2png.private_key}.to raise_error end
21
+
22
+ it "should set mode" do
23
+ mode = "placehold"
24
+ Url2png.config ({mode: mode})
25
+ Url2png.mode.should eq mode
26
+ end
27
+
28
+ it "should throw an exception with an invalid mode" do
29
+ expect {Url2png.config({mode: "invalid mode"})}.to raise_error
30
+ end
31
+ end
32
+
33
+ describe "v3" do
34
+ it "should generate a valid token for v3" do
35
+ Url2png.config ({api_key: 'api_key', private_key: 'private_key', api_version: 'v3'})
36
+ Url2png.token('google.com').should eq '1bf25a84efbd998f2b9f5b4f1b0ad6eb'
37
+ end
38
+
39
+ it "should generate a valid token for v4" do
40
+ Url2png.config ({api_key: 'api_key', private_key: 'private_key', api_version: 'v4'})
41
+ Url2png.token('google.com').should eq '1bf25a84efbd998f2b9f5b4f1b0ad6eb'
42
+ end
43
+
44
+ it "should generate a valid token for v6" do
45
+ Url2png.config ({api_key: 'api_key', private_key: 'private_key', api_version: 'v6'})
46
+ Url2png.token('google.com').should eq 'a6bcacefb7e7b1a89ecd619af960b3b8'
47
+ end
6
48
  end
7
49
  end
data/url2png.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "url2png"
8
- s.version = "0.2.2"
8
+ s.version = "0.3.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["robinhoudmeyers", "wout fierens", "fuzzyalej", "ceritium", "lukemelia"]
12
- s.date = "2012-06-15"
12
+ s.date = "2012-06-27"
13
13
  s.description = "Generate screenshots from websites almost instantly at any preferred size using ruby and the url2png.com API"
14
14
  s.email = "houdmeyers@gmail.com"
15
15
  s.extra_rdoc_files = [
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: url2png
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,11 +13,11 @@ authors:
13
13
  autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
- date: 2012-06-15 00:00:00.000000000 Z
16
+ date: 2012-06-27 00:00:00.000000000 Z
17
17
  dependencies:
18
18
  - !ruby/object:Gem::Dependency
19
19
  name: rspec
20
- requirement: &70170384723740 !ruby/object:Gem::Requirement
20
+ requirement: &70092339600580 !ruby/object:Gem::Requirement
21
21
  none: false
22
22
  requirements:
23
23
  - - ~>
@@ -25,10 +25,10 @@ dependencies:
25
25
  version: 2.6.0
26
26
  type: :development
27
27
  prerelease: false
28
- version_requirements: *70170384723740
28
+ version_requirements: *70092339600580
29
29
  - !ruby/object:Gem::Dependency
30
30
  name: bundler
31
- requirement: &70170384723260 !ruby/object:Gem::Requirement
31
+ requirement: &70092339599060 !ruby/object:Gem::Requirement
32
32
  none: false
33
33
  requirements:
34
34
  - - ~>
@@ -36,10 +36,10 @@ dependencies:
36
36
  version: 1.1.3
37
37
  type: :development
38
38
  prerelease: false
39
- version_requirements: *70170384723260
39
+ version_requirements: *70092339599060
40
40
  - !ruby/object:Gem::Dependency
41
41
  name: jeweler
42
- requirement: &70170384722780 !ruby/object:Gem::Requirement
42
+ requirement: &70092339613580 !ruby/object:Gem::Requirement
43
43
  none: false
44
44
  requirements:
45
45
  - - ~>
@@ -47,10 +47,10 @@ dependencies:
47
47
  version: 1.8.3
48
48
  type: :development
49
49
  prerelease: false
50
- version_requirements: *70170384722780
50
+ version_requirements: *70092339613580
51
51
  - !ruby/object:Gem::Dependency
52
52
  name: rcov
53
- requirement: &70170384722260 !ruby/object:Gem::Requirement
53
+ requirement: &70092339611600 !ruby/object:Gem::Requirement
54
54
  none: false
55
55
  requirements:
56
56
  - - ! '>='
@@ -58,10 +58,10 @@ dependencies:
58
58
  version: '0'
59
59
  type: :development
60
60
  prerelease: false
61
- version_requirements: *70170384722260
61
+ version_requirements: *70092339611600
62
62
  - !ruby/object:Gem::Dependency
63
63
  name: rspec
64
- requirement: &70170384721780 !ruby/object:Gem::Requirement
64
+ requirement: &70092339606840 !ruby/object:Gem::Requirement
65
65
  none: false
66
66
  requirements:
67
67
  - - ! '>'
@@ -69,7 +69,7 @@ dependencies:
69
69
  version: 2.6.0
70
70
  type: :development
71
71
  prerelease: false
72
- version_requirements: *70170384721780
72
+ version_requirements: *70092339606840
73
73
  description: Generate screenshots from websites almost instantly at any preferred
74
74
  size using ruby and the url2png.com API
75
75
  email: houdmeyers@gmail.com
@@ -112,7 +112,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
112
112
  version: '0'
113
113
  segments:
114
114
  - 0
115
- hash: 1190505122960655189
115
+ hash: -354415127866206290
116
116
  required_rubygems_version: !ruby/object:Gem::Requirement
117
117
  none: false
118
118
  requirements: