playback 1.1.0 → 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 580e0a3e9b1cafc764fe1f62eb6b405078c360c0
4
- data.tar.gz: 0420f4a3e89b6b7665b6a5337e332b38989258f6
3
+ metadata.gz: 047d1f2495190a640bc69a27cac7ccd8c947b410
4
+ data.tar.gz: 78ce24836ece75f735de0480260204ee7740574b
5
5
  SHA512:
6
- metadata.gz: 12540808291a231bd988178f6bb463b3605056effd7a98ce896d2e90fb4437856e1f33a84bc32b9a9c108874c3ed9bbc7ccab1151445054ae1bdb099e02035a4
7
- data.tar.gz: 84cb9e852812fa93dedf78bdfe039dfcb6b6f08d0d8e0dd3b3ae282aa5067db4c4621b2dd6721d23f477a04092db3683bd5ca8eb72c8d9a0196ccc4978a3fddc
6
+ metadata.gz: ee385541f2c0c9256f87043c08ac1da99835716d94054191be0d610d698306404492d3a8b208a14d91aeb2eb6ce58545808dba6c9b09274db5684c157f87d97e
7
+ data.tar.gz: 62b0f255d0cc4933564fcacec00ee53ecb8472fac52450d97852361dd600d04dab010c21650d2ce862192b3155584342192d784bdac1468b19bb732e9d8c8db0
data/README.md CHANGED
@@ -1,6 +1,5 @@
1
- # Playback
2
- [![Build Status](https://travis-ci.org/takady/playback.svg?branch=master)](https://travis-ci.org/takady/playback) [![Code Climate](https://codeclimate.com/github/takady/playback/badges/gpa.svg)](https://codeclimate.com/github/takady/playback)
3
- Execute http request from apache access log
1
+ # Playback [![Build Status](https://travis-ci.org/takady/playback.svg?branch=master)](https://travis-ci.org/takady/playback) [![Code Climate](https://codeclimate.com/github/takady/playback/badges/gpa.svg)](https://codeclimate.com/github/takady/playback)
2
+ Execute http request from apache access log
4
3
 
5
4
  ## Installation
6
5
 
@@ -10,7 +9,7 @@ Execute http request from apache access log
10
9
 
11
10
  ### as a command line tool
12
11
  ```sh
13
- playback 'http://httpbin.org' /path/to/access.log
12
+ $ playback 'http://httpbin.org' /path/to/access.log
14
13
  #=> { "method": "GET", "path": "/get", "status": 200 }
15
14
  #=> { "method": "POST", "path": "/post?hoge=1", "status": 404 }
16
15
  #=> { "method": "PUT", "path": "/put?foo=bar", "status": 200 }
@@ -1,6 +1,2 @@
1
1
  require "playback/version"
2
2
  require "playback/request"
3
-
4
- module Playback
5
- # Your code goes here...
6
- end
@@ -7,74 +7,53 @@ module Playback
7
7
  DEFAULT_CONTENT_TYPE = 'application/text'
8
8
  DEFAULT_USER_AGENT = 'From Playback rubygems'
9
9
 
10
+ PARSER = ApacheLog::Parser
11
+
12
+ REQUEST_WITHOUT_BODY = %w(GET DELETE HEAD)
13
+ REQUEST_WITH_BODY = %w(POST PUT PATCH)
14
+
10
15
  def initialize(base_uri)
11
16
  @base_uri = base_uri
12
- @common_parser = ApacheLog::Parser.new('common')
13
- @combined_parser = ApacheLog::Parser.new('combined')
17
+ @common_parser = PARSER.new('common')
18
+ @combined_parser = PARSER.new('combined')
14
19
  end
15
20
 
16
21
  def run(line, return_type='')
17
- parsed_line = parse(line)
18
- method = parsed_line[:request][:method]
19
- path = parsed_line[:request][:path]
20
- referer = parsed_line[:referer] ||= ''
21
- user_agent = parsed_line[:user_agent] ||= DEFAULT_USER_AGENT
22
+ parsed_line = parse_log(line)
22
23
 
23
- res = request(method, path, referer, user_agent)
24
+ method = parsed_line[:request][:method]
25
+ path = parsed_line[:request][:path]
26
+ referer = parsed_line[:referer] ||= ''
27
+ user_agent = parsed_line[:user_agent] ||= DEFAULT_USER_AGENT
24
28
 
25
- unless (return_type == 'net-http')
26
- result = {
27
- method: method,
28
- path: path,
29
- status: res.code.to_i,
30
- }
31
- res = JSON.generate(result)
32
- end
29
+ response = request(method, path, referer, user_agent)
33
30
 
34
- res
31
+ return response if return_type == 'net-http'
35
32
 
33
+ {method: method, path: path, status: response.code.to_i}.to_json
36
34
  rescue => e
37
35
  e.message
38
36
  end
39
37
 
40
38
  private
41
39
 
42
- def parse(line)
40
+ def parse_log(line)
43
41
  begin
44
42
  @combined_parser.parse(line.chomp)
45
43
  rescue
46
- begin
47
- @common_parser.parse(line.chomp)
48
- rescue => e
49
- raise e
50
- end
44
+ @common_parser.parse(line.chomp)
51
45
  end
52
46
  end
53
47
 
54
48
  def request(method, path, referer, user_agent)
55
- begin
56
- uri = URI.parse(@base_uri + path)
57
- rescue
58
- raise "it can not be recognized as a uri: <#{@base_uri + path}>"
59
- end
60
-
49
+ uri = URI.parse("#{@base_uri}#{path}")
61
50
  http = Net::HTTP.new(uri.host, uri.port)
62
- query = uri.query ||= ''
63
- data = {'Content-Type' => DEFAULT_CONTENT_TYPE, 'Referer' => referer, 'User-Agent' => user_agent}
51
+ header = {'Content-Type' => DEFAULT_CONTENT_TYPE, 'Referer' => referer, 'User-Agent' => user_agent}
64
52
 
65
- case method
66
- when 'GET'
67
- http.get(path, data)
68
- when 'POST'
69
- http.post(uri.path, query, data)
70
- when 'PUT'
71
- http.put(uri.path, query, data)
72
- when 'DELETE'
73
- http.delete(path, data)
74
- when 'PATCH'
75
- http.patch(uri.path, query, data)
76
- when 'HEAD'
77
- http.head(path, data)
53
+ if REQUEST_WITHOUT_BODY.include?(method)
54
+ http.send(method.downcase, path, header)
55
+ elsif REQUEST_WITH_BODY.include?(method)
56
+ http.send(method.downcase, uri.path, uri.query, header)
78
57
  else
79
58
  raise "it is not supported http method: <#{method}>"
80
59
  end
@@ -1,3 +1,3 @@
1
1
  module Playback
2
- VERSION = "1.1.0"
2
+ VERSION = "1.1.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: playback
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yuichi Takada
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-26 00:00:00.000000000 Z
11
+ date: 2015-06-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -108,7 +108,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
108
108
  version: '0'
109
109
  requirements: []
110
110
  rubyforge_project:
111
- rubygems_version: 2.2.2
111
+ rubygems_version: 2.4.5
112
112
  signing_key:
113
113
  specification_version: 4
114
114
  summary: Execute http request from apache access log