Skip to content

Commit 5df0f23

Browse files
committed
files
1 parent 1f586fe commit 5df0f23

12 files changed

+175
-29
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.ruby-version
22
test/examples/private_examples
3+
.byebug_history

.rubocop.yml

+3
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,6 @@ Gemspec/RequireMFA:
1010

1111
Gemspec/RequiredRubyVersion:
1212
Enabled: false
13+
14+
Style/AndOr:
15+
Enabled: false

Gemfile

+1
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@ end
1414
group :test do
1515
gem 'minitest', '~> 5.22', '>= 5.22.3'
1616
gem 'minitest-reporters', '~> 1.6', '>= 1.6.1'
17+
gem 'mocha', '~> 2.2'
1718
gem 'webmock', '~> 3.23'
1819
end

Gemfile.lock

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
PATH
22
remote: .
33
specs:
4-
yarh (0.1.4)
4+
yarh (0.1.5)
55
faraday (~> 2.9)
6+
faraday-multipart (~> 1.0, >= 1.0.4)
67
thor (~> 1.3)
78

89
GEM
@@ -20,6 +21,8 @@ GEM
2021
rexml
2122
faraday (2.9.0)
2223
faraday-net_http (>= 2.0, < 3.2)
24+
faraday-multipart (1.0.4)
25+
multipart-post (~> 2)
2326
faraday-net_http (3.1.0)
2427
net-http
2528
hashdiff (1.1.0)
@@ -31,6 +34,9 @@ GEM
3134
builder
3235
minitest (>= 5.0)
3336
ruby-progressbar
37+
mocha (2.2.0)
38+
ruby2_keywords (>= 0.0.5)
39+
multipart-post (2.4.0)
3440
net-http (0.4.1)
3541
uri
3642
parallel (1.24.0)
@@ -60,6 +66,7 @@ GEM
6066
rubocop (>= 1.61, < 2.0)
6167
rubocop-ast (>= 1.31.1, < 2.0)
6268
ruby-progressbar (1.13.0)
69+
ruby2_keywords (0.0.5)
6370
thor (1.3.1)
6471
unicode-display_width (2.5.0)
6572
uri (0.13.0)
@@ -76,6 +83,7 @@ DEPENDENCIES
7683
byebug (~> 11.1, >= 11.1.3)
7784
minitest (~> 5.22, >= 5.22.3)
7885
minitest-reporters (~> 1.6, >= 1.6.1)
86+
mocha (~> 2.2)
7987
rake (~> 13.2, >= 13.2.1)
8088
rubocop (~> 1.63, >= 1.63.4)
8189
rubocop-minitest (~> 0.35.0)

lib/yarh/cli.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class CLI < Thor
1313

1414
desc '-r PATH', 'execute requests from file'
1515
def run_requests(path)
16-
Yarh.run_requests(path)
16+
puts Yarh.run_requests(path)
1717
end
1818

1919
desc 'version', 'show Yarh version'

lib/yarh/request_body.rb

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# frozen_string_literal: true
2+
3+
require 'faraday'
4+
require 'faraday/multipart'
5+
6+
module Yarh
7+
# Class for creating request bodies
8+
class RequestBody
9+
def initialize(body)
10+
@body_data = body
11+
@result_body = {}
12+
end
13+
14+
def create
15+
body_data.each do |key, value|
16+
if value.is_a?(Hash)
17+
prepare_value(key, value)
18+
else
19+
@result_body[key] = value
20+
end
21+
end
22+
23+
@result_body
24+
end
25+
26+
private
27+
28+
attr_reader :body_data
29+
30+
# NOTE: Currently the method is used for parsing files
31+
def prepare_value(key, value)
32+
if value['path'] and value['file_type']
33+
file = Faraday::UploadIO.new(value['path'], value['file_type'])
34+
@result_body[key] = file
35+
else
36+
@result_body[key] = value
37+
end
38+
end
39+
end
40+
end

lib/yarh/request_builder.rb

+50-26
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
# frozen_string_literal: true
22

33
require 'faraday'
4+
require 'faraday/multipart'
45
require 'json'
56
require_relative 'errors/wrong_data_error'
7+
require_relative 'request_body'
68

79
module Yarh
810
# Сlass builds a request from hash parameters and executes the request
@@ -15,17 +17,29 @@ def initialize(data)
1517
end
1618

1719
def request
18-
raise Errors::WrongDataError if method.nil? || url.nil?
20+
validate_before_request
1921

20-
conn = Faraday.new(url: url)
21-
22-
@response = send_request(conn)
22+
@response = send_request(build_conncetion)
2323
end
2424

