browserstack-local 0.1.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
+ SHA512:
3
+ metadata.gz: f4445863dbc5f71843fa4794855e95505ef65ddb402b74ae668ed0b67d9c6fda8db2de5fa9f5954deaaf51ada3f870a883e5a12c963a6cc0ffb6681593a21ce2
4
+ data.tar.gz: d8b41e259ce95761bc3d4d0a1e5374358c4f8144267a1da8cfacade7c8643e256393aed3a596b5105395407234d12b8358dd6ba7d9ada73742f1247deab979bc
5
+ SHA1:
6
+ metadata.gz: 65c5e18b24c7a27508774143b8216f6fe53287cd
7
+ data.tar.gz: 31a1d287582f92b6d1d5756ce83c9d98070d72f3
@@ -0,0 +1,136 @@
1
+ require 'browserstack/localbinary'
2
+ require 'browserstack/localexception'
3
+
4
+ module BrowserStack
5
+
6
+ class Local
7
+ attr_reader :pid
8
+
9
+ def initialize(key = ENV["BROWSERSTACK_ACCESS_KEY"])
10
+ @key = key
11
+ @user_arguments = []
12
+ @logfile = File.join(Dir.pwd, "local.log")
13
+ @is_windows = RbConfig::CONFIG['host_os'].match(/mswin|msys|mingw|cygwin|bccwin|wince|emc|win32/)
14
+ @exec = @is_windows ? "call" : "exec";
15
+ end
16
+
17
+ def add_args(key, value=nil)
18
+ if key == "key"
19
+ @key = value
20
+ elsif key == "v" && value.to_s != "false"
21
+ @verbose_flag = "-vvv"
22
+ elsif key == "force" && value.to_s != "false"
23
+ @force_flag = "-force"
24
+ elsif key == "only" && value.to_s != "false"
25
+ @only_flag = "-only"
26
+ elsif key == "onlyAutomate" && value.to_s != "false"
27
+ @only_automate_flag = "-onlyAutomate"
28
+ elsif key == "forcelocal" && value.to_s != "false"
29
+ @force_local_flag = "-forcelocal"
30
+ elsif key == "localIdentifier"
31
+ @local_identifier_flag = "-localIdentifier '#{value}'"
32
+ elsif key == "f"
33
+ @folder_flag = "-f"
34
+ @folder_path = "'#{value}'"
35
+ elsif key == "proxyHost"
36
+ @proxy_host = "-proxyHost '#{value}'"
37
+ elsif key == "proxyPort"
38
+ @proxy_port = "-proxyPort #{value}"
39
+ elsif key == "proxyUser"
40
+ @proxy_user = "-proxyUser '#{value}'"
41
+ elsif key == "proxyPass"
42
+ @proxy_pass = "-proxyPass '#{value}'"
43
+ elsif key == "hosts"
44
+ @hosts = value
45
+ elsif key == "logfile"
46
+ @logfile = value
47
+ elsif key == "binarypath"
48
+ @binary_path = value
49
+ elsif key == "forceproxy" && value.to_s != "false"
50
+ @force_proxy_flag = "-forceproxy"
51
+ else
52
+ if value.to_s.downcase.eql?("true")
53
+ @user_arguments << "-#{key}"
54
+ else
55
+ @user_arguments << "-#{key} '#{value}'"
56
+ end
57
+ end
58
+ end
59
+
60
+ def start(options = {})
61
+ options.each_pair do |key, value|
62
+ self.add_args(key, value)
63
+ end
64
+
65
+ @binary_path = if @binary_path.nil?
66
+ BrowserStack::LocalBinary.new.binary_path
67
+ else
68
+ @binary_path
69
+ end
70
+
71
+ if @is_windows
72
+ system("echo > #{@logfile}")
73
+ else
74
+ system("echo '' > '#{@logfile}'")
75
+ end
76
+
77
+ if defined? spawn
78
+ @process = IO.popen(command_args)
79
+ else
80
+ @process = IO.popen(command)
81
+ end
82
+ @stdout = File.open(@logfile, "r")
83
+
84
+ while true
85
+ begin
86
+ line = @stdout.readline
87
+ rescue EOFError => e
88
+ sleep 1
89
+ next
90
+ end
91
+ break if line.nil?
92
+ if line.match(/\*\*\* Error\:/)
93
+ @stdout.close
94
+ raise BrowserStack::LocalException.new(line)
95
+ return
96
+ end
97
+ if line.strip == "Press Ctrl-C to exit"
98
+ @pid = @process.pid
99
+ @stdout.close
100
+ break
101
+ end
102
+ end
103
+
104
+ while true
105
+ break if self.isRunning
106
+ sleep 1
107
+ end
108
+ end
109
+
110
+ def isRunning
111
+ return true if (!@pid.nil? && Process.kill(0, @pid)) rescue false
112
+ end
113
+
114
+ def stop
115
+ return if @pid.nil?
116
+ Process.kill("TERM", @pid) rescue Process.kill(9, @pid)
117
+ @process.close
118
+ @pid = nil if @is_windows
119
+ while self.isRunning
120
+ sleep 1
121
+ end
122
+ end
123
+
124
+ def command
125
+ "#{@exec} #{@binary_path} -logFile '#{@logfile}' #{@folder_flag} #{@key} #{@folder_path} #{@force_local_flag} #{@local_identifier_flag} #{@only_flag} #{@only_automate_flag} #{@proxy_host} #{@proxy_port} #{@proxy_user} #{@proxy_pass} #{@force_proxy_flag} #{@force_flag} #{@verbose_flag} #{@hosts} #{@user_arguments.join(" ")}".strip
126
+ end
127
+
128
+ def command_args
129
+ args = ["#{@binary_path}", "-logFile", "#{@logfile}", "#{@key}", "#{@folder_flag}", "#{@folder_path}", "#{@force_local_flag}", "#{@local_identifier_flag}", "#{@only_flag}", "#{@only_automate_flag}", "#{@proxy_host}", "#{@proxy_port}", "#{@proxy_user}", "#{@proxy_pass}", "#{@force_proxy_flag}","#{@force_flag}", "#{@verbose_flag}", "#{@hosts}", "#{@user_arguments.join(" ")}"]
130
+ args = args.select {|a| a.to_s != "" }
131
+ args.push(:err => [:child, :out])
132
+ args
133
+ end
134
+ end
135
+
136
+ end
@@ -0,0 +1,88 @@
1
+ require 'net/http'
2
+ require 'net/https'
3
+ require 'rbconfig'
4
+ require 'openssl'
5
+ require 'tmpdir'
6
+ require 'browserstack/localexception'
7
+
8
+ module BrowserStack
9
+
10
+ class LocalBinary
11
+ def initialize
12
+ host_os = RbConfig::CONFIG['host_os']
13
+ @http_path = case host_os
14
+ when /mswin|msys|mingw|cygwin|bccwin|wince|emc/
15
+ @windows = true
16
+ "https://s3.amazonaws.com/browserStack/browserstack-local/BrowserStackLocal.exe"
17
+ when /darwin|mac os/
18
+ "https://s3.amazonaws.com/browserStack/browserstack-local/BrowserStackLocal-darwin-x64"
19
+ when /linux/
20
+ if 1.size == 8
21
+ "https://s3.amazonaws.com/browserStack/browserstack-local/BrowserStackLocal-linux-x64"
22
+ else
23
+ "https://s3.amazonaws.com/browserStack/browserstack-local/BrowserStackLocal-linux-ia32"
24
+ end
25
+ end
26
+
27
+ @ordered_paths = [
28
+ File.join(File.expand_path('~'), '.browserstack'),
29
+ Dir.pwd,
30
+ Dir.tmpdir
31
+ ]
32
+ end
33
+
34
+ def download(dest_parent_dir)
35
+ unless File.exists? dest_parent_dir
36
+ Dir.mkdir dest_parent_dir
37
+ end
38
+ uri = URI.parse(@http_path)
39
+ binary_path = File.join(dest_parent_dir, "BrowserStackLocal#{".exe" if @windows}")
40
+ http = Net::HTTP.new(uri.host, uri.port)
41
+ http.use_ssl = true
42
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
43
+
44
+ res = http.get(uri.path)
45
+ file = open(binary_path, 'w')
46
+ file.write(res.body)
47
+ file.close
48
+ FileUtils.chmod 0755, binary_path
49
+
50
+ binary_path
51
+ end
52
+
53
+ def binary_path
54
+ dest_parent_dir = get_available_dirs
55
+ binary_path = File.join(dest_parent_dir, "BrowserStackLocal#{".exe" if @windows}")
56
+ if File.exists? binary_path
57
+ binary_path
58
+ else
59
+ download(dest_parent_dir)
60
+ end
61
+ end
62
+
63
+ private
64
+
65
+ def get_available_dirs
66
+ i = 0
67
+ while i < @ordered_paths.size
68
+ path = @ordered_paths[i]
69
+ if make_path(path)
70
+ return path
71
+ else
72
+ i += 1
73
+ end
74
+ end
75
+ raise BrowserStack::LocalException.new('Error trying to download BrowserStack Local binary')
76
+ end
77
+
78
+ def make_path(path)
79
+ begin
80
+ FileUtils.mkdir_p path if !File.directory?(path)
81
+ return true
82
+ rescue Exception
83
+ return false
84
+ end
85
+ end
86
+ end
87
+
88
+ end
metadata ADDED
@@ -0,0 +1,53 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: browserstack-local
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - ""
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2016-05-01 00:00:00 Z
13
+ dependencies: []
14
+
15
+ description: Ruby bindings for BrowserStack Local
16
+ email: support@browserstack.com
17
+ executables: []
18
+
19
+ extensions: []
20
+
21
+ extra_rdoc_files: []
22
+
23
+ files:
24
+ - lib/browserstack/local.rb
25
+ - lib/browserstack/localbinary.rb
26
+ homepage: http://rubygems.org/gems/browserstack-local
27
+ licenses:
28
+ - MIT
29
+ metadata: {}
30
+
31
+ post_install_message:
32
+ rdoc_options: []
33
+
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - &id001
39
+ - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: "0"
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - *id001
45
+ requirements: []
46
+
47
+ rubyforge_project:
48
+ rubygems_version: 2.0.14
49
+ signing_key:
50
+ specification_version: 4
51
+ summary: BrowserStack Local
52
+ test_files: []
53
+