Skip to content
This repository has been archived by the owner on Jan 4, 2024. It is now read-only.

Commit

Permalink
Remove support ActiveSupport 🎉
Browse files Browse the repository at this point in the history
  • Loading branch information
mgrachev committed Jul 16, 2016
1 parent e0f60a4 commit e7f0bce
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 28 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ source 'https://rubygems.org'
# Specify your gem's dependencies in gastly.gemspec
gemspec

gem 'coveralls', require: false
gem 'coveralls', require: false
1 change: 0 additions & 1 deletion gastly.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 1 addition & 2 deletions lib/gastly.rb
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
2 changes: 0 additions & 2 deletions lib/gastly/exceptions.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
module Gastly
class FetchError < StandardError
attr_reader :url

def initialize(url)
super("Unable to load #{url}")
end
Expand Down
34 changes: 20 additions & 14 deletions lib/gastly/phantomjs_patch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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!"
Expand All @@ -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
Expand All @@ -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
16 changes: 9 additions & 7 deletions lib/gastly/screenshot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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

Expand All @@ -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
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions lib/gastly/utils.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require_relative 'utils/hash'
require_relative 'utils/string'
29 changes: 29 additions & 0 deletions lib/gastly/utils/hash.rb
Original file line number Diff line number Diff line change
@@ -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 <tt>*valid_keys</tt>, 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
36 changes: 36 additions & 0 deletions lib/gastly/utils/string.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion spec/gastly/screenshot_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,4 @@
expect(screenshot.browser_height).to eq 720
end
end
end
end

0 comments on commit e7f0bce

Please sign in to comment.