gastly 0.2.1 → 0.2.2
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +32 -0
- data/lib/gastly.rb +1 -0
- data/lib/gastly/exceptions.rb +11 -0
- data/lib/gastly/screenshot.rb +30 -15
- data/lib/gastly/script.js +40 -18
- data/lib/gastly/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 327d0941c591a2f1b07c19b013307d917f9c744e
|
4
|
+
data.tar.gz: 0f87c8fc8905ffc76e0dccb6395302214f13ca71
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bd7e4f76405c0c344427fc052950ff8b36abf56269becf515cd278527a2af2e09dd48f31891fa1646653117e26c52d1146edbe273f4316f4384c5b449d79c8cc
|
7
|
+
data.tar.gz: c49f95e949893ef171c9d6d9366aa327d10300db95cc8e91b03db5a82717c6981855f097973be6b1a640d7096727a066ad30c15919ed369f1103743dc0bd7ec5
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
## 0.2.2 (19/08/2015)
|
2
|
+
|
3
|
+
### Enhancements
|
4
|
+
|
5
|
+
* Improved exception handling and error
|
6
|
+
* Capture all page if selector not found
|
7
|
+
|
8
|
+
## 0.2.1 (10/08/2015)
|
9
|
+
|
10
|
+
### Bug Fixes
|
11
|
+
|
12
|
+
* Stop PhantomJS if a site unavailable
|
13
|
+
|
14
|
+
## 0.2.0 (23/07/2015)
|
15
|
+
|
16
|
+
### Features
|
17
|
+
|
18
|
+
* Add proxy support
|
19
|
+
|
20
|
+
### Enhancements
|
21
|
+
|
22
|
+
* Update dependencies
|
23
|
+
|
24
|
+
## 0.1.1 (13/07/2015)
|
25
|
+
|
26
|
+
### Enhancements
|
27
|
+
|
28
|
+
* Update version Phantomjs to 2.0
|
29
|
+
|
30
|
+
## 0.1.0 (13/07/2015)
|
31
|
+
|
32
|
+
* Initial release
|
data/lib/gastly.rb
CHANGED
data/lib/gastly/screenshot.rb
CHANGED
@@ -5,8 +5,6 @@ require 'active_support/core_ext/object/blank'
|
|
5
5
|
module Gastly
|
6
6
|
class Screenshot
|
7
7
|
|
8
|
-
FetchError = Class.new(StandardError)
|
9
|
-
|
10
8
|
SCRIPT_PATH = File.expand_path('../script.js', __FILE__)
|
11
9
|
DEFAULT_TIMEOUT = 0
|
12
10
|
DEFAULT_BROWSER_WIDTH = 1440
|
@@ -16,11 +14,13 @@ module Gastly
|
|
16
14
|
|
17
15
|
attr_writer :timeout, :browser_width, :browser_height
|
18
16
|
attr_accessor :url, :selector, :cookies, :proxy_host, :proxy_port
|
17
|
+
attr_reader :tempfile
|
19
18
|
|
20
19
|
def initialize(url, **kwargs)
|
21
20
|
kwargs.assert_valid_keys(:timeout, :browser_width, :browser_height, :selector, :cookies, :proxy_host, :proxy_port)
|
22
21
|
|
23
22
|
@url = url
|
23
|
+
@tempfile = Tempfile.new([DEFAULT_FILE_NAME, DEFAULT_FILE_FORMAT])
|
24
24
|
self.cookies = kwargs.delete(:cookies)
|
25
25
|
|
26
26
|
kwargs.each { |key, value| instance_variable_set(:"@#{key}", value) }
|
@@ -39,8 +39,24 @@ module Gastly
|
|
39
39
|
end
|
40
40
|
|
41
41
|
def capture
|
42
|
-
|
42
|
+
Phantomjs.proxy_host = proxy_host if proxy_host
|
43
|
+
Phantomjs.proxy_port = proxy_port if proxy_port
|
44
|
+
|
45
|
+
output = Phantomjs.run(proxy_options, SCRIPT_PATH.to_s, *prepared_params)
|
46
|
+
|
47
|
+
handle_exception(output) if output.present?
|
48
|
+
|
49
|
+
Gastly::Image.new(tempfile)
|
50
|
+
end
|
51
|
+
|
52
|
+
private
|
53
|
+
|
54
|
+
def proxy_options
|
55
|
+
return '' if proxy_host.blank? && proxy_port.blank?
|
56
|
+
"--proxy=#{proxy_host}:#{proxy_port}"
|
57
|
+
end
|
43
58
|
|
59
|
+
def prepared_params
|
44
60
|
params = {
|
45
61
|
url: url,
|
46
62
|
timeout: timeout,
|
@@ -50,23 +66,22 @@ module Gastly
|
|
50
66
|
}
|
51
67
|
|
52
68
|
params[:selector] = selector if selector.present?
|
53
|
-
params[:cookies] = cookies.
|
54
|
-
prepared_params = params.map { |k, v| "#{k}=#{v}" }
|
55
|
-
|
56
|
-
Phantomjs.proxy_host = proxy_host if proxy_host
|
57
|
-
Phantomjs.proxy_port = proxy_port if proxy_port
|
58
|
-
output = Phantomjs.run(proxy_options, SCRIPT_PATH.to_s, *prepared_params)
|
69
|
+
params[:cookies] = hash_to_array(cookies).join(',') if cookies.present?
|
59
70
|
|
60
|
-
|
71
|
+
hash_to_array(params)
|
72
|
+
end
|
61
73
|
|
62
|
-
|
74
|
+
def hash_to_array(data)
|
75
|
+
data.map { |key, value| "#{key}=#{value}" }
|
63
76
|
end
|
64
77
|
|
65
|
-
|
78
|
+
def handle_exception(output)
|
79
|
+
error = case output
|
80
|
+
when /^FetchError:(.+)/ then Gastly::FetchError
|
81
|
+
when /^RuntimeError:(.+)/m then Gastly::PhantomJSError
|
82
|
+
end
|
66
83
|
|
67
|
-
|
68
|
-
return '' if proxy_host.blank? && proxy_port.blank?
|
69
|
-
"--proxy=#{proxy_host}:#{proxy_port}"
|
84
|
+
raise error, $1
|
70
85
|
end
|
71
86
|
|
72
87
|
end
|
data/lib/gastly/script.js
CHANGED
@@ -1,13 +1,11 @@
|
|
1
1
|
function extractDomain(url){
|
2
2
|
var domain;
|
3
3
|
// Remove protocol
|
4
|
-
if (url.indexOf("://") > -1){
|
4
|
+
if (url.indexOf("://") > -1) {
|
5
5
|
domain = url.split('/')[2];
|
6
|
-
}
|
7
|
-
else {
|
6
|
+
} else {
|
8
7
|
domain = url.split('/')[0];
|
9
8
|
}
|
10
|
-
|
11
9
|
// Remove port number
|
12
10
|
domain = domain.split(':')[0];
|
13
11
|
|
@@ -19,16 +17,16 @@ var i, pair,
|
|
19
17
|
system = require('system'),
|
20
18
|
page = require('webpage').create();
|
21
19
|
|
22
|
-
|
23
|
-
pair =
|
20
|
+
system.args.forEach(function(arg) {
|
21
|
+
pair = arg.split(/=(.*)/);
|
24
22
|
args[pair[0]] = pair[1];
|
25
|
-
}
|
23
|
+
});
|
26
24
|
|
27
|
-
if (args.cookies !== undefined){
|
25
|
+
if (args.cookies !== undefined) {
|
28
26
|
var cookiesPair = args.cookies.split(',');
|
29
27
|
|
30
|
-
|
31
|
-
pair =
|
28
|
+
cookiesPair.forEach(function(cookie) {
|
29
|
+
pair = cookie.split(/=(.*)/);
|
32
30
|
|
33
31
|
page.addCookie({
|
34
32
|
'name': pair[0],
|
@@ -36,28 +34,52 @@ if (args.cookies !== undefined){
|
|
36
34
|
'path': '/',
|
37
35
|
'domain': extractDomain(args.url)
|
38
36
|
});
|
37
|
+
});
|
38
|
+
}
|
39
|
+
|
40
|
+
phantom.onError = function(message, trace){
|
41
|
+
var messageStack = ['RuntimeError:' + message];
|
42
|
+
|
43
|
+
if (trace && trace.length) {
|
44
|
+
messageStack.push('Trace:');
|
45
|
+
|
46
|
+
trace.forEach(function(t) {
|
47
|
+
messageStack.push(' -> ' + t.file + ': ' + t.line + (t.function ? ' (in function "' + t.function +'")' : ''));
|
48
|
+
});
|
39
49
|
}
|
50
|
+
|
51
|
+
console.log(messageStack.join('\n'));
|
52
|
+
phantom.exit(1);
|
40
53
|
}
|
41
54
|
|
42
55
|
page.open(args.url, function(status){
|
43
56
|
if(status !== 'success') {
|
44
|
-
console.log('
|
57
|
+
console.log('FetchError:' + args.url);
|
45
58
|
phantom.exit();
|
46
59
|
} else {
|
47
60
|
window.setTimeout(function(){
|
48
61
|
page.viewportSize = { width: args.width, height: args.height };
|
49
62
|
|
50
63
|
if (args.selector !== undefined){
|
64
|
+
// Returns ClientRect object or null if selector not found
|
51
65
|
var clipRect = page.evaluate(function(s){
|
52
|
-
|
66
|
+
var result, selector = document.querySelector(s);
|
67
|
+
|
68
|
+
if (selector !== null) {
|
69
|
+
result = selector.getBoundingClientRect();
|
70
|
+
}
|
71
|
+
|
72
|
+
return result;
|
53
73
|
}, args.selector);
|
54
74
|
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
75
|
+
if (clipRect !== null) {
|
76
|
+
page.clipRect = {
|
77
|
+
top: clipRect.top,
|
78
|
+
left: clipRect.left,
|
79
|
+
width: clipRect.width,
|
80
|
+
height: clipRect.height
|
81
|
+
};
|
82
|
+
}
|
61
83
|
}
|
62
84
|
|
63
85
|
page.render(args.output);
|
data/lib/gastly/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: gastly
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mikhail Grachev
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-08-
|
11
|
+
date: 2015-08-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -90,6 +90,7 @@ extra_rdoc_files: []
|
|
90
90
|
files:
|
91
91
|
- ".gitignore"
|
92
92
|
- ".travis.yml"
|
93
|
+
- CHANGELOG.md
|
93
94
|
- Gemfile
|
94
95
|
- README.md
|
95
96
|
- Rakefile
|
@@ -98,6 +99,7 @@ files:
|
|
98
99
|
- gastly.gemspec
|
99
100
|
- gastly.png
|
100
101
|
- lib/gastly.rb
|
102
|
+
- lib/gastly/exceptions.rb
|
101
103
|
- lib/gastly/image.rb
|
102
104
|
- lib/gastly/screenshot.rb
|
103
105
|
- lib/gastly/script.js
|