screenshot_machine 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.rspec +3 -0
- data/.travis.yml +3 -0
- data/Gemfile +9 -0
- data/README.md +2 -0
- data/Rakefile +6 -0
- data/lib/screenshot_machine.rb +20 -0
- data/lib/screenshot_machine/configuration.rb +13 -6
- data/lib/screenshot_machine/version.rb +1 -1
- data/spec/screenshot_machine/generator_spec.rb +94 -0
- data/spec/screenshot_machine_spec.rb +110 -0
- data/spec/spec_helper.rb +17 -0
- metadata +10 -2
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
CHANGED
data/README.md
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
[](https://travis-ci.org/tupalo/screenshot_machine)
|
|
2
|
+
|
|
1
3
|
# ScreenshotMachine
|
|
2
4
|
|
|
3
5
|
A gem to create screenshots of webpages by using the [ScreenshotMachine.com](http://screenshotmachine.com) API.
|
data/Rakefile
CHANGED
data/lib/screenshot_machine.rb
CHANGED
|
@@ -13,6 +13,26 @@ module ScreenshotMachine
|
|
|
13
13
|
class InvalidKey < StandardError; end
|
|
14
14
|
class NoCredits < StandardError; end
|
|
15
15
|
end
|
|
16
|
+
|
|
17
|
+
class << self
|
|
18
|
+
# Alias for ScreenshotMachine::Generator.new
|
|
19
|
+
#
|
|
20
|
+
# @return [ScreenshotMachine::Generator]
|
|
21
|
+
def new(url, options={})
|
|
22
|
+
ScreenshotMachine::Generator.new(url, options)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# Delegate to ScreenshotMachine::Generator
|
|
26
|
+
def method_missing(method, *args, &block)
|
|
27
|
+
return super unless new.respond_to?(method)
|
|
28
|
+
new.send(method, *args, &block)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# Delegate to ScreenshotMachine::Generator
|
|
32
|
+
def respond_to?(method, include_private = false)
|
|
33
|
+
new.respond_to?(method, include_private) || super(method, include_private)
|
|
34
|
+
end
|
|
35
|
+
end
|
|
16
36
|
end
|
|
17
37
|
|
|
18
38
|
require "screenshot_machine/generator"
|
|
@@ -12,6 +12,13 @@ module ScreenshotMachine
|
|
|
12
12
|
# @private
|
|
13
13
|
attr_accessor *VALID_PARAMS_KEYS
|
|
14
14
|
|
|
15
|
+
DEFAULT_SIZE = "L" # T, S, E, N, M, L, X, F
|
|
16
|
+
DEFAULT_FORMAT = "JPG" # JPG, GIF, PNG
|
|
17
|
+
DEFAULT_CACHELIMIT = 14 # 0-14 in days
|
|
18
|
+
DEFAULT_TIMEOUT = 200 # 0, 200, 400, 600, 800, 1000 in ms
|
|
19
|
+
DEFAULT_URL = nil
|
|
20
|
+
DEFAULT_KEY = nil
|
|
21
|
+
|
|
15
22
|
# When this module is extended, set all configuration options to their default values
|
|
16
23
|
def self.extended(base)
|
|
17
24
|
base.reset
|
|
@@ -29,12 +36,12 @@ module ScreenshotMachine
|
|
|
29
36
|
|
|
30
37
|
# Reset all configuration options to defaults
|
|
31
38
|
def reset
|
|
32
|
-
self.size =
|
|
33
|
-
self.format =
|
|
34
|
-
self.cacheLimit =
|
|
35
|
-
self.timeout =
|
|
36
|
-
self.url =
|
|
37
|
-
self.key =
|
|
39
|
+
self.size = DEFAULT_SIZE
|
|
40
|
+
self.format = DEFAULT_FORMAT
|
|
41
|
+
self.cacheLimit = DEFAULT_CACHELIMIT
|
|
42
|
+
self.timeout = DEFAULT_TIMEOUT
|
|
43
|
+
self.url = DEFAULT_URL
|
|
44
|
+
self.key = DEFAULT_KEY
|
|
38
45
|
self
|
|
39
46
|
end
|
|
40
47
|
end
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe ScreenshotMachine::Generator do
|
|
4
|
+
describe ".new" do
|
|
5
|
+
it "requires one argument" do
|
|
6
|
+
expect { ScreenshotMachine::Generator.new }.to raise_error(ArgumentError)
|
|
7
|
+
expect(ScreenshotMachine::Generator).to respond_to(:new).with(1).argument
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
it "takes an optional second argument" do
|
|
11
|
+
expect(ScreenshotMachine::Generator).to respond_to(:new).with(2).arguments
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
it "initializes with one argument" do
|
|
15
|
+
generator = ScreenshotMachine::Generator.new(true)
|
|
16
|
+
expect(generator).to be_kind_of(ScreenshotMachine::Generator)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
it "takes the url as first argument" do
|
|
20
|
+
url = 'http://example.com'
|
|
21
|
+
generator = ScreenshotMachine::Generator.new(url)
|
|
22
|
+
expect(generator.url).to eq(url)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
it "takes the options as second argument" do
|
|
26
|
+
url = nil
|
|
27
|
+
options = { :key => 'abc123' }
|
|
28
|
+
generator = ScreenshotMachine::Generator.new(url, options)
|
|
29
|
+
expect(generator.key).to eq(options[:key])
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
describe ".screenshot_url" do
|
|
34
|
+
let(:generator){ ScreenshotMachine::Generator.new("http://example.com") }
|
|
35
|
+
|
|
36
|
+
it "calls params" do
|
|
37
|
+
generator.should_receive(:params).and_return( { :a => 'b', :c => 'd' })
|
|
38
|
+
generator.send(:screenshot_url)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
it "includes all params" do
|
|
42
|
+
generator.should_receive(:params).and_return( { :a => 'b', :c => 'd' })
|
|
43
|
+
expect(generator.send(:screenshot_url)).to match("a=b")
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
it "includes the API_URL" do
|
|
47
|
+
expect(generator.send(:screenshot_url)).to match(ScreenshotMachine::API_URL)
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
describe ".screenshot" do
|
|
52
|
+
let(:generator){ ScreenshotMachine::Generator.new("http://example.com") }
|
|
53
|
+
|
|
54
|
+
context "when a valid API call" do
|
|
55
|
+
it "returns a string" do
|
|
56
|
+
stub_request(:get, /api\.screenshotmachine\.com.*/)
|
|
57
|
+
expect(generator.screenshot.class).to eq(String)
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
context "when requesting an invalid URL" do
|
|
62
|
+
it "should raise an exception" do
|
|
63
|
+
stub_request(:get, /api\.screenshotmachine\.com.*/).
|
|
64
|
+
to_return(
|
|
65
|
+
:status => 200,
|
|
66
|
+
:headers => { 'x-screenshotmachine-response' => 'invalid_url' }
|
|
67
|
+
)
|
|
68
|
+
expect{ generator.screenshot }.to raise_error(ScreenshotMachine::Exceptions::InvalidUrl)
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
context "when requesting an invalid API KEY" do
|
|
73
|
+
it "should raise an exception" do
|
|
74
|
+
stub_request(:get, /api\.screenshotmachine\.com.*/).
|
|
75
|
+
to_return(
|
|
76
|
+
:status => 200,
|
|
77
|
+
:headers => { 'x-screenshotmachine-response' => 'invalid_key' }
|
|
78
|
+
)
|
|
79
|
+
expect{ generator.screenshot }.to raise_error(ScreenshotMachine::Exceptions::InvalidKey)
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
context "when requesting with no credits left" do
|
|
84
|
+
it "should raise an exception" do
|
|
85
|
+
stub_request(:get, /api\.screenshotmachine\.com.*/).
|
|
86
|
+
to_return(
|
|
87
|
+
:status => 200,
|
|
88
|
+
:headers => { 'x-screenshotmachine-response' => 'no_credits' }
|
|
89
|
+
)
|
|
90
|
+
expect{ generator.screenshot }.to raise_error(ScreenshotMachine::Exceptions::NoCredits)
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
end
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe ScreenshotMachine do
|
|
4
|
+
after do
|
|
5
|
+
ScreenshotMachine.reset
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
describe ".new" do
|
|
9
|
+
it "is a ScreenshotMachine::Generator" do
|
|
10
|
+
expect(ScreenshotMachine.new("")).to be_a ScreenshotMachine::Generator
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
describe ".size" do
|
|
15
|
+
it "returns the default size" do
|
|
16
|
+
expect(ScreenshotMachine.size).to eq(ScreenshotMachine::Configuration::DEFAULT_SIZE)
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
describe ".size=" do
|
|
21
|
+
it "sets the size" do
|
|
22
|
+
ScreenshotMachine.size = 'X'
|
|
23
|
+
expect(ScreenshotMachine.size).to eq('X')
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
describe ".format" do
|
|
28
|
+
it "returns the default format" do
|
|
29
|
+
expect(ScreenshotMachine.format).to eq(ScreenshotMachine::Configuration::DEFAULT_FORMAT)
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
describe ".format=" do
|
|
34
|
+
it "sets the format" do
|
|
35
|
+
ScreenshotMachine.format = 'PNG'
|
|
36
|
+
expect(ScreenshotMachine.format).to eq('PNG')
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
describe ".cacheLimit" do
|
|
42
|
+
it "returns the default cacheLimit" do
|
|
43
|
+
expect(ScreenshotMachine.cacheLimit).to eq(ScreenshotMachine::Configuration::DEFAULT_CACHELIMIT)
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
describe ".cacheLimit=" do
|
|
48
|
+
it "sets the cacheLimit" do
|
|
49
|
+
ScreenshotMachine.cacheLimit = 400
|
|
50
|
+
expect(ScreenshotMachine.cacheLimit).to eq(400)
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
describe ".timeout" do
|
|
55
|
+
it "returns the default timeout" do
|
|
56
|
+
expect(ScreenshotMachine.timeout).to eq(ScreenshotMachine::Configuration::DEFAULT_TIMEOUT)
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
describe ".timeout=" do
|
|
61
|
+
it "sets the timeout" do
|
|
62
|
+
ScreenshotMachine.timeout = 400
|
|
63
|
+
expect(ScreenshotMachine.timeout).to eq(400)
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
describe ".url" do
|
|
68
|
+
it "returns the default url" do
|
|
69
|
+
expect(ScreenshotMachine.url).to eq(ScreenshotMachine::Configuration::DEFAULT_URL)
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
describe ".url=" do
|
|
74
|
+
it "sets the url" do
|
|
75
|
+
ScreenshotMachine.url = 'http://example.com'
|
|
76
|
+
expect(ScreenshotMachine.url).to eq('http://example.com')
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
describe ".key" do
|
|
81
|
+
it "returns the default key" do
|
|
82
|
+
expect(ScreenshotMachine.key).to eq(ScreenshotMachine::Configuration::DEFAULT_KEY)
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
describe ".key=" do
|
|
87
|
+
it "sets the key" do
|
|
88
|
+
ScreenshotMachine.key = 'abcdefgh123456'
|
|
89
|
+
expect(ScreenshotMachine.key).to eq('abcdefgh123456')
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
describe ".configure" do
|
|
94
|
+
ScreenshotMachine::Configuration::VALID_PARAMS_KEYS.each do |key|
|
|
95
|
+
it "sets the #{key}" do
|
|
96
|
+
ScreenshotMachine.configure do |config|
|
|
97
|
+
config.send("#{key}=", key)
|
|
98
|
+
expect(ScreenshotMachine.send(key)).to eq(key)
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
describe ".options" do
|
|
105
|
+
it "returns the configuration as a hash" do
|
|
106
|
+
expect(ScreenshotMachine.options).to be_kind_of(Hash)
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
end
|
data/spec/spec_helper.rb
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
unless ENV['CI']
|
|
2
|
+
require 'simplecov'
|
|
3
|
+
SimpleCov.start do
|
|
4
|
+
add_group 'Tweetstream', 'lib/tweetstream'
|
|
5
|
+
add_group 'Specs', 'spec'
|
|
6
|
+
end
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
require 'screenshot_machine'
|
|
10
|
+
require 'rspec'
|
|
11
|
+
require 'webmock/rspec'
|
|
12
|
+
|
|
13
|
+
RSpec.configure do |config|
|
|
14
|
+
config.expect_with :rspec do |c|
|
|
15
|
+
c.syntax = :expect
|
|
16
|
+
end
|
|
17
|
+
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: screenshot_machine
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.0.
|
|
4
|
+
version: 0.0.3
|
|
5
5
|
prerelease:
|
|
6
6
|
platform: ruby
|
|
7
7
|
authors:
|
|
@@ -19,6 +19,8 @@ extensions: []
|
|
|
19
19
|
extra_rdoc_files: []
|
|
20
20
|
files:
|
|
21
21
|
- .gitignore
|
|
22
|
+
- .rspec
|
|
23
|
+
- .travis.yml
|
|
22
24
|
- Gemfile
|
|
23
25
|
- LICENSE
|
|
24
26
|
- README.md
|
|
@@ -28,6 +30,9 @@ files:
|
|
|
28
30
|
- lib/screenshot_machine/generator.rb
|
|
29
31
|
- lib/screenshot_machine/version.rb
|
|
30
32
|
- screenshot_machine.gemspec
|
|
33
|
+
- spec/screenshot_machine/generator_spec.rb
|
|
34
|
+
- spec/screenshot_machine_spec.rb
|
|
35
|
+
- spec/spec_helper.rb
|
|
31
36
|
homepage: ''
|
|
32
37
|
licenses: []
|
|
33
38
|
post_install_message:
|
|
@@ -53,4 +58,7 @@ signing_key:
|
|
|
53
58
|
specification_version: 3
|
|
54
59
|
summary: This gem returns a screenshot of a webpage, using the ScreenshotMachine.com
|
|
55
60
|
API (free account required).
|
|
56
|
-
test_files:
|
|
61
|
+
test_files:
|
|
62
|
+
- spec/screenshot_machine/generator_spec.rb
|
|
63
|
+
- spec/screenshot_machine_spec.rb
|
|
64
|
+
- spec/spec_helper.rb
|