playtypus 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9aec06b1f7368bf6ff41307b3233f48fe739a017
4
+ data.tar.gz: 228631e1ae380172ee68b4723ecbeccca4360a7b
5
+ SHA512:
6
+ metadata.gz: cea6432424ebb439fb7a9d17df48b5b8c65200fa49e62c96c33fb4ad2b462dfdd5527ffe875c9b437882fb0180a9f218db4a033ed34d5624a8b7da9d800df6da
7
+ data.tar.gz: 3bf11b8b37fa84ed25d97aefe8cbb0b62effed5072ec398e66678d1b7911ad24f4101433a02e21526a3b4ad5cbf7434ab33f19be20fba1f88511099f6aa3c83c
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/MIT-LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2015 Rackspace, Inc.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,2 @@
1
+ # playtypus
2
+ The Playtypus is an eventmachine-based command line utility that plays HTTP calls
data/ascii.rb ADDED
@@ -0,0 +1,21 @@
1
+ ascii = <<-EOH
2
+
3
+ -/- /_ _
4
+ _/__/ (__(/_
5
+
6
+ _
7
+ ,_ // __, -/- ,_ ,
8
+ _/_)__(/_(_/(__(_/__/__(_/__/_)__(_/__/_)_
9
+ / _/_ _/_ /
10
+ / (/ (/ /
11
+
12
+
13
+ _.-^~~^^^`~-,_,,~''''''```~,''``~'``~,
14
+ ______,' -o :. _ . ; ,'`, `.
15
+ ( -\.._,.;;'._ ,( } _`_-_,, `, `,
16
+ ``~~~~~~' ((/'((((____/~~~~~~'(,(,___> `~'
17
+
18
+
19
+ EOH
20
+
21
+ puts ascii
data/bin/playtypus ADDED
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/env ruby
2
+ require "pathname"
3
+ pn = Pathname.new(__FILE__)
4
+ bin_file = pn.realpath
5
+ $:.unshift File.expand_path("../../lib", bin_file)
6
+
7
+ require 'playtypus'
8
+
9
+ root = File.expand_path('../..', bin_file)
10
+ ENV['BUNDLE_GEMFILE'] = "#{root}/Gemfile"
11
+
12
+ Playtypus::CLI.source_root(File.expand_path('../../', bin_file))
13
+ Playtypus::CLI.start(ARGV)
@@ -0,0 +1,38 @@
1
+ require 'json'
2
+ require 'time'
3
+
4
+ module Playtypus
5
+ class Call
6
+ attr_accessor :timestamp,
7
+ :path,
8
+ :verb,
9
+ :headers,
10
+ :body
11
+
12
+ def self.from_hash(hash)
13
+ return self.new(hash['timestamp'], hash['path'], hash['verb'], hash['headers'], hash['body'])
14
+ end
15
+
16
+ def initialize(timestamp, path, verb, headers, body)
17
+ @timestamp = Time.iso8601(timestamp)
18
+ @path = path
19
+ @verb = verb
20
+ @headers = headers
21
+ @body = body
22
+ end
23
+
24
+ def to_hash
25
+ {
26
+ 'timestamp' => @timestamp,
27
+ 'path' => @path,
28
+ 'verb' => @verb,
29
+ 'headers' => @headers,
30
+ 'body' => @body
31
+ }
32
+ end
33
+
34
+ def to_s
35
+ self.to_hash.to_json
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,22 @@
1
+ require 'json'
2
+
3
+ module Playtypus
4
+ class CallContainer
5
+
6
+ attr_accessor :calls
7
+
8
+ def self.from_log(log_content)
9
+ calls = []
10
+ json = JSON.parse(log_content.force_encoding("utf-8"))
11
+ json.each do |log_entry|
12
+ calls << Playtypus::Call.from_hash(log_entry)
13
+ end
14
+ return self.new(calls.sort_by{ |k| k.timestamp})
15
+ end
16
+
17
+ def initialize(calls)
18
+ @calls = calls
19
+ end
20
+
21
+ end
22
+ end
@@ -0,0 +1,44 @@
1
+ # example
2
+ # ruby ./bin/playtypus play --host=http://localhost:8081/ --call-log=/Users/justin/git/primary/Automation/DataRepository/exchange_service_api/smoke_tests/playback_service_api_primer/exchange_crud_api.json --log-responses --preserve-times
3
+
4
+ require 'thor'
5
+ require 'fileutils'
6
+
7
+ module Playtypus
8
+ class CLI < Thor
9
+ include Thor::Actions
10
+
11
+ desc "play", "plays the playtypus"
12
+ method_option :host, :type => :string, :aliases => '-h', :required => true
13
+ method_option :call_log, :type => :string, :aliases => '-c', :required => true
14
+ method_option :response_log, :type => :string, :aliases => '-r', :default => ''
15
+ method_option :preserve_times, :type => :boolean, :aliases => '-p', :default => false
16
+ def play(*args)
17
+ require_relative '../../ascii.rb'
18
+ $logger.debug "playtypus is playing with options #{options.inspect}"
19
+
20
+ sender = Playtypus::HttpSender.new(options[:host])
21
+ preserve_times = options[:preserve_times]
22
+ calls = []
23
+
24
+ if File.directory? options[:call_log]
25
+ $logger.info "--call-log is a directory. calls will be sent in order, without --preserve-times"
26
+ preserve_times = false
27
+ logs = Dir.glob("#{options[:call_log]}/**/*.json")
28
+ logs.each do |log|
29
+ calls.concat(Playtypus::CallContainer.from_log(File.read(log)).calls)
30
+ end
31
+ else
32
+ log_file = File.read(options[:call_log])
33
+ calls = Playtypus::CallContainer.from_log(log_file).calls
34
+ end
35
+ unless options[:response_log].to_s.empty?
36
+ FileUtils.mkdir_p options[:response_log]
37
+ end
38
+ gateway = Playtypus::Gateway.new(calls, sender, preserve_times, options[:response_log])
39
+ gateway.playback()
40
+ end
41
+
42
+ map 'p' => :play
43
+ end
44
+ end
@@ -0,0 +1,57 @@
1
+ require 'eventmachine'
2
+ require 'json'
3
+
4
+ module Playtypus
5
+ class Gateway
6
+
7
+ def initialize(call_list, sender, preserve_times, response_log)
8
+ @call_list = call_list.clone.freeze
9
+ @sender = sender
10
+ @preserve_times = preserve_times
11
+ @response_log = response_log
12
+ end
13
+
14
+ def playback()
15
+
16
+ if(@call_list.size == 0)
17
+ return
18
+ end
19
+
20
+ @diff = Time.now.utc - @call_list.first.timestamp
21
+ @ticks = 0
22
+ @position = 0
23
+ EventMachine.run do
24
+ Signal.trap("INT") { EventMachine.stop }
25
+ Signal.trap("TERM") { EventMachine.stop }
26
+ EM.add_periodic_timer(0.01) {
27
+ current = Time.now.utc - @diff
28
+ while(@position < @call_list.size)
29
+ call = @call_list[@position]
30
+ if(!(@preserve_times) || (call.timestamp <= current))
31
+ result = @sender.send(call)
32
+ $logger.info "#{self.class.name}: waited #{@ticks} ticks. sent message #{call.timestamp}"
33
+ unless @response_log.to_s.empty?
34
+ begin
35
+ File.open("#{@response_log}/#{@position.to_s.rjust(10, '0')}.log", 'w+') do |file|
36
+ file.write(JSON.pretty_generate({ 'code' => result.response.code, 'headers' => result.headers.to_hash, 'body' => result.parsed_response}))
37
+ end
38
+ rescue => e
39
+ $logger.warn "failed to log response with exception: #{e.inspect}"
40
+ end
41
+ end
42
+ @position += 1
43
+ @ticks = 0
44
+ else
45
+ @ticks += 1
46
+ break
47
+ end
48
+ end
49
+ if(@position >= @call_list.size)
50
+ EventMachine.stop
51
+ end
52
+ }
53
+ end
54
+ end
55
+ end
56
+
57
+ end
@@ -0,0 +1,75 @@
1
+ require 'json'
2
+ require 'httparty'
3
+ require 'uri'
4
+
5
+ module HTTParty
6
+ class Parser
7
+ def self.call(body, format)
8
+ begin
9
+ new(body, format).parse
10
+ rescue JSON::ParserError
11
+ format = :plain
12
+ retry
13
+ end
14
+ end
15
+ end
16
+ end
17
+
18
+ module Playtypus
19
+ class HttpSender
20
+ include HTTParty
21
+
22
+ class HttpSendError < Exception
23
+ end
24
+
25
+ def initialize(host)
26
+ @host = host
27
+ end
28
+
29
+ def send(call)
30
+ $logger.debug("sending call #{call.inspect}")
31
+ body = transform_payload(call.body)
32
+ $logger.debug "transformed body to #{body}"
33
+
34
+ begin
35
+ URI.parse("#{@host}#{call.path}")
36
+ @url = "#{@host}#{call.path}"
37
+ rescue URI::InvalidURIError
38
+ @url = URI::encode("#{@host}#{call.path}")
39
+ end
40
+
41
+ $logger.info("sending call to #{@url}")
42
+ response = nil
43
+
44
+ begin
45
+ response = send_http(call.verb.downcase, @url, { :body => body, :headers => call.headers })
46
+ rescue => e
47
+ $logger.warn "call failed with exception: #{e.inspect}"
48
+ end
49
+
50
+ unless(response == nil)
51
+ $logger.debug "received response #{response.inspect}"
52
+ end
53
+
54
+ return response
55
+ end
56
+
57
+ private
58
+
59
+ def send_http(verb, url, options)
60
+ self.class.send(verb, url, options)
61
+ end
62
+
63
+ def transform_payload(input)
64
+ result = nil
65
+ begin
66
+ result = JSON.pretty_generate(input)
67
+ rescue
68
+ result = input
69
+ end
70
+ return result
71
+ end
72
+
73
+ end
74
+
75
+ end
data/lib/playtypus.rb ADDED
@@ -0,0 +1,13 @@
1
+ require 'logger'
2
+
3
+ $logger = Logger.new(STDOUT) unless $logger
4
+
5
+ require 'playtypus/call'
6
+ require 'playtypus/call_container'
7
+ require 'playtypus/gateway'
8
+ require 'playtypus/http_sender'
9
+ require 'playtypus/cli'
10
+
11
+ module Playtypus
12
+
13
+ end
data/playtypus.gemspec ADDED
@@ -0,0 +1,28 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = 'playtypus'
5
+ s.version = '0.0.1'
6
+ s.date = '2015-04-09'
7
+ s.required_ruby_version = '>= 1.9.3'
8
+ s.licenses = ['MIT']
9
+
10
+ s.summary = 'A command line utility that plays HTTP calls'
11
+ s.description = 'The Playtypus is an eventmachine-based command line utility that plays HTTP calls'
12
+ s.author = 'Rackspace'
13
+ s.email = ['racksburg_automation@lists.rackspace.com']
14
+ s.homepage = 'https://github.com/rackspaceautomationco/playtypus'
15
+
16
+ s.files = Dir.glob("{bin,lib}/**/*") + %w(MIT-LICENSE README.md Gemfile playtypus.gemspec ascii.rb)
17
+ s.executables = %w(playtypus)
18
+
19
+ s.add_dependency 'thor', '~> 0.19.1', '>= 0.19.1'
20
+ s.add_dependency 'rake', '~> 10.1.0', '>= 10.1.0'
21
+ s.add_dependency 'minitest', '~> 5.2.0', '>= 5.2.0'
22
+ s.add_dependency 'simplecov', '~> 0.9.2', '>= 0.9.2'
23
+ s.add_dependency 'coveralls', '~> 0.7.11', '>= 0.7.11'
24
+ s.add_dependency 'pry', '~> 0.10.1', '>= 0.10.1'
25
+ s.add_dependency 'httparty', '~> 0.11.0', '>= 0.11.0'
26
+ s.add_dependency 'eventmachine', '~> 1.0.3', '>= 1.0.3'
27
+ s.add_dependency 'mocha', '~> 0.14.0', '>= 0.14.0'
28
+ end
metadata ADDED
@@ -0,0 +1,238 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: playtypus
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Rackspace
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-04-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: thor
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.19.1
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 0.19.1
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: 0.19.1
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 0.19.1
33
+ - !ruby/object:Gem::Dependency
34
+ name: rake
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: 10.1.0
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 10.1.0
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: 10.1.0
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 10.1.0
53
+ - !ruby/object:Gem::Dependency
54
+ name: minitest
55
+ requirement: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - "~>"
58
+ - !ruby/object:Gem::Version
59
+ version: 5.2.0
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: 5.2.0
63
+ type: :runtime
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: 5.2.0
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: 5.2.0
73
+ - !ruby/object:Gem::Dependency
74
+ name: simplecov
75
+ requirement: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - "~>"
78
+ - !ruby/object:Gem::Version
79
+ version: 0.9.2
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: 0.9.2
83
+ type: :runtime
84
+ prerelease: false
85
+ version_requirements: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 0.9.2
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: 0.9.2
93
+ - !ruby/object:Gem::Dependency
94
+ name: coveralls
95
+ requirement: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - "~>"
98
+ - !ruby/object:Gem::Version
99
+ version: 0.7.11
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: 0.7.11
103
+ type: :runtime
104
+ prerelease: false
105
+ version_requirements: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - "~>"
108
+ - !ruby/object:Gem::Version
109
+ version: 0.7.11
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: 0.7.11
113
+ - !ruby/object:Gem::Dependency
114
+ name: pry
115
+ requirement: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - "~>"
118
+ - !ruby/object:Gem::Version
119
+ version: 0.10.1
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: 0.10.1
123
+ type: :runtime
124
+ prerelease: false
125
+ version_requirements: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - "~>"
128
+ - !ruby/object:Gem::Version
129
+ version: 0.10.1
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: 0.10.1
133
+ - !ruby/object:Gem::Dependency
134
+ name: httparty
135
+ requirement: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - "~>"
138
+ - !ruby/object:Gem::Version
139
+ version: 0.11.0
140
+ - - ">="
141
+ - !ruby/object:Gem::Version
142
+ version: 0.11.0
143
+ type: :runtime
144
+ prerelease: false
145
+ version_requirements: !ruby/object:Gem::Requirement
146
+ requirements:
147
+ - - "~>"
148
+ - !ruby/object:Gem::Version
149
+ version: 0.11.0
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: 0.11.0
153
+ - !ruby/object:Gem::Dependency
154
+ name: eventmachine
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - "~>"
158
+ - !ruby/object:Gem::Version
159
+ version: 1.0.3
160
+ - - ">="
161
+ - !ruby/object:Gem::Version
162
+ version: 1.0.3
163
+ type: :runtime
164
+ prerelease: false
165
+ version_requirements: !ruby/object:Gem::Requirement
166
+ requirements:
167
+ - - "~>"
168
+ - !ruby/object:Gem::Version
169
+ version: 1.0.3
170
+ - - ">="
171
+ - !ruby/object:Gem::Version
172
+ version: 1.0.3
173
+ - !ruby/object:Gem::Dependency
174
+ name: mocha
175
+ requirement: !ruby/object:Gem::Requirement
176
+ requirements:
177
+ - - "~>"
178
+ - !ruby/object:Gem::Version
179
+ version: 0.14.0
180
+ - - ">="
181
+ - !ruby/object:Gem::Version
182
+ version: 0.14.0
183
+ type: :runtime
184
+ prerelease: false
185
+ version_requirements: !ruby/object:Gem::Requirement
186
+ requirements:
187
+ - - "~>"
188
+ - !ruby/object:Gem::Version
189
+ version: 0.14.0
190
+ - - ">="
191
+ - !ruby/object:Gem::Version
192
+ version: 0.14.0
193
+ description: The Playtypus is an eventmachine-based command line utility that plays
194
+ HTTP calls
195
+ email:
196
+ - racksburg_automation@lists.rackspace.com
197
+ executables:
198
+ - playtypus
199
+ extensions: []
200
+ extra_rdoc_files: []
201
+ files:
202
+ - Gemfile
203
+ - MIT-LICENSE
204
+ - README.md
205
+ - ascii.rb
206
+ - bin/playtypus
207
+ - lib/playtypus.rb
208
+ - lib/playtypus/call.rb
209
+ - lib/playtypus/call_container.rb
210
+ - lib/playtypus/cli.rb
211
+ - lib/playtypus/gateway.rb
212
+ - lib/playtypus/http_sender.rb
213
+ - playtypus.gemspec
214
+ homepage: https://github.com/rackspaceautomationco/playtypus
215
+ licenses:
216
+ - MIT
217
+ metadata: {}
218
+ post_install_message:
219
+ rdoc_options: []
220
+ require_paths:
221
+ - lib
222
+ required_ruby_version: !ruby/object:Gem::Requirement
223
+ requirements:
224
+ - - ">="
225
+ - !ruby/object:Gem::Version
226
+ version: 1.9.3
227
+ required_rubygems_version: !ruby/object:Gem::Requirement
228
+ requirements:
229
+ - - ">="
230
+ - !ruby/object:Gem::Version
231
+ version: '0'
232
+ requirements: []
233
+ rubyforge_project:
234
+ rubygems_version: 2.2.2
235
+ signing_key:
236
+ specification_version: 4
237
+ summary: A command line utility that plays HTTP calls
238
+ test_files: []