websnap 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -2,9 +2,9 @@ require 'rubygems'
2
2
  require 'echoe'
3
3
 
4
4
  require 'rake'
5
- #require 'spec/rake/spectask'
5
+ require 'rspec/core/rake_task'
6
6
 
7
- Echoe.new("websnap", "0.1.2") do |p|
7
+ Echoe.new("websnap", "0.1.3") do |p|
8
8
  p.author = "Francis Chong"
9
9
  p.description = "Create snapshot of webpage"
10
10
  p.url = "http://github.com/siuying/websnap"
@@ -12,3 +12,6 @@ Echoe.new("websnap", "0.1.2") do |p|
12
12
  p.development_dependencies = ["rspec", "echoe", "mocha"]
13
13
  p.runtime_dependencies = []
14
14
  end
15
+
16
+ RSpec::Core::RakeTask.new('spec') do |t|
17
+ end
@@ -1,4 +1,4 @@
1
- class WebSnap
1
+ module WebSnap
2
2
 
3
3
  class Source
4
4
 
@@ -1,93 +1,95 @@
1
- class WebSnap
1
+ module WebSnap
2
+ class Snapper
2
3
 
3
- class NoExecutableError < StandardError
4
- def initialize
5
- super('Could not locate wkhtmltoimage-proxy executable')
4
+ class NoExecutableError < StandardError
5
+ def initialize
6
+ super('Could not locate wkhtmltoimage-proxy executable')
7
+ end
6
8
  end
7
- end
8
9
 
9
- class ImproperSourceError < StandardError
10
- def initialize(msg)
11
- super("Improper Source: #{msg}")
10
+ class ImproperSourceError < StandardError
11
+ def initialize(msg)
12
+ super("Improper Source: #{msg}")
13
+ end
12
14
  end
13
- end
14
15
 
15
- attr_accessor :source, :stylesheets
16
- attr_reader :options
16
+ attr_accessor :source, :stylesheets
17
+ attr_reader :options
17
18
 
18
- def initialize(url_file_or_html, options={})
19
- @source = Source.new(url_file_or_html)
19
+ def initialize(url_file_or_html, options={})
20
+ @source = Source.new(url_file_or_html)
20
21
 
21
- @stylesheets = []
22
+ @stylesheets = []
22
23
 
23
- default_options = {
24
- :'crop-h' => '768',
25
- :'crop-w' => '1024',
26
- :'crop-x' => '0',
27
- :'crop-y' => '0',
28
- :'format' => 'png'
29
- }
30
- @options = normalize_options(default_options.merge(options))
24
+ default_options = {
25
+ :'crop-h' => '768',
26
+ :'crop-w' => '1024',
27
+ :'crop-x' => '0',
28
+ :'crop-y' => '0',
29
+ :'format' => 'jpg'
30
+ }
31
+ @options = normalize_options(default_options.merge(options))
31
32
 
32
- raise NoExecutableError.new if wkhtmltoimage.nil? || wkhtmltoimage == ''
33
- end
33
+ raise NoExecutableError.new if wkhtmltoimage.nil? || wkhtmltoimage == ''
34
+ end
34
35
 
35
- def command
36
- args = [wkhtmltoimage]
37
- args += @options.to_a.flatten.compact
36
+ def command
37
+ args = [wkhtmltoimage]
38
+ args += @options.to_a.flatten.compact
38
39
 
39
- if @source.html?
40
- args << '-' # Get HTML from stdin
41
- else
42
- args << @source.to_s
43
- end
40
+ if @source.html?
41
+ args << '-' # Get HTML from stdin
42
+ else
43
+ args << @source.to_s
44
+ end
44
45
 
45
- args << '-' # Read PDF from stdout
46
- args.join(' ')
47
- end
46
+ args << '-' # Read PDF from stdout
47
+ args.join(' ')
48
+ end
48
49
 
49
- def to_bytes
50
- img = IO.popen(command, "w+")
51
- img.puts(@source.to_s) if @source.html?
52
- img.close_write
53
- result = img.gets(nil)
54
- img.close_read
55
- return result
56
- end
50
+ def to_bytes
51
+ img = IO.popen(command, "w+")
52
+ img.puts(@source.to_s) if @source.html?
53
+ img.close_write
54
+ result = img.gets(nil)
55
+ img.close_read
56
+ return result
57
+ end
57
58
 
58
- def to_file(path)
59
- File.open(path,'w') {|file| file << self.to_bytes}
60
- end
59
+ def to_file(path)
60
+ File.open(path,'w') {|file| file << self.to_bytes}
61
+ end
61
62
 
62
- protected
63
+ protected
63
64
 
64
- def wkhtmltoimage
65
- @wkhtmltoimage ||= `which wkhtmltoimage-proxy`.chomp
66
- end
65
+ def wkhtmltoimage
66
+ @wkhtmltoimage ||= `which wkhtmltoimage-proxy`.chomp
67
+ end
67
68
 
68
- def normalize_options(options)
69
- normalized_options = {}
70
- options.each do |key, value|
71
- next if !value
72
- normalized_key = "--#{normalize_arg key}"
73
- normalized_options[normalized_key] = normalize_value(value)
69
+ def normalize_options(options)
70
+ normalized_options = {}
71
+ options.each do |key, value|
72
+ next if !value
73
+ normalized_key = "--#{normalize_arg key}"
74
+ normalized_options[normalized_key] = normalize_value(value)
75
+ end
76
+ normalized_options
74
77
  end
