yarh 0.1.3 → 0.1.4

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: be06fa99910b08969531006fb8437dd82770a62fdd4d7d428d9d10599c33786e
4
- data.tar.gz: 9ced27dcff33621e24ac55aa181af3cadfbbcd32255c3184ac2e73bf44dbecfa
3
+ metadata.gz: 230590cd46a0d3c0a79928ad65f90a980ff736e25ab71be810f0e525de707263
4
+ data.tar.gz: 186debbf786606aec63b311290a2c605102a178a275d406182901aabfabdc1a2
5
5
  SHA512:
6
- metadata.gz: c6e28d50e58caadd9fe64e15f20fa4d001648af8c8bfebd200f0361c163032774171997060af51a8c6b3f0970c0450dfdc1e4d08d6e8acd51eb70f4a9748fc11
7
- data.tar.gz: 78f9bf12359dde945a9dcb969224a5321020a43cddaa07bdae549d3cb870d77cbc6a08212f4028c31bbc20302dff49a1cae9980db7a9dcaa56d1e1057bcc54ae
6
+ metadata.gz: a7760c56390192b0076fc1edc1ca62d66b21f5b75e304d9d3dbd25a6e82d9d0a5bd8a9c7dff01c25bf1fc8e79d956c2437281893137336655109a3b3f583b3a9
7
+ data.tar.gz: 7f71b503a8958a6cba8b0b1adfc11cd581c3f3b03bcd7d3e9a4cdb3eb38219b31840bb74778cc67181579603058b0ccf7af75f8d10f3826148215142df47d259
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Yarh
4
+ module Errors
5
+ class BaseError < StandardError
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'base_error'
4
+
5
+ module Yarh
6
+ module Errors
7
+ # wrong data error
8
+ class WrongDataError < BaseError
9
+ def message
10
+ 'Incorrect request data sent'
11
+ end
12
+ end
13
+ end
14
+ end
@@ -2,36 +2,85 @@
2
2
 
3
3
  require 'faraday'
4
4
  require 'json'
5
+ require_relative 'errors/wrong_data_error'
5
6
 
6
7
  module Yarh
7
- # RequestBuilder
8
+ # Сlass builds a request from hash parameters and executes the request
8
9
  class RequestBuilder
9
10
  attr_reader :response
10
11
 
11
12
  def initialize(data)
12
- @data = data
13
+ @data = stringify_keys(data)
13
14
  @response = nil
14
15
  end
15
16
 
16
17
  def request
17
- conn = Faraday.new(url: data['url'])
18
+ raise Errors::WrongDataError if method.nil? || url.nil?
18
19
 
19
- response = conn.send(data['method'].downcase.to_sym) do |req|
20
- build_headers(req, data['headers'])
21
- req.body = data['body'].to_json
22
- end
20
+ conn = Faraday.new(url: url)
23
21
 
24
- @response = response
22
+ @response = send_request(conn)
25
23
  end
26
24
 
27
25
  private
28
26
 
29
27
  attr_reader :data
30
28
 
29
+ def send_request(faraday_connection)
30
+ faraday_connection.send(method) do |req|
31
+ build_headers(req, data['headers'])
32
+ req.body = body if body
33
+ req.params = params if params
34
+ req.options.timeout timeout if timeout
35
+ end
36
+ end
37
+
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
47
+
48
+ def params
49
+ data['params']
50
+ end
51
+
52
+ def timeout
53
+ data['timeout']
54
+ end
55
+
56
+ # TODO: optimize this method in the future
57
+ def body
58
+ return unless data['body']
59
+
60
+ if application_json?
61
+ data['body'].to_json
62
+ else
63
+ data['body']
64
+ end
65
+ end
66
+
67
+ def application_json?
68
+ return false unless data['headers']
69
+ return false unless data['headers']['Content-Type']
70
+
71
+ data['headers']['Content-Type'] == 'application/json'
72
+ end
73
+
31
74
  def build_headers(req, headers)
75
+ return unless headers
76
+
32
77
  headers.each do |header, value|
33
78
  req.headers[header] = value
34
79
  end
35
80
  end
81
+
82
+ def stringify_keys(data)
83
+ data.is_a?(Hash) ? data.to_h { |k, v| [k.to_s, stringify_keys(v)] } : data
84
+ end
36
85
  end
37
86
  end
@@ -10,17 +10,23 @@ module Yarh
10
10
 
11
11
  def initialize(path)
12
12
  @path = path
13
+ @responses = {}
13
14
  end
14
15
 
15
16
  def run
16
- parsed_yaml.each_value do |data|
17
+ parsed_yaml.each do |request_name, data|
17
18
  request = RequestBuilder.new(data)
18
19
  request.request
20
+ responses[request_name] = request.response
19
21
  end
22
+
23
+ responses
20
24
  end
21
25
 
22
26
  private
23
27
 
28
+ attr_reader :responses
29
+
24
30
  def parsed_yaml
25
31
  @parsed_yaml ||= YamlParser.parse(path)
26
32
  end
data/lib/yarh/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Yarh
4
- VERSION = '0.1.3'
4
+ VERSION = '0.1.4'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yarh
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kirill Leonov
@@ -48,6 +48,8 @@ files:
48
48
  - exe/yarh
49
49
  - lib/yarh.rb
50
50
  - lib/yarh/cli.rb
51
+ - lib/yarh/errors/base_error.rb
52
+ - lib/yarh/errors/wrong_data_error.rb
51
53
  - lib/yarh/request_builder.rb
52
54
  - lib/yarh/request_runner.rb
53
55
  - lib/yarh/version.rb