2525
private
2626

2727
attr_reader :data
2828

29+
def validate_before_request
30+
raise Errors::WrongDataError if method.nil? or url.nil?
31+
end
32+
33+
def build_conncetion
34+
if multipart?
35+
Faraday.new(url: url) do |request|
36+
request.request :multipart
37+
end
38+
else
39+
Faraday.new(url: url)
40+
end
41+
end
42+
2943
def send_request(faraday_connection)
3044
faraday_connection.send(method) do |req|
3145
build_headers(req, data['headers'])
@@ -35,33 +49,25 @@ def send_request(faraday_connection)
3549
end
3650
end
3751

38-
def method
39-
return unless data['method']
40-
41-
data['method'].downcase.to_sym
42-
end
43-
44-
def url
45-
data['url']
46-
end
52+
# TODO: optimize this method in the future
53+
def body
54+
return @body unless @body.nil?
55+
return unless data['body']
4756

48-
def params
49-
data['params']
50-
end
57+
body = RequestBody.new(data['body']).create
5158

52-
def timeout
53-
data['timeout']
59+
@body = if application_json?
60+
body.to_json
61+
else
62+
body
63+
end
5464
end
5565

56-
# TODO: optimize this method in the future
57-
def body
58-
return unless data['body']
66+
def multipart?
67+
return false unless data['headers']
68+
return false unless data['headers']['Content-Type']
5969

60-
if application_json?
61-
data['body'].to_json
62-
else
63-
data['body']
64-
end
70+
data['headers']['Content-Type'] == 'multipart/form-data'
6571
end
6672

6773
def application_json?
@@ -82,5 +88,23 @@ def build_headers(req, headers)
8288
def stringify_keys(data)
8389
data.is_a?(Hash) ? data.to_h { |k, v| [k.to_s, stringify_keys(v)] } : data
8490
end
91+
92+
def method
93+
return unless data['method']
94+
95+
data['method'].downcase.to_sym
96+
end
97+
98+
def url
99+
data['url']
100+
end
101+
102+
def params
103+
data['params']
104+
end
105+
106+
def timeout
107+
data['timeout']
108+
end
85109
end
86110
end

lib/yarh/version.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
module Yarh
4-
VERSION = '0.1.4'
4+
VERSION = '0.1.5'
55
end

test.txt

Whitespace-only changes.

test/lib/yarh/request_body_test.rb

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../../test_helper'
4+
5+
module Yarh
6+
class RequestBodyTest < Minitest::Test
7+
def test_when_simple_body
8+
body = {
9+
key1: 'value1',
10+
key2: 'value2'
11+
}
12+
13+
result = RequestBody.new(body).create
14+
15+
assert_equal body, result
16+
end
17+
18+
def test_when_body_include_hash # rubocop:disable Metrics/MethodLength
19+
body = {
20+
key1: 'value1',
21+
key2: 'value2',
22+
key3: {
23+
'path' => 'path',
24+
'file_type' => 'type'
25+
}
26+
}
27+
28+
expected_body = {
29+
key1: 'value1',
30+
key2: 'value2',
31+
key3: 123
32+
}
33+
Faraday::UploadIO.expects(:new).with('path', 'type').returns(123)
34+
result = RequestBody.new(body).create
35+
36+
assert_equal expected_body, result
37+
end
38+
39+
def test_when_no_path
40+
body = {
41+
key1: 'value1',
42+
key2: 'value2',
43+
key3: {
44+
'file_type' => 'type'
45+
}
46+
}
47+
48+
result = RequestBody.new(body).create
49+
50+
assert_equal body, result
51+
end
52+
53+
def test_when_no_file_type
54+
body = {
55+
key1: 'value1',
56+
key2: 'value2',
57+
key3: {
58+
'path' => 'path'
59+
}
60+
}
61+
62+
result = RequestBody.new(body).create
63+
64+
assert_equal body, result
65+
end
66+
end
67+
end

test/test_helper.rb

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
require 'minitest/reporters'
66
require 'webmock/minitest'
77
require 'json'
8+
require 'mocha/minitest'
89
require_relative '../lib/yarh'
910
require_relative '../lib/yarh/version'
1011

yarh.gemspec

+1
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@ Gem::Specification.new do |s|
2020
s.metadata['source_code_uri'] = s.homepage
2121
s.metadata['documentation_uri'] = s.homepage
2222
s.add_dependency 'faraday', '~> 2.9'
23+
s.add_dependency 'faraday-multipart', '~> 1.0', '>= 1.0.4'
2324
s.add_dependency 'thor', '~> 1.3'
2425
end

0 commit comments

Comments
 (0)