gastly 0.3.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 61481db693dd21bbbe481bc40e94cda224eeb786
4
- data.tar.gz: 406d4be2fa87cb64536a8e687a1935b965e3227a
3
+ metadata.gz: 2e380ceb3db540ad9ed92bfa89e2b80d49283996
4
+ data.tar.gz: f7c93c82e5a3f8b1d5d15ef2c831d295cd917c78
5
5
  SHA512:
6
- metadata.gz: 6350109f7427759e9ea898cd7caa9965efb869ce2bfd2f3c4b7dfbf02b58a9a49aaf9d94d35fc7ec387f5db9b51693d1d6ef91eb8dc7f953430b388057c153a1
7
- data.tar.gz: adac8e9e13cd50ccd1b8a3bc4c1ae44a20c39057e18c7ed078116dc24bc3a3cf9fc80e2bc65cf25fcc16444d1b64ab5350bc788450ff4894d32afd691ed5c56d
6
+ metadata.gz: abbafd3fef69db4563e9f73840c1e109676c27538b0541c11ba0353a413884b2dee4ac18ea50708b9fd90ad55eeee8b328551365f19b94f557966f347c3ee41b
7
+ data.tar.gz: 1b163d12b37ab611656dbd018f6c5845571ed368f766f89474618ab939ea6199582435ddf6f3d1d58743633f87a8852eb12e8effc3471424ec6d7ecd3b692db6
@@ -1,3 +1,4 @@
1
+ dist: trusty
1
2
  language: ruby
2
3
  rvm:
3
4
  - 2.1.7
@@ -1,3 +1,9 @@
1
+ ## 1.0.0 (16/07/2016)
2
+
3
+ ### Enhancements
4
+
5
+ * Remove support ActiveSupport
6
+
1
7
  ## 0.3.0 (29/01/2016)
2
8
 
3
9
  ### Enhancements
data/Gemfile CHANGED
@@ -3,4 +3,4 @@ source 'https://rubygems.org'
3
3
  # Specify your gem's dependencies in gastly.gemspec
4
4
  gemspec
5
5
 
6
- gem 'coveralls', require: false
6
+ gem 'coveralls', require: false
@@ -25,5 +25,4 @@ Gem::Specification.new do |spec|
25
25
 
26
26
  spec.add_dependency 'phantomjs', '~> 2.1.1'
27
27
  spec.add_dependency 'mini_magick', '~> 4.2'
28
- spec.add_dependency 'activesupport', '>= 3.1'
29
28
  end
@@ -1,8 +1,7 @@
1
1
  require 'phantomjs'
2
2
  require 'mini_magick'
3
- require 'active_support/core_ext/hash/keys'
4
- require 'active_support/core_ext/object/blank'
5
3
 
4
+ require_relative 'gastly/utils'
6
5
  require_relative 'gastly/phantomjs_patch'
7
6
  require_relative 'gastly/image'
8
7
  require_relative 'gastly/screenshot'
@@ -1,7 +1,5 @@
1
1
  module Gastly
2
2
  class FetchError < StandardError
3
- attr_reader :url
4
-
5
3
  def initialize(url)
6
4
  super("Unable to load #{url}")
7
5
  end
@@ -4,6 +4,8 @@ module Phantomjs
4
4
  end
5
5
 
6
6
  class Platform
7
+ RETRY_COUNT = 5
8
+
7
9
  class << self
8
10
  def install!
9
11
  STDERR.puts "Phantomjs does not appear to be installed in #{phantomjs_path}, installing!"
@@ -16,18 +18,18 @@ module Phantomjs
16
18
  FileUtils.mkdir_p temp_dir
17
19
 
18
20
  Dir.chdir temp_dir do
19
- unless download_via_curl or download_via_wget
20
- raise "\n\nFailed to load phantomjs! :(\nYou need to have cURL or wget installed on your system.\nIf you have, the source of phantomjs might be unavailable: #{package_url}\n\n"
21
+ unless download_via_curl || download_via_wget
22
+ fail "\n\nFailed to load phantomjs! :(\nYou need to have cURL or wget installed on your system.\nIf you have, the source of phantomjs might be unavailable: #{package_url}\n\n"
21
23
  end
22
24
 
23
25
  case package_url.split('.').last
