postman-ruby 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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/postman/postman.rb +199 -0
  3. metadata +45 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6ef18a58f3100c88c9cd6cf0c100b6b05a54dfbd
4
+ data.tar.gz: 2632ced5fe06dae2a6c7c49af87b343c25c7b84d
5
+ SHA512:
6
+ metadata.gz: 111a3cba3f75d149b2718688fc0ea6f7394dd52e7627bb1722748173399a1f02415373121da6b965304d656eebb7c71bb89644a4ce8fe118a35330045877215d
7
+ data.tar.gz: bf47979eea454a437b4c20aef0d41d3086091a5ff36bd48fc04bdb569b7e271aa748286e42f2e02e3b36cf2ba6b09d0a960c6886acb38c745dcc22afbd3942c3
@@ -0,0 +1,199 @@
1
+ require 'json'
2
+ require 'net/http'
3
+
4
+ module Postman
5
+ class URL
6
+ def initialize(url)
7
+ # @raw
8
+ # @auth
9
+ # @host
10
+ # @path
11
+ # @variable
12
+ if url.class == String
13
+ url = {
14
+ 'raw' => url,
15
+ 'auth' => {},
16
+ 'host' => [],
17
+ 'path' => [],
18
+ 'variable' => {},
19
+ }
20
+ end
21
+ @raw = url
22
+ url.each do |k,v|
23
+ instance_variable_set("@#{k}", v)
24
+ self.class.send(:attr_reader, k.to_sym)
25
+ end
26
+
27
+ if !@variable.nil? && !@variable.empty?
28
+ var = {}
29
+ @variable.each do |v|
30
+ var[v['id']] = v['value']
31
+ end
32
+ @variable = var
33
+ end
34
+ end
35
+
36
+ def to_s
37
+ interpolate
38
+ end
39
+
40
+ private
41
+
42
+ def interpolate
43
+ url = @raw
44
+ vars = @raw.scan(/:[A-Za-z\-_]+\//)
45
+ vars.each do |v|
46
+ url = url.gsub(v, @variable[v[1..-1]])
47
+ end
48
+ url
49
+ end
50
+ end
51
+
52
+ class Request
53
+ def initialize(params)
54
+ #@name
55
+ #@description
56
+ #@method
57
+ #@url
58
+ #@header
59
+ #@body
60
+ #@environment
61
+ params.each do |k,v|
62
+ instance_variable_set("@#{k}", v)
63
+ self.class.send(:attr_reader, k.to_sym)
64
+ end
65
+ @url = URL.new(@url)
66
+ @env = @environment
67
+ end
68
+
69
+ def set_env(env)
70
+ env.each { |k, v| @env[k] = v }
71
+ end
72
+
73
+ def reset_env(env)
74
+ @env = env.clone
75
+ end
76
+
77
+ def call
78
+ send @method.downcase.to_sym
79
+ end
80
+
81
+ private
82
+
83
+ def apply_env(str)
84
+ vars = str.scan(/\{{2}[A-Za-z_\-.]+\}{2}/)
85
+ vars.each do |v|
86
+ val = @env[v[2..-3]]
87
+ raise "Request env var is missing: #{v}" if val.nil?
88
+ str = str.gsub(v, val)
89
+ end
90
+ str
91
+ end
92
+
93
+ def get
94
+ uri = URI(apply_env(@url.to_s))
95
+ req = Net::HTTP::Get.new(uri)
96
+ if @header.class == Array
97
+ @header.each do |h|
98
+ req[h['key']] = apply_env(h['value'])
99
+ end
100
+ end
101
+ Net::HTTP.start(uri.host, uri.port) do |http|
102
+ return http.request req # Net::HTTPResponse object
103
+ end
104
+ end
105
+ end
106
+
107
+ FILTER_KEYS=%w(name url method description)
108
+
109
+ class Collection
110
+ def initialize(hash={})
111
+ @parsed = hash
112
+ @parsed['item'] = [] if @parsed['item'].nil?
113
+ @env = {}
114
+ end
115
+
116
+ def set_env(env)
117
+ env.each do |k, v|
118
+ @env[k] = v
119
+ end
120
+ end
121
+
122
+ def reset_env(env={})
123
+ @env = env.clone
124
+ end
125
+
126
+ def filter(flt_hash={}, &block)
127
+ flt_hash.each do |k, v|
128
+ if v.nil?
129
+ raise ArgumentError.new("filter value must be string")
130
+ end
131
+ if !FILTER_KEYS.include?(k.to_s)
132
+ raise ArgumentError.new("invalid filter key \"#{k}\", allowed keys: [#{FILTER_KEYS.join(', ')}]")
133
+ end
134
+ end
135
+ filter_collection(@parsed, flt_hash, &block)
136
+ end
137
+
138
+ def to_a
139
+ filter({})
140
+ end
141
+
142
+ private
143
+
144
+ def make_child_request(params)
145
+ r = Request.new(params)
146
+ r.reset_env(@env)
147
+ r
148
+ end
149
+
150
+ def filter_collection(collection, flt_hash={}, &block)
151
+ ret = []
152
+ collection['item'].each do |item|
153
+ if is_collection(item)
154
+ ret += filter_collection(item, flt_hash, &block)
155
+ elsif is_request(item)
156
+ req = item['request']
157
+ req['name'] = item['name']
158
+ req['response'] = item['response']
159
+ if block_given? || flt_hash.empty?
160
+ r = make_child_request(req)
161
+ next if block_given? && ! yield(r)
162
+ ret << r
163
+ next
164
+ end
165
+ score = 0
166
+ flt_hash.each do |k, v|
167
+ itk = req[k.to_s]
168
+ if !itk.nil?
169
+ score+=1 if v.class == Regexp && v =~ itk || v.class == String && v.downcase == itk.downcase
170
+ end
171
+ end
172
+ if score == flt_hash.size
173
+ r = make_child_request(req)
174
+ ret << r
175
+ end
176
+ end
177
+ end
178
+ ret
179
+ end
180
+
181
+ def is_collection(item)
182
+ item['item'].class == Array
183
+ end
184
+
185
+ def is_request(item)
186
+ !item['request'].nil?
187
+ end
188
+ end
189
+
190
+
191
+ def self.parse(json)
192
+ Collection.new(JSON.parse(json))
193
+ end
194
+
195
+ def self.parse_file(name)
196
+ Postman.parse(File.read(name))
197
+ end
198
+ end
199
+
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: postman-ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Joseph Buchma
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-04-18 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Allows to parse Postman's JSON reqeuests collection dump and make requests
14
+ from ruby
15
+ email: josephbuchma@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/postman/postman.rb
21
+ homepage: http://github.com/josephbuchma/postman-ruby
22
+ licenses:
23
+ - MIT
24
+ metadata: {}
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 2.4.5
42
+ signing_key:
43
+ specification_version: 4
44
+ summary: Postman requests collection parser
45
+ test_files: []