From e7f0bce7caa0e0af33addc41d7f36e8b8ecd233b Mon Sep 17 00:00:00 2001 From: Mikhail Grachev Date: Sat, 16 Jul 2016 14:35:20 +0300 Subject: [PATCH] Remove support ActiveSupport :tada: --- Gemfile | 2 +- gastly.gemspec | 1 - lib/gastly.rb | 3 +-- lib/gastly/exceptions.rb | 2 -- lib/gastly/phantomjs_patch.rb | 34 +++++++++++++++++++------------- lib/gastly/screenshot.rb | 16 ++++++++------- lib/gastly/utils.rb | 2 ++ lib/gastly/utils/hash.rb | 29 +++++++++++++++++++++++++++ lib/gastly/utils/string.rb | 36 ++++++++++++++++++++++++++++++++++ spec/gastly/screenshot_spec.rb | 2 +- 10 files changed, 99 insertions(+), 28 deletions(-) create mode 100644 lib/gastly/utils.rb create mode 100644 lib/gastly/utils/hash.rb create mode 100644 lib/gastly/utils/string.rb diff --git a/Gemfile b/Gemfile index ed8adc6..f0824f6 100644 --- a/Gemfile +++ b/Gemfile @@ -3,4 +3,4 @@ source 'https://rubygems.org' # Specify your gem's dependencies in gastly.gemspec gemspec -gem 'coveralls', require: false \ No newline at end of file +gem 'coveralls', require: false diff --git a/gastly.gemspec b/gastly.gemspec index aaf0ef0..b91891c 100644 --- a/gastly.gemspec +++ b/gastly.gemspec @@ -25,5 +25,4 @@ Gem::Specification.new do |spec| spec.add_dependency 'phantomjs', '~> 2.1.1' spec.add_dependency 'mini_magick', '~> 4.2' - spec.add_dependency 'activesupport', '>= 3.1' end diff --git a/lib/gastly.rb b/lib/gastly.rb index eae712e..97aa9ee 100644 --- a/lib/gastly.rb +++ b/lib/gastly.rb @@ -1,8 +1,7 @@ require 'phantomjs' require 'mini_magick' -require 'active_support/core_ext/hash/keys' -require 'active_support/core_ext/object/blank' +require_relative 'gastly/utils' require_relative 'gastly/phantomjs_patch' require_relative 'gastly/image' require_relative 'gastly/screenshot' diff --git a/lib/gastly/exceptions.rb b/lib/gastly/exceptions.rb index 44e44d6..1961361 100644 --- a/lib/gastly/exceptions.rb +++ b/lib/gastly/exceptions.rb @@ -1,7 +1,5 @@ module Gastly class FetchError < StandardError - attr_reader :url - def initialize(url) super("Unable to load #{url}") end diff --git a/lib/gastly/phantomjs_patch.rb b/lib/gastly/phantomjs_patch.rb index 035e385..96801f3 100644 --- a/lib/gastly/phantomjs_patch.rb +++ b/lib/gastly/phantomjs_patch.rb @@ -4,6 +4,8 @@ class << self end class Platform + RETRY_COUNT = 5 + class << self def install! STDERR.puts "Phantomjs does not appear to be installed in #{phantomjs_path}, installing!" @@ -16,18 +18,18 @@ def install! FileUtils.mkdir_p temp_dir Dir.chdir temp_dir do - unless download_via_curl or download_via_wget - 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" + unless download_via_curl || download_via_wget + 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" end case package_url.split('.').last - when 'bz2' - system "bunzip2 #{File.basename(package_url)}" - system "tar xf #{File.basename(package_url).sub(/\.bz2$/, '')}" - when 'zip' - system "unzip #{File.basename(package_url)}" - else - raise "Unknown compression format for #{File.basename(package_url)}" + when 'bz2' + system "bunzip2 #{File.basename(package_url)}" + system "tar xf #{File.basename(package_url).sub(/\.bz2$/, '')}" + when 'zip' + system "unzip #{File.basename(package_url)}" + else + fail "Unknown compression format for #{File.basename(package_url)}" end # Find the phantomjs build we just extracted @@ -52,28 +54,32 @@ def install! end end - raise 'Failed to install phantomjs. Sorry :(' unless File.exist?(phantomjs_path) + fail 'Failed to install phantomjs. Sorry :(' unless File.exist?(phantomjs_path) end private def download_via_curl - system "curl -L -O #{package_url} #{curl_proxy_options}" + system "curl -L --retry #{RETRY_COUNT} -O #{package_url} #{curl_proxy_options}" end def download_via_wget - system "wget #{package_url} #{wget_proxy_options}" + system "wget -t #{RETRY_COUNT} #{package_url} #{wget_proxy_options}" end def curl_proxy_options - return '' if Phantomjs.proxy_host.nil? && Phantomjs.proxy_port.nil? + return '' if proxy_options_exist? "-x #{Phantomjs.proxy_host}:#{Phantomjs.proxy_port}" end def wget_proxy_options - return '' if Phantomjs.proxy_host.nil? && Phantomjs.proxy_port.nil? + return '' if proxy_options_exist? "-e use_proxy=yes -e http_proxy=#{Phantomjs.proxy_host}:#{Phantomjs.proxy_port}" end + + def proxy_options_exist? + Phantomjs.proxy_host.nil? && Phantomjs.proxy_port.nil? + end end end end diff --git a/lib/gastly/screenshot.rb b/lib/gastly/screenshot.rb index ad85bac..2058375 100644 --- a/lib/gastly/screenshot.rb +++ b/lib/gastly/screenshot.rb @@ -12,7 +12,8 @@ class Screenshot # @param url [String] The full url to the site def initialize(url, **kwargs) - kwargs.assert_valid_keys(:timeout, :browser_width, :browser_height, :selector, :cookies, :proxy_host, :proxy_port) + hash = Gastly::Utils::Hash.new(kwargs) + hash.assert_valid_keys(:timeout, :browser_width, :browser_height, :selector, :cookies, :proxy_host, :proxy_port) @url = url @cookies = kwargs.delete(:cookies) @@ -39,14 +40,14 @@ def capture %w(timeout browser_width browser_height).each do |name| define_method name do # def timeout instance_variable_get("@#{name}") || # @timeout || - self.class.const_get("default_#{name}".upcase) # self.class.const_get('DEFAULT_TIMEOUT') + self.class.const_get("default_#{name}".upcase) # self.class.const_get('DEFAULT_TIMEOUT') end # end end private def proxy_options - return '' if proxy_host.blank? && proxy_port.blank? + return '' if proxy_host.nil? && proxy_port.nil? "--proxy=#{proxy_host}:#{proxy_port}" end @@ -59,8 +60,8 @@ def prepared_params output: image.path } - params[:selector] = selector if selector.present? - params[:cookies] = parameterize(cookies).join(',') if cookies.present? + params[:selector] = selector if selector + params[:cookies] = parameterize(cookies).join(',') if cookies parameterize(params) end @@ -71,10 +72,11 @@ def parameterize(hash) hash.map { |key, value| "#{key}=#{value}" } end - def handle_output(output) + def handle_output(out) + output = Gastly::Utils::String.new(out) return unless output.present? - error = case output + error = case output.string when /^FetchError:(.+)/ then Gastly::FetchError when /^RuntimeError:(.+)/m then Gastly::PhantomJSError else UnknownError diff --git a/lib/gastly/utils.rb b/lib/gastly/utils.rb new file mode 100644 index 0000000..d66f2f8 --- /dev/null +++ b/lib/gastly/utils.rb @@ -0,0 +1,2 @@ +require_relative 'utils/hash' +require_relative 'utils/string' diff --git a/lib/gastly/utils/hash.rb b/lib/gastly/utils/hash.rb new file mode 100644 index 0000000..8293858 --- /dev/null +++ b/lib/gastly/utils/hash.rb @@ -0,0 +1,29 @@ +module Gastly + module Utils + class Hash + attr_reader :hash + + def initialize(hash = {}) + @hash = hash.to_h + end + + # Validates all keys in a hash match *valid_keys, raising + # +ArgumentError+ on a mismatch. + # + # Note that keys are treated differently than HashWithIndifferentAccess, + # meaning that string and symbol keys will not match. + # + # { name: 'Rob', years: '28' }.assert_valid_keys(:name, :age) # => raises "ArgumentError: Unknown key: :years. Valid keys are: :name, :age" + # { name: 'Rob', age: '28' }.assert_valid_keys('name', 'age') # => raises "ArgumentError: Unknown key: :name. Valid keys are: 'name', 'age'" + # { name: 'Rob', age: '28' }.assert_valid_keys(:name, :age) # => passes, raises nothing + def assert_valid_keys(*valid_keys) + valid_keys.flatten! + hash.each_key do |k| + unless valid_keys.include?(k) + fail ArgumentError.new("Unknown key: #{k.inspect}. Valid keys are: #{valid_keys.map(&:inspect).join(', ')}") + end + end + end + end + end +end diff --git a/lib/gastly/utils/string.rb b/lib/gastly/utils/string.rb new file mode 100644 index 0000000..86a440e --- /dev/null +++ b/lib/gastly/utils/string.rb @@ -0,0 +1,36 @@ +module Gastly + module Utils + class String + BLANK_RE = /\A[[:space:]]*\z/ + + attr_reader :string + + def initialize(string = '') + @string = string.to_s + end + + # A string is blank if it's empty or contains whitespaces only: + # + # ''.blank? # => true + # ' '.blank? # => true + # "\t\n\r".blank? # => true + # ' blah '.blank? # => false + # + # Unicode whitespace is supported: + # + # "\u00a0".blank? # => true + # + # @return [true, false] + def blank? + BLANK_RE === string + end + + # An object is present if it's not blank. + # + # @return [true, false] + def present? + !blank? + end + end + end +end diff --git a/spec/gastly/screenshot_spec.rb b/spec/gastly/screenshot_spec.rb index 26e4055..c46391f 100644 --- a/spec/gastly/screenshot_spec.rb +++ b/spec/gastly/screenshot_spec.rb @@ -118,4 +118,4 @@ expect(screenshot.browser_height).to eq 720 end end -end \ No newline at end of file +end