rspec-sane-http 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +0 -0
  3. data/lib/rspec-sane-http.rb +117 -0
  4. metadata +59 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ae8accf71844940fdfe6faebded745c3ed18389f
4
+ data.tar.gz: c40245728bc329f091b9ab1bd87449036470a6aa
5
+ SHA512:
6
+ metadata.gz: 6da7e7db222cbc51d36af6e474cac4ad8219c0afc12a9648238f48cb2a028d83684cb967bda116c2dc65fe72787d35643b79a9afe173ff863b8d0c64c65e3f5b
7
+ data.tar.gz: 15d755219957b76dd196f22e30a752d4105684b3d3760d8c64701f4e7c8a220a0c5d80a802546051d2d790714f9fc7f8c9a5a58318aa5fd26590e7176e425473
data/README.md ADDED
File without changes
@@ -0,0 +1,117 @@
1
+ require 'http'
2
+
3
+ module HttpApi
4
+ module Extensions
5
+ def self.extended(base)
6
+ base.class_eval do
7
+ def description
8
+ block = Proc.new do |metadata|
9
+ if metadata[:description].match(/^(GET|POST|PUT|DELETE|OPTIONS|PATCH) (.+)$/)
10
+ metadata[:description]
11
+ else
12
+ block.call(metadata[:parent_example_group])
13
+ end
14
+ end
15
+
16
+ block.call(self.class.metadata)
17
+ end
18
+
19
+ def request_method
20
+ self.description.split(' ').first
21
+ end
22
+
23
+ def request_path
24
+ self.description.split(' ').last.split('/').
25
+ map { |fragment| fragment.sub(/^:(.+)$/) { |match|
26
+ # instance.send(match[1..-1])
27
+ self.class.metadata[match[1..-1].to_sym]
28
+ } }.join('/')
29
+ end
30
+
31
+ let(:attrs) do
32
+ Hash.new
33
+ end
34
+
35
+ # let(:factory) do
36
+ # self.model.factory(attrs)
37
+ # end
38
+
39
+ let(:instance) do
40
+ # TODO: maybe inheritance?
41
+ if request_method == 'POST'
42
+ factory
43
+ else
44
+ factory.save
45
+ puts "~ DB: #{factory.values}"
46
+ factory
47
+ end
48
+ end
49
+
50
+ let(:url) do
51
+ [RSpec.configuration.base_url, request_path].join('')
52
+ end
53
+
54
+ # Rest client docs: https://github.com/rest-client/rest-client
55
+ let(:response) do
56
+ if ['GET', 'DELETE'].include?(request_method)
57
+ headers = self.class.metadata[:headers]
58
+ request = HTTP.with_headers(headers || {})
59
+
60
+ log(request_method, request_path, headers)
61
+ request.send(request_method.downcase, url)
62
+ else
63
+ data = self.class.metadata[:data]
64
+ headers = self.class.metadata[:headers]
65
+ request = HTTP.with_headers(headers || {})
66
+
67
+ log(request_method, request_path, headers, data)
68
+ request.send(request_method.downcase, url, body: data)
69
+ end
70
+ end
71
+
72
+ def request(request_method, request_path = request_path, headers = {}, data = {})
73
+ url = [RSpec.configuration.base_url, request_path].join('')
74
+ request = HTTP.with_headers(headers)
75
+ data = data.to_json unless data.is_a?(String) # TODO: Switch to using data as an argument rather than stringified JSON.
76
+ response = request.send(request_method.downcase, url, body: data)
77
+ log(request_method, request_path, headers, data)
78
+
79
+ JSON.parse(response.body.readpartial)
80
+ end
81
+
82
+ # data = POST('/posts', {Authorization: '...'}, {'title': ...})
83
+ ['POST', 'PUT'].each do |http_method|
84
+ define_method(http_method) do |request_path = request_path, headers = {}, body|
85
+ request(http_method, request_path, headers, body)
86
+ end
87
+ end
88
+
89
+ ['GET', 'DELETE'].each do |http_method|
90
+ define_method(http_method) do |request_path = request_path, headers = {}|
91
+ request(http_method, request_path, headers, nil)
92
+ end
93
+ end
94
+
95
+ def log(request_method, request_path, headers, data = nil)
96
+ if $DEBUG
97
+ string = "~ #{request_method} #{request_path}"
98
+ string << " #{headers.inspect}" if headers && ! headers.empty?
99
+ string << " data: #{data.inspect}" if data
100
+ warn string
101
+ end
102
+ end
103
+
104
+ let(:response_data) do
105
+ data = JSON.parse(response).reduce(Hash.new) do |buffer, (key, value)|
106
+ buffer.merge(key.to_sym => value)
107
+ end
108
+
109
+ puts "Code: #{response.code}"
110
+ puts "Data: #{data.inspect}"
111
+
112
+ data
113
+ end
114
+ end
115
+ end
116
+ end
117
+ end
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rspec-sane-http
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - https://github.com/botanicus
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-09-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: http
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: XXX
28
+ email: james@101ideas.cz
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - README.md
34
+ - lib/rspec-sane-http.rb
35
+ homepage: https://github.com/botanicus/rspec-sane-http
36
+ licenses:
37
+ - MIT
38
+ metadata: {}
39
+ post_install_message:
40
+ rdoc_options: []
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ requirements: []
54
+ rubyforge_project: rspec-sane-http
55
+ rubygems_version: 2.6.13
56
+ signing_key:
57
+ specification_version: 4
58
+ summary: YYY
59
+ test_files: []