24
- when 'bz2'
25
- system "bunzip2 #{File.basename(package_url)}"
26
- system "tar xf #{File.basename(package_url).sub(/\.bz2$/, '')}"
27
- when 'zip'
28
- system "unzip #{File.basename(package_url)}"
29
- else
30
- raise "Unknown compression format for #{File.basename(package_url)}"
26
+ when 'bz2'
27
+ system "bunzip2 #{File.basename(package_url)}"
28
+ system "tar xf #{File.basename(package_url).sub(/\.bz2$/, '')}"
29
+ when 'zip'
30
+ system "unzip #{File.basename(package_url)}"
31
+ else
32
+ fail "Unknown compression format for #{File.basename(package_url)}"
31
33
  end
32
34
 
33
35
  # Find the phantomjs build we just extracted
@@ -52,28 +54,32 @@ module Phantomjs
52
54
  end
53
55
  end
54
56
 
55
- raise 'Failed to install phantomjs. Sorry :(' unless File.exist?(phantomjs_path)
57
+ fail 'Failed to install phantomjs. Sorry :(' unless File.exist?(phantomjs_path)
56
58
  end
57
59
 
58
60
  private
59
61
 
60
62
  def download_via_curl
61
- system "curl -L -O #{package_url} #{curl_proxy_options}"
63
+ system "curl -L --retry #{RETRY_COUNT} -O #{package_url} #{curl_proxy_options}"
62
64
  end
63
65
 
64
66
  def download_via_wget
65
- system "wget #{package_url} #{wget_proxy_options}"
67
+ system "wget -t #{RETRY_COUNT} #{package_url} #{wget_proxy_options}"
66
68
  end
67
69
 
68
70
  def curl_proxy_options
69
- return '' if Phantomjs.proxy_host.nil? && Phantomjs.proxy_port.nil?
71
+ return '' if proxy_options_exist?
70
72
  "-x #{Phantomjs.proxy_host}:#{Phantomjs.proxy_port}"
71
73
  end
72
74
 
73
75
  def wget_proxy_options
74
- return '' if Phantomjs.proxy_host.nil? && Phantomjs.proxy_port.nil?
76
+ return '' if proxy_options_exist?
75
77
  "-e use_proxy=yes -e http_proxy=#{Phantomjs.proxy_host}:#{Phantomjs.proxy_port}"
76
78
  end
79
+
80
+ def proxy_options_exist?
81
+ Phantomjs.proxy_host.nil? && Phantomjs.proxy_port.nil?
82
+ end
77
83
  end
78
84
  end
79
85
  end
@@ -12,7 +12,8 @@ module Gastly
12
12
 
13
13
  # @param url [String] The full url to the site
14
14
  def initialize(url, **kwargs)
15
- kwargs.assert_valid_keys(:timeout, :browser_width, :browser_height, :selector, :cookies, :proxy_host, :proxy_port)
15
+ hash = Gastly::Utils::Hash.new(kwargs)
16
+ hash.assert_valid_keys(:timeout, :browser_width, :browser_height, :selector, :cookies, :proxy_host, :proxy_port)
16
17
 
17
18
  @url = url
18
19
  @cookies = kwargs.delete(:cookies)
@@ -39,14 +40,14 @@ module Gastly
39
40
  %w(timeout browser_width browser_height).each do |name|
40
41
  define_method name do # def timeout
41
42
  instance_variable_get("@#{name}") || # @timeout ||
42
- self.class.const_get("default_#{name}".upcase) # self.class.const_get('DEFAULT_TIMEOUT')
43
+ self.class.const_get("default_#{name}".upcase) # self.class.const_get('DEFAULT_TIMEOUT')
43
44
  end # end
44
45
  end
45
46
 
46
47
  private
47
48
 
48
49
  def proxy_options
49
- return '' if proxy_host.blank? && proxy_port.blank?
50
+ return '' if proxy_host.nil? && proxy_port.nil?
50
51
  "--proxy=#{proxy_host}:#{proxy_port}"
51
52
  end
52
53
 
@@ -59,8 +60,8 @@ module Gastly
59
60
  output: image.path
60
61
  }
61
62
 
62
- params[:selector] = selector if selector.present?
63
- params[:cookies] = parameterize(cookies).join(',') if cookies.present?
63
+ params[:selector] = selector if selector
64
+ params[:cookies] = parameterize(cookies).join(',') if cookies
64
65
 
65
66
  parameterize(params)
66
67
  end
@@ -71,10 +72,11 @@ module Gastly
71
72
  hash.map { |key, value| "#{key}=#{value}" }
72
73
  end
73
74
 
