request-builder 0.1.0 → 0.1.2

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
  SHA256:
3
- metadata.gz: 457e80f8bbd15d0c31db2b9c22eb0cdad23d220263b046333e7f23233fd39f7c
4
- data.tar.gz: b2631a86209e6e280069118793f8610360675ace8cc84841a091389220cb9c78
3
+ metadata.gz: d96e607c780a2bf0f4b9a76576137b973ba71daa719b17838836a9890fece4c4
4
+ data.tar.gz: 696d365b78f1a133f40d5bc784e96a1e2d86a1a49f8f42eb7ba4f23379bd9473
5
5
  SHA512:
6
- metadata.gz: fccbcdc8f74870d091a5d660ea44b76b400037570db2bfa1af2516fffa14a4f1ec49625785e65062da585eb36fc8d75bb62b6395f705da6a9fd68c5d06ac93d0
7
- data.tar.gz: 8040052bf21eaa5c8693abccc82959d3739684cda46145ab2921d5fc09ed734a69ff57816057126163070d403bff2a9acb9907cf6b918a6ccc0941609225a4f5
6
+ metadata.gz: bf03752615829aa961327be7a13a8c56662e3796ec0fe4b565495c873ffa8f2eb31229d740475930ef3bbe69e9f345c695c3fa37f31236bef9cfbc4d5cd62856
7
+ data.tar.gz: 074b41b7a673cb4687f6c6a3331162d33159b5a30cde121b4921f41c7433eb6a9f2bea945188a2830df5281eb7111bc47e19e82aee3a822fe30224538a688198
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
1
  # Request::Builder
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/request/builder`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
3
+ Simple DSL wrapper for faraday gem
6
4
 
7
5
  ## Installation
8
6
 
@@ -22,7 +20,75 @@ Or install it yourself as:
22
20
 
23
21
  ## Usage
24
22
 
25
- TODO: Write usage instructions here
23
+ Include `Request::Builder` to your client class and describe your request using DSL
24
+
25
+ ```ruby
26
+ class MyApiClient
27
+ include Request::Builder
28
+
29
+ option :my_option
30
+ option :my_header_value
31
+
32
+ configure do
33
+ adapter :net_http
34
+ response_middleware :json
35
+ request_middleware :json
36
+ method :get
37
+ logger Logger.new(STDOUT)
38
+ timeout 10
39
+
40
+ host 'http://api.forismatic.com/'
41
+ path '/api/1.0/'
42
+
43
+ params do
44
+ # you can pass a block or lambda to params
45
+ # this allows you to use object variables or configuration variables
46
+ param 'param1', &:my_option
47
+ param 'param2', -> { "#{my_option}" }
48
+ param('param3') { my_option }
49
+ param 'param4' 'string'
50
+ end
51
+
52
+ headers do
53
+ header 'header1', &:my_header_value
54
+ end
55
+
56
+ # in body you can use object variables or configuration variables too
57
+ body do
58
+ {
59
+ 'hello' => lang,
60
+ 'path' => config.path
61
+ }
62
+ end
63
+
64
+ # this callback executes after receiving the response
65
+ before_validate do |body|
66
+ body.delete('unnecessary_param')
67
+ body.deep_symbolize_keys
68
+ body
69
+ end
70
+
71
+ # you can use dry-schema to validate response
72
+ schema do
73
+ required(:status).value(eql?: 'success')
74
+ end
75
+ end
76
+ end
77
+ ```
78
+
79
+ After you describe your request class you can use it:
80
+
81
+ ```ruby
82
+ result = MyApiClient.call(my_option: 'some_value', my_header_value: 'header_value')
83
+
84
+ result.success? # true if status code == 200 and schema is valid
85
+ result.failure?
86
+ result.status # result code - Integer
87
+ result.headers # hash of headers
88
+ result.body # raw body from request
89
+ result.schema_result # result of dry-schema
90
+ result.full_errors # array of errors from dry-schema
91
+ ```
26
92
 
27
93
  ## Development
28
94
 
@@ -11,16 +11,16 @@ module Request
11
11
  end
12
12
 
13
13
  module ClassMethods
14
- def config
15
- @config ||= RequestConfig.new
14
+ def config(conf=nil)
15
+ @config ||= conf.deep_dup || RequestConfig.new
16
16
  end
17
17
 
18
18
  def configure(conf = nil, &block)
19
- @config = conf.dup if conf
19
+ config(conf)
20
20
 
21
21
  config.instance_eval(&block) if block
22
22
  end
23
23
  end
24
24
  end
25
25
  end
26
- end
26
+ end
@@ -5,28 +5,24 @@ module Request
5
5
 
6
6
  delegate_missing_to :context
7
7
 
8
- attrs = [:host, :path, :method, :request_middleware, :response_middleware, :adapter, :logger, :timeout]
8
+ attrs = [:params, :callbacks, :headers, :body, :host, :path, :method, :request_middleware, :response_middleware, :adapter, :logger, :timeout]
9
9
 
10
- attr_reader :context, :body_conf, :headers_conf, :params_conf, :callbacks_conf, :stubs
11
- attr_writer *attrs, :stubs
12
-
13
- alias callbacks callbacks_conf
10
+ attr_reader :context, :stubs
11
+ attr_writer *attrs, :stubs, :context
14
12
 
15
13
  def initialize
14
+ @body = nil
16
15
  @host = nil
17
16
  @path = '/'
18
17
  @method = :get
19
18
  @request_middleware = :json
20
19
  @response_middleware = :json
21
- @adapter = :net_http
20
+ @adapter = Request::Builder.default_adapter
22
21
  @stubs = Faraday::Adapter::Test::Stubs.new
23
22
  @logger = nil
24
23
  @timeout = 30
25
- @body_conf = RequestConfig::Body.new
26
- @headers_conf = RequestConfig::Headers.new
27
- @params_conf = RequestConfig::Params.new
28
- @callbacks_conf = RequestConfig::Callbacks.new
29
24
  @response_schema = Dry::Schema.Params
25
+ @context = nil
30
26
  end
31
27
 
32
28
  attrs.each do |attr|
@@ -41,10 +37,22 @@ module Request
41
37
 
42
38
  [:before_validate].each do |name|
43
39
  define_method name do |&block|
44
- callbacks.send(name, &block)
40
+ callbacks[name] = block
45
41
  end
46
42
  end
47
43
 
44
+ def headers
45
+ @headers ||= HashWithIndifferentAccess.new
46
+ end
47
+
48
+ def params
49
+ @params ||= HashWithIndifferentAccess.new
50
+ end
51
+
52
+ def callbacks
53
+ @callbacks ||= HashWithIndifferentAccess.new
54
+ end
55
+
48
56
  def stubs(value = nil, &block)
49
57
  if block_given?
50
58
  @stubs.instance_eval(&block)
@@ -65,36 +73,32 @@ module Request
65
73
  end
66
74
  end
67
75
 
68
- def body(&block)
69
- return body_conf.set(&block) if block_given?
70
-
71
- body_conf.to_h
72
- end
73
-
74
- def headers(&block)
75
- headers_conf.instance_eval(&block) if block_given?
76
-
77
- headers_conf
78
- end
79
-
80
76
  def header(key, value = nil, &block)
81
- headers_conf.header(key, value, &block)
82
- end
83
-
84
- def params(&block)
85
- params_conf.instance_eval(&block) if block_given?
86
-
87
- params_conf
77
+ if value.nil? && block.nil?
78
+ value_with_context(headers[key])
79
+ else
80
+ headers[key] = block || value
81
+ end
88
82
  end
89
83
 
90
84
  def param(key, value = nil, &block)
91
- params_conf.param(key, value, &block)
85
+ if value.nil? && block.nil?
86
+ value_with_context(params[key])
87
+ else
88
+ params[key] = block || value
89
+ end
92
90
  end
93
91
 
94
- def context=(context)
95
- @context = context
92
+ def each_header
93
+ headers.each do |key, value|
94
+ yield key, value_with_context(value)
95
+ end
96
+ end
96
97
 
97
- [body_conf, headers_conf, params_conf, callbacks_conf].each { |cnf| cnf.context = context }
98
+ def each_param
99
+ params.each do |key, value|
100
+ yield key, value_with_context(value)
101
+ end
98
102
  end
99
103
 
100
104
  def [] property
@@ -5,7 +5,7 @@ module Request
5
5
  return obj unless obj.is_a?(Proc)
6
6
 
7
7
  case obj.arity
8
- when 1, -1, -2 then instance_exec(@context, &obj)
8
+ when 1, -1, -2 then instance_exec(self, &obj)
9
9
  else instance_exec(&obj)
10
10
  end
11
11
  end
@@ -1,5 +1,5 @@
1
1
  module Request
2
2
  module Builder
3
- VERSION = "0.1.0"
3
+ VERSION = "0.1.2"
4
4
  end
5
5
  end
@@ -6,11 +6,6 @@ require "active_support/core_ext/module/delegation"
6
6
  require_relative "builder/version"
7
7
  require_relative "builder/value_with_context"
8
8
  require_relative "builder/request_config"
9
- require_relative "builder/request_config/base"
10
- require_relative "builder/request_config/body"
11
- require_relative "builder/request_config/params"
12
- require_relative "builder/request_config/headers"
13
- require_relative "builder/request_config/callbacks"
14
9
  require_relative "builder/result"
15
10
  require_relative "builder/dsl"
16
11
  require_relative "builder/connection"
@@ -25,8 +20,8 @@ module Request
25
20
 
26
21
  class << base
27
22
  alias_method :__new, :new
28
- def new(*args)
29
- e = __new(*args)
23
+ def new(*args, **kwargs)
24
+ e = __new(*args, **kwargs)
30
25
  e.send(:set_config_context)
31
26
  e
32
27
  end
@@ -34,12 +29,18 @@ module Request
34
29
  end
35
30
 
36
31
  module ClassMethods
37
- def call(**args)
38
- new(**args).call
32
+ def call(*args, **kwargs)
33
+ new(*args, **kwargs).call
39
34
  end
40
35
  alias perform call
41
36
  end
42
37
 
38
+ def self.default_adapter(adapter = nil)
39
+ @default_adapter = adapter if adapter
40
+
41
+ @default_adapter || :net_http
42
+ end
43
+
43
44
  def call
44
45
  do_request
45
46
  result
@@ -58,11 +59,11 @@ module Request
58
59
  @response = connection.send(config.method.downcase, config.path) do |req|
59
60
  req.options.timeout = config.timeout
60
61
 
61
- config.params.each do |key, value|
62
+ config.each_param do |key, value|
62
63
  req.params[key] = value
63
64
  end
64
65
 
65
- config.headers.each do |key, value|
66
+ config.each_header do |key, value|
66
67
  req.headers[key] = value
67
68
  end
68
69
 
@@ -9,14 +9,14 @@ Gem::Specification.new do |spec|
9
9
  spec.authors = ["Anton Kostyuk"]
10
10
  spec.email = ["anton.kostuk.2012@gmail.com"]
11
11
 
12
- spec.summary = %q{Request DSL}
13
- spec.description = %q{Request DSL}
14
- spec.homepage = "https://github.com/RainbowPonny/request-builder"
12
+ spec.summary = %q{Ruby Request DSL}
13
+ spec.description = %q{Simple DSL wrapper for faraday gem}
14
+ spec.homepage = "https://github.com/RainbowPonny/ruby-request-builder"
15
15
  spec.license = "MIT"
16
16
  spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
17
17
 
18
18
  spec.metadata["homepage_uri"] = spec.homepage
19
- spec.metadata["source_code_uri"] = "https://github.com/RainbowPonny/request-builder"
19
+ spec.metadata["source_code_uri"] = "https://github.com/RainbowPonny/ruby-request-builder"
20
20
 
21
21
  spec.files = Dir["CHANGELOG.md", "LICENSE", "README.md", "request-builder.gemspec", "lib/**/*"]
22
22
  spec.bindir = "bin"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: request-builder
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Anton Kostyuk
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-09-17 00:00:00.000000000 Z
11
+ date: 2022-12-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -114,7 +114,7 @@ dependencies:
114
114
  - - "~>"
115
115
  - !ruby/object:Gem::Version
116
116
  version: '3.0'
117
- description: Request DSL
117
+ description: Simple DSL wrapper for faraday gem
118
118
  email:
119
119
  - anton.kostuk.2012@gmail.com
120
120
  executables: []
@@ -127,22 +127,17 @@ files:
127
127
  - lib/request/builder/connection.rb
128
128
  - lib/request/builder/dsl.rb
129
129
  - lib/request/builder/request_config.rb
130
- - lib/request/builder/request_config/base.rb
131
- - lib/request/builder/request_config/body.rb
132
- - lib/request/builder/request_config/callbacks.rb
133
- - lib/request/builder/request_config/headers.rb
134
- - lib/request/builder/request_config/params.rb
135
130
  - lib/request/builder/result.rb
136
131
  - lib/request/builder/value_with_context.rb
137
132
  - lib/request/builder/version.rb
138
133
  - request-builder.gemspec
139
- homepage: https://github.com/RainbowPonny/request-builder
134
+ homepage: https://github.com/RainbowPonny/ruby-request-builder
140
135
  licenses:
141
136
  - MIT
142
137
  metadata:
143
- homepage_uri: https://github.com/RainbowPonny/request-builder
144
- source_code_uri: https://github.com/RainbowPonny/request-builder
145
- post_install_message:
138
+ homepage_uri: https://github.com/RainbowPonny/ruby-request-builder
139
+ source_code_uri: https://github.com/RainbowPonny/ruby-request-builder
140
+ post_install_message:
146
141
  rdoc_options: []
147
142
  require_paths:
148
143
  - lib
@@ -157,8 +152,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
157
152
  - !ruby/object:Gem::Version
158
153
  version: '0'
159
154
  requirements: []
160
- rubygems_version: 3.1.6
161
- signing_key:
155
+ rubygems_version: 3.3.7
156
+ signing_key:
162
157
  specification_version: 4
163
- summary: Request DSL
158
+ summary: Ruby Request DSL
164
159
  test_files: []
@@ -1,37 +0,0 @@
1
- module Request
2
- module Builder
3
- class RequestConfig::Base
4
- include ValueWithContext
5
-
6
- attr_accessor :context
7
- attr_reader :store
8
-
9
- delegate_missing_to :context
10
-
11
- def initialize(context = nil)
12
- @context = context
13
- end
14
-
15
- def each(&block)
16
- raise NotImplementedError unless store.is_a?(Enumerable)
17
-
18
- store.each do |value|
19
- value[1] = value_with_context(value[1])
20
- block.call(value)
21
- end
22
- end
23
-
24
- def [] key
25
- raise NotImplementedError unless store.is_a?(Enumerable)
26
-
27
- value_with_context(store[key])
28
- end
29
-
30
- private
31
-
32
- def store
33
- raise NotImplementedError
34
- end
35
- end
36
- end
37
- end
@@ -1,21 +0,0 @@
1
- module Request
2
- module Builder
3
- class RequestConfig::Body < Request::Builder::RequestConfig::Base
4
- def set(&block)
5
- raise ArgumentError, 'You must provide a block' unless block_given?
6
-
7
- @store = block
8
- end
9
-
10
- def to_h
11
- value_with_context(store)
12
- end
13
-
14
- private
15
-
16
- def store
17
- @store ||= nil
18
- end
19
- end
20
- end
21
- end
@@ -1,29 +0,0 @@
1
- module Request
2
- module Builder
3
- class RequestConfig::Callbacks < Request::Builder::RequestConfig::Base
4
- [:before_validate].each do |name|
5
- define_method name do |&block|
6
- raise ArgumentError, 'You must provide a block' unless block
7
-
8
- store[name] = block
9
- end
10
- end
11
-
12
- def each(&block)
13
- store.each do |value|
14
- block.call(value)
15
- end
16
- end
17
-
18
- def [] key
19
- store[key]
20
- end
21
-
22
- private
23
-
24
- def store
25
- @store ||= HashWithIndifferentAccess.new
26
- end
27
- end
28
- end
29
- end
@@ -1,17 +0,0 @@
1
- module Request
2
- module Builder
3
- class RequestConfig::Headers < Request::Builder::RequestConfig::Base
4
- def header(key, value = nil, &block)
5
- raise ArgumentError, 'Must provide a value or block' if !value && !block
6
-
7
- store[key] = block || value
8
- end
9
-
10
- private
11
-
12
- def store
13
- @store ||= HashWithIndifferentAccess.new
14
- end
15
- end
16
- end
17
- end
@@ -1,17 +0,0 @@
1
- module Request
2
- module Builder
3
- class RequestConfig::Params < Request::Builder::RequestConfig::Base
4
- def param(key, value = nil, &block)
5
- raise ArgumentError, 'Must provide a value or block' if !value && !block
6
-
7
- store[key] = block || value
8
- end
9
-
10
- private
11
-
12
- def store
13
- @store ||= HashWithIndifferentAccess.new
14
- end
15
- end
16
- end
17
- end