phantomjs-helper 1.0.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f14a442f426beabbc56f22f50c53eec8e7025e80
4
+ data.tar.gz: 4b2fdb20b403f963ca01bf200e9bc737013c5d29
5
+ SHA512:
6
+ metadata.gz: 35c0adbe230238f60973c745854b48d16e381e277b64fdf6195e09eefb8744c6798f9fc4f6a56ce5db172f938b790bdb80f7a55c3378e8e62fa3553052fd91d9
7
+ data.tar.gz: 4d9a858cccb23923eda35c02982c57d31bec8ae4a2e0186659d5c534e5e1201f2a3d14dca8efb0c2c78e5c3ad087fa37e10aabb3943ccf3b6fa6008b4c249875
data/.gitignore ADDED
@@ -0,0 +1,6 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ chromedriver.log
6
+ .idea/
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.0
data/CHANGELOG.md ADDED
@@ -0,0 +1,7 @@
1
+ Changelog
2
+ ==========
3
+
4
+ 1.0.0 - 19 July 2016
5
+ ----------
6
+
7
+ * Birthday!
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in chromedriver-helper.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ (The MIT License)
2
+
3
+ Copyright (c) 2016: Rasmus Bergholdt
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ 'Software'), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,48 @@
1
+ # phantomjs-helper
2
+
3
+ [![Build status](https://api.travis-ci.org/bergholdt/phantomjs-helper.svg)](https://travis-ci.org/bergholdt/phantomjs-helper)
4
+
5
+ Easy installation and use of [PhantomJS](http://phantomjs.org).
6
+
7
+ * [http://github.com/bergholdt/phantomjs-helper](http://github.com/bergholdt/phantomjs-helper)
8
+
9
+
10
+ # Description
11
+
12
+ `phantomjs-helper` installs an executable, `phantomjs`, in your
13
+ gem path.
14
+
15
+ This script will, if necessary, download the appropriate binary for
16
+ your platform and install it into `~/.phantomjs-helper`, then exec
17
+ it. Easy peasy!
18
+
19
+
20
+ # Usage
21
+
22
+ If you're using Bundler, it's as easy as:
23
+
24
+ # Gemfile
25
+ gem "phantomjs-helper"
26
+
27
+ # Updating phantomjs
28
+
29
+ If you'd like to force-upgrade to the latest version of phantomjs,
30
+ run the script `phantomjs-update` that also comes packaged with
31
+ this gem.
32
+
33
+
34
+ # Support
35
+
36
+ The code lives at
37
+ [http://github.com/bergholdt/phantomjs-helper](http://github.com/bergholdt/phantomjs-helper).
38
+ Open a Github Issue, or send a pull request! Thanks! You're the best.
39
+
40
+
41
+ # License
42
+
43
+ MIT licensed, see LICENSE.txt for full details.
44
+
45
+
46
+ # Credit
47
+
48
+ This gem is heavy inspired by http://github.com/flavorjones/chromedriver-helper
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/phantomjs ADDED
@@ -0,0 +1,5 @@
1
+ #! /usr/bin/env ruby
2
+
3
+ require "phantomjs/helper"
4
+
5
+ Phantomjs::Helper.new.run *ARGV
@@ -0,0 +1,5 @@
1
+ #! /usr/bin/env ruby
2
+
3
+ require "phantomjs/helper"
4
+
5
+ Phantomjs::Helper.new.update
@@ -0,0 +1,38 @@
1
+ require 'nokogiri'
2
+ require 'open-uri'
3
+
4
+ module Phantomjs
5
+ class Helper
6
+ class BitbucketDownloadParser
7
+
8
+ attr_reader :source, :driver_name, :url
9
+
10
+ def initialize(driver_name, url:, open_uri_provider: OpenURI)
11
+ @driver_name = driver_name
12
+ @url = url
13
+ @source = open_uri_provider.open_uri(url)
14
+ end
15
+
16
+ def downloads
17
+ doc = Nokogiri::XML.parse(source)
18
+ items = doc.css('table#uploaded-files tr.iterable-item td.name a.execute').collect {|k| {text:k.text, url:k[:href]} }
19
+ items.reject! {|k| !(driver_name===k[:text]) }
20
+ items.map {|k| URI.parse(url).tap {|u|u.path = k[:url]}.to_s}
21
+ end
22
+
23
+ def newest_download
24
+ (downloads.sort { |a, b| version_of(a) <=> version_of(b)}).last
25
+ end
26
+
27
+ private
28
+
29
+ def version_of(url)
30
+ Gem::Version.new grab_version_string_from(url)
31
+ end
32
+
33
+ def grab_version_string_from(url)
34
+ url.match(/-(\d+\.?\d+\.?\d+)\-/).captures.first
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,5 @@
1
+ module Phantomjs
2
+ class Helper
3
+ VERSION = "1.0.0"
4
+ end
5
+ end
@@ -0,0 +1,108 @@
1
+ require 'phantomjs/helper/version'
2
+ require 'phantomjs/helper/bitbucket_download_parser'
3
+ require 'fileutils'
4
+ require 'rbconfig'
5
+ require 'open-uri'
6
+ require 'zip'
7
+ require 'ffi-libarchive'
8
+
9
+ module Phantomjs
10
+ class Helper
11
+ BUCKET_URL = 'https://bitbucket.org/ariya/phantomjs/downloads'
12
+
13
+ def run(*args)
14
+ download
15
+ exec binary_path, *args
16
+ end
17
+
18
+ def update
19
+ download true
20
+ end
21
+
22
+ def download(hit_network=false)
23
+ return if File.exists?(binary_path) && !hit_network
24
+ url = download_url
25
+ filename = File.basename url
26
+ FileUtils.mkdir_p install_dir
27
+ Dir.chdir install_dir do
28
+ FileUtils.rm_f filename
29
+ File.open(filename, 'wb') do |saved_file|
30
+ URI.parse(url).open('rb') do |read_file|
31
+ saved_file.write(read_file.read)
32
+ end
33
+ end
34
+ raise "Could not download #{url}" unless File.exists? filename
35
+ extract filename
36
+ FileUtils.rm_f filename
37
+ end
38
+ raise "Could not unzip #{filename} to get #{binary_path}" unless File.exists? binary_path
39
+ FileUtils.chmod 'ugo+rx', binary_path
40
+ end
41
+
42
+ def remove
43
+ FileUtils.rm binary_path
44
+ end
45
+
46
+ def binary_path
47
+ if platform == 'windows'
48
+ File.join install_dir, 'phantomjs.exe'
49
+ else
50
+ File.join install_dir, 'phantomjs'
51
+ end
52
+ end
53
+
54
+ private
55
+
56
+ def extract(filename)
57
+ case File.extname(filename)
58
+ when '.zip'
59
+ Zip::File.open(filename) do |zip_file|
60
+ entry = zip_file.glob('**/bin/phantomjs*').first
61
+ raise "Could not find phantomjs binary in zip archive #{filename}" unless entry
62
+ FileUtils.rm_f entry.name
63
+ entry.extract(File.basename(entry.name))
64
+ end
65
+ when '.bz2'
66
+ Archive.read_open_filename(filename) do |ar|
67
+ ar.each_entry do |e|
68
+ if e.pathname.include?('bin/phantomjs')
69
+ File.open(binary_path, 'wb') do |saved_file|
70
+ saved_file.write(ar.read_data)
71
+ end
72
+ end
73
+ end
74
+ end
75
+ else
76
+ raise "Do not know how to extract binary from #{filename}"
77
+ end
78
+ end
79
+
80
+ def driver_name
81
+ /phantomjs-.*-#{platform}/
82
+ end
83
+
84
+ def download_url
85
+ BitbucketDownloadParser.new(driver_name, url: BUCKET_URL).newest_download
86
+ end
87
+
88
+ def install_dir
89
+ File.expand_path File.join(home_dir, '.phantomjs-helper', platform)
90
+ end
91
+
92
+ def platform
93
+ case RbConfig::CONFIG['host_os']
94
+ when /linux/ then
95
+ RbConfig::CONFIG['host_cpu'] =~ /x86_64|amd64/ ? 'linux-x86_64' : 'linux-i686'
96
+ when /darwin/ then
97
+ 'macosx'
98
+ else
99
+ 'windows'
100
+ end
101
+ end
102
+
103
+ def home_dir
104
+ ENV['HOME']
105
+ end
106
+
107
+ end
108
+ end
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path('../lib', __FILE__)
3
+ require 'phantomjs/helper/version'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'phantomjs-helper'
7
+ s.version = Phantomjs::Helper::VERSION
8
+ s.authors = ['Rasmus Bergholdt']
9
+ s.email = ['rasmus.bergholdt@gmail.com']
10
+ s.homepage = 'https://github.com/bergholdt/phantomjs-helper'
11
+ s.summary = 'Easy installation and use of Phantomjs.'
12
+ s.description = 'Easy installation and use of Phantomjs.'
13
+ s.licenses = ['MIT']
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+ s.require_paths = ['lib']
19
+
20
+ s.add_development_dependency 'rspec', '~> 3.0'
21
+ s.add_development_dependency 'rake', '~> 11.2'
22
+
23
+ s.add_runtime_dependency 'nokogiri', '~> 1.6'
24
+ s.add_runtime_dependency 'rubyzip'
25
+ s.add_runtime_dependency 'ffi-libarchive'
26
+ end