74
- def handle_output(output)
75
+ def handle_output(out)
76
+ output = Gastly::Utils::String.new(out)
75
77
  return unless output.present?
76
78
 
77
- error = case output
79
+ error = case output.string
78
80
  when /^FetchError:(.+)/ then Gastly::FetchError
79
81
  when /^RuntimeError:(.+)/m then Gastly::PhantomJSError
80
82
  else UnknownError
@@ -0,0 +1,2 @@
1
+ require_relative 'utils/hash'
2
+ require_relative 'utils/string'
@@ -0,0 +1,29 @@
1
+ module Gastly
2
+ module Utils
3
+ class Hash
4
+ attr_reader :hash
5
+
6
+ def initialize(hash = {})
7
+ @hash = hash.to_h
8
+ end
9
+
10
+ # Validates all keys in a hash match <tt>*valid_keys</tt>, raising
11
+ # +ArgumentError+ on a mismatch.
12
+ #
13
+ # Note that keys are treated differently than HashWithIndifferentAccess,
14
+ # meaning that string and symbol keys will not match.
15
+ #
16
+ # { name: 'Rob', years: '28' }.assert_valid_keys(:name, :age) # => raises "ArgumentError: Unknown key: :years. Valid keys are: :name, :age"
17
+ # { name: 'Rob', age: '28' }.assert_valid_keys('name', 'age') # => raises "ArgumentError: Unknown key: :name. Valid keys are: 'name', 'age'"
18
+ # { name: 'Rob', age: '28' }.assert_valid_keys(:name, :age) # => passes, raises nothing
19
+ def assert_valid_keys(*valid_keys)
20
+ valid_keys.flatten!
21
+ hash.each_key do |k|
22
+ unless valid_keys.include?(k)
23
+ fail ArgumentError.new("Unknown key: #{k.inspect}. Valid keys are: #{valid_keys.map(&:inspect).join(', ')}")
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,36 @@
1
+ module Gastly
2
+ module Utils
3
+ class String
4
+ BLANK_RE = /\A[[:space:]]*\z/
5
+
6
+ attr_reader :string
7
+
8
+ def initialize(string = '')
9
+ @string = string.to_s
10
+ end
11
+
12
+ # A string is blank if it's empty or contains whitespaces only:
13
+ #
14
+ # ''.blank? # => true
15
+ # ' '.blank? # => true
16
+ # "\t\n\r".blank? # => true
17
+ # ' blah '.blank? # => false
18
+ #
19
+ # Unicode whitespace is supported:
20
+ #
21
+ # "\u00a0".blank? # => true
22
+ #
23
+ # @return [true, false]
24
+ def blank?
25
+ BLANK_RE === string
26
+ end
27
+
28
+ # An object is present if it's not blank.
29
+ #
30
+ # @return [true, false]
31
+ def present?
32
+ !blank?
33
+ end
34
+ end
35
+ end
36
+ end
@@ -1,3 +1,3 @@
1
1
  module Gastly
2
- VERSION = '0.3.0'
2
+ VERSION = '1.0.0'
3
3
  end
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.3.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mikhail Grachev
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-01-29 00:00:00.000000000 Z
11
+ date: 2016-07-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -94,20 +94,6 @@ dependencies:
94
94
  - - "~>"
95
95
  - !ruby/object:Gem::Version
96
96
  version: '4.2'
97
- - !ruby/object:Gem::Dependency
98
- name: activesupport
99
- requirement: !ruby/object:Gem::Requirement
100
- requirements:
101
- - - ">="
102
- - !ruby/object:Gem::Version
103
- version: '3.1'
104
- type: :runtime
105
- prerelease: false
106
- version_requirements: !ruby/object:Gem::Requirement
107
- requirements:
108
- - - ">="
109
- - !ruby/object:Gem::Version
110
- version: '3.1'
111
97
  description: Create screenshots or previews of web pages using Gastly. Under the hood
112
98
  Phantom.js and MiniMagick.
113
99
  email:
@@ -135,6 +121,9 @@ files:
135
121
  - lib/gastly/phantomjs_patch.rb
136
122
  - lib/gastly/screenshot.rb
137
123
  - lib/gastly/script.js
124
+ - lib/gastly/utils.rb
125
+ - lib/gastly/utils/hash.rb
126
+ - lib/gastly/utils/string.rb
138
127
  - lib/gastly/version.rb
139
128
  homepage: https://github.com/mgrachev/gastly
140
129
  licenses: []