tee-io 0.0.1

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8ba88dc5d70cdb5fba4808f7a97823bc18933e8d
4
+ data.tar.gz: 9f2957e8af2bbfd52c900471248399d075f32e0b
5
+ SHA512:
6
+ metadata.gz: ad4d5b490948002c410e4b55c1a97151309b7b6b71e94a5fd0d88c4576915cddf6a36c008e0b17f854b1433fe2a42e42a6116c83d86f539bd0126c0e64b104ba
7
+ data.tar.gz: a6735b05c983f9aafa44f7d1b595fe981e51038f69a29433ca9c99fc8476a97fa58978363f8e244d9d3d5c56431b37e2a20777f90069a1f47261793a178459ba
@@ -0,0 +1,28 @@
1
+ require "curb"
2
+ require "json"
3
+ require "posix/spawn"
4
+ require "timeout"
5
+
6
+ require "tee_io/api"
7
+ require "tee_io/process"
8
+ require "tee_io/token_response"
9
+
10
+ module TeeIO
11
+ DEFAULT_URL = "https://tee-io.herokuapp.com"
12
+ DEFAULT_TIMEOUT = 5 * 60
13
+
14
+ def self.run(*command, description: nil, base_url: DEFAULT_URL, timeout: DEFAULT_TIMEOUT)
15
+ api = API.new(base_url)
16
+ resp = api.create_command(description || "#{command.join(" ")}")
17
+ token = TokenResponse.new(resp).token
18
+
19
+ yield "#{base_url}/commands/#{token}"
20
+
21
+ process = Process.new(command, timeout)
22
+ process.run do |_stream, io|
23
+ io.each_line do |line|
24
+ api.create_output(token, line)
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,28 @@
1
+ require "curb"
2
+
3
+ module TeeIO
4
+ class API
5
+ def initialize(base_url)
6
+ @base_url = base_url
7
+ end
8
+
9
+ def create_command(description)
10
+ request(:post, "/commands", description: description)
11
+ end
12
+
13
+ def create_output(token, content)
14
+ request(:post, "/commands/#{token}/output", content: content)
15
+ end
16
+
17
+ private
18
+
19
+ attr_reader :base_url
20
+
21
+ def request(method, path, body)
22
+ Curl.send(method, "#{base_url}#{path}", body.to_json) do |curl|
23
+ curl.headers["Accept"] = "application/json"
24
+ curl.headers["Content-Type"] = "application/json"
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,42 @@
1
+ module TeeIO
2
+ class Process
3
+ def initialize(command, timeout)
4
+ @command = command
5
+ @timeout = timeout
6
+ end
7
+
8
+ def run(&block)
9
+ pid, stdin, out, err = POSIX::Spawn.popen4(*command)
10
+
11
+ stdin.close
12
+ stdout_thread = Thread.new { block.call(:stdout, out) }
13
+ stderr_thread = Thread.new { block.call(:stderr, err) }
14
+
15
+ status = Timeout.timeout(timeout) do
16
+ ::Process.waitpid2(pid)[1]
17
+ end
18
+
19
+ stdout_thread.join
20
+ stderr_thread.join
21
+
22
+ status
23
+ rescue Exception => ex
24
+ if pid
25
+ begin
26
+ ::Process.kill("KILL", pid)
27
+ ::Process.waitpid2(pid)
28
+ rescue Errno::ESRCH
29
+ end
30
+ end
31
+
32
+ stdout_thread.kill if stdout_thread
33
+ stderr_thread.kill if stderr_thread
34
+
35
+ raise
36
+ end
37
+
38
+ private
39
+
40
+ attr_reader :command, :arguments, :timeout
41
+ end
42
+ end
@@ -0,0 +1,34 @@
1
+ module TeeIO
2
+ class TokenResponse
3
+ InvalidResponseError = Class.new(StandardError)
4
+
5
+ def initialize(response)
6
+ code = response.response_code
7
+
8
+ unless [200, 201].include?(code)
9
+ raise_invalid!("#{code} #{response.body_str}")
10
+ end
11
+
12
+ @response = response
13
+ end
14
+
15
+ def token
16
+ json_response["token"] or raise_invalid!("no token present")
17
+ end
18
+
19
+ private
20
+
21
+ attr_reader :response
22
+
23
+ def json_response
24
+ JSON.parse(response.body_str)
25
+ rescue JSON::ParserError => ex
26
+ raise_invalid!("JSON parse error: #{ex.message}")
27
+ end
28
+
29
+ def raise_invalid!(message)
30
+ raise InvalidResponseError,
31
+ "Unable to start command #{message}"
32
+ end
33
+ end
34
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tee-io
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Pat Brisbin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-12-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: curb
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.8.8
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.8.8
27
+ - !ruby/object:Gem::Dependency
28
+ name: posix-spawn
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.3.11
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.3.11
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: webmock
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Client SDK for interfacing with tee.io
84
+ email: pbrisbin@gmail.com
85
+ executables: []
86
+ extensions: []
87
+ extra_rdoc_files: []
88
+ files:
89
+ - lib/tee_io.rb
90
+ - lib/tee_io/api.rb
91
+ - lib/tee_io/process.rb
92
+ - lib/tee_io/token_response.rb
93
+ homepage: https://tee-io.herokuapp.com
94
+ licenses:
95
+ - MIT
96
+ metadata: {}
97
+ post_install_message:
98
+ rdoc_options: []
99
+ require_paths:
100
+ - lib
101
+ required_ruby_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ requirements: []
112
+ rubyforge_project:
113
+ rubygems_version: 2.4.5
114
+ signing_key:
115
+ specification_version: 4
116
+ summary: tee.io client SDK
117
+ test_files: []