75
- normalized_options
76
- end
77
78
 
78
- def normalize_arg(arg)
79
- arg.to_s.downcase.gsub(/[^a-z0-9]/,'-')
80
- end
79
+ def normalize_arg(arg)
80
+ arg.to_s.downcase.gsub(/[^a-z0-9]/,'-')
81
+ end
81
82
 
82
- def normalize_value(value)
83
- case value
84
- when TrueClass
85
- nil
86
- when String
87
- value.match(/\s/) ? "\"#{value}\"" : value
88
- else
89
- value
83
+ def normalize_value(value)
84
+ case value
85
+ when TrueClass
86
+ nil
87
+ when String
88
+ value.match(/\s/) ? "\"#{value}\"" : value
89
+ else
90
+ value
91
+ end
90
92
  end
91
- end
92
93
 
94
+ end
93
95
  end
@@ -1,34 +1,34 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe WebSnap do
3
+ describe WebSnap::Snapper do
4
4
 
5
5
  context "initialization" do
6
6
  it "should accept HTML as the source" do
7
- websnap = WebSnap.new('<h1>Oh Hai</h1>')
7
+ websnap = WebSnap::Snapper.new('<h1>Oh Hai</h1>')
8
8
  websnap.source.should be_html
9
9
  websnap.source.to_s.should == '<h1>Oh Hai</h1>'
10
10
  end
11
11
 
12
12
  it "should accept a URL as the source" do
13
- websnap = WebSnap.new('http://google.com')
13
+ websnap = WebSnap::Snapper.new('http://google.com')
14
14
  websnap.source.should be_url
15
15
  websnap.source.to_s.should == 'http://google.com'
16
16
  end
17
17
 
18
18
  it "should accept a File as the source" do
19
19
  file_path = File.join(SPEC_ROOT,'fixtures','google.html')
20
- websnap = WebSnap.new(File.new(file_path))
20
+ websnap = WebSnap::Snapper.new(File.new(file_path))
21
21
  websnap.source.should be_file
22
22
  websnap.source.to_s.should == file_path
23
23
  end
24
24
 
25
25
  it "should parse the options into a cmd line friedly format" do
26
- websnap = WebSnap.new('html', :'crop-w' => '800', :'crop-h' => '600')
26
+ websnap = WebSnap::Snapper.new('html', :'crop-w' => '800', :'crop-h' => '600')
27
27
  websnap.options.should have_key('--crop-w')
28
28
  end
29
29
 
30
30
  it "should provide default options" do
31
- websnap = WebSnap.new('<h1>Oh Hai</h1>')
31
+ websnap = WebSnap::Snapper.new('<h1>Oh Hai</h1>')
32
32
  ['--crop-w', '--crop-h', '--scale-w', '--scale-h', '--format'].each do |option|
33
33
  websnap.options.should have_key(option)
34
34
  end
@@ -37,30 +37,30 @@ describe WebSnap do
37
37
 
38
38
  context "command" do
39
39
  it "should contstruct the correct command" do
40
- websnap = WebSnap.new('html', :'encoding' => 'Big5')
40
+ websnap = WebSnap::Snapper.new('html', :'encoding' => 'Big5')
41
41
  websnap.command.should include('--encoding Big5')
42
42
  end
43
43
 
44
44
  it "read the source from stdin if it is html" do
45
- websnap = WebSnap.new('html')
45
+ websnap = WebSnap::Snapper.new('html')
46
46
  websnap.command.should match(/ - -$/)
47
47
  end
48
48
 
49
49
  it "specify the URL to the source if it is a url" do
50
- websnap = WebSnap.new('http://google.com')
50
+ websnap = WebSnap::Snapper.new('http://google.com')
51
51
  websnap.command.should match(/ http:\/\/google\.com -$/)
52
52
  end
53
53
 
54
54
  it "should specify the path to the source if it is a file" do
55
55
  file_path = File.join(SPEC_ROOT,'fixtures','google.html')
56
- websnap = WebSnap.new(File.new(file_path))
56
+ websnap = WebSnap::Snapper.new(File.new(file_path))
57
57
  websnap.command.should match(/ #{file_path} -$/)
58
58
  end
59
59
  end
60
60
 
61
61
  context "#to_bytes" do
62
62
  it "should generate a PDF of the HTML" do
63
- websnap = WebSnap.new('html')
63
+ websnap = WebSnap::Snapper.new('html')
64
64
  websnap.expects(:to_bytes).returns('PNG')
65
65
  png = websnap.to_bytes
66
66
  png.should match(/PNG/)
@@ -78,7 +78,7 @@ describe WebSnap do
78
78
  end
79
79
 
80
80
  it "should create a file with the PNG as content" do
81
- websnap = WebSnap.new('html')
81
+ websnap = WebSnap::Snapper.new('html')
82
82
  websnap.expects(:to_bytes).returns('PNG')
83
83
 
84
84
  file = websnap.to_file(@file_path)
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{websnap}
5
- s.version = "0.1.2"
5
+ s.version = "0.1.3"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Francis Chong"]
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: websnap
3
3
  version: !ruby/object:Gem::Version
4
- hash: 31
4
+ hash: 29
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 2
10
- version: 0.1.2
9
+ - 3
10
+ version: 0.1.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Francis Chong