evil-client 0.2.1 → 0.2.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
  SHA1:
3
- metadata.gz: e3ce5ab3f46cc4b5835d2ca831403c9f7090c7df
4
- data.tar.gz: 0a46913313a0cb465864f58a0af5ca8c69862b2d
3
+ metadata.gz: 3657d0246cbb2f95df2b4aa8562d4fe192a93050
4
+ data.tar.gz: 8f9c4cd0be983d7942749801c2a440cc4650d7fb
5
5
  SHA512:
6
- metadata.gz: 58cd0f896ad69d342e01eb8d0839012154ab09940138ad337c723bc5b2f9c8e1eb35fa7726b9544e9451c9ac179faa94da5286f3e645711453db67e611ddfaed
7
- data.tar.gz: 7a300cbf1de16050958f698ab869679babac7888998b3eacc27de369f8df654ea2b9a26a60c02f0124925fc18e65932536b3e6b5685315f44cbdaae4f54f2243
6
+ metadata.gz: 482ff1dc21692b4680bdb2d342d39c624f20f7eb33f8d0f88ffa74ac3d9d2d1723977b32f281c399a42a4a472465daef5e170ede3f5e4afdbcdb04c58df306e7
7
+ data.tar.gz: 4bcb567014c69fe09a5611947c893be8e2518347344651a27f297861da5c3eb832c2132d720e85649017f799ed53610064ff1bed1aa236c42bcf3c5079cf449d
data/evil-client.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |gem|
2
2
  gem.name = "evil-client"
3
- gem.version = "0.2.1"
3
+ gem.version = "0.2.2"
4
4
  gem.author = "Andrew Kozin (nepalez)"
5
5
  gem.email = "andrew.kozin@gmail.com"
6
6
  gem.homepage = "https://github.com/evilmartians/evil-client"
@@ -13,9 +13,9 @@ Gem::Specification.new do |gem|
13
13
 
14
14
  gem.required_ruby_version = ">= 2.3"
15
15
 
16
- gem.add_runtime_dependency "dry-initializer", "~> 0.7.0"
16
+ gem.add_runtime_dependency "dry-initializer", "~> 0.9.0"
17
17
  gem.add_runtime_dependency "mime-types", "~> 3.0"
18
- gem.add_runtime_dependency "rack"
18
+ gem.add_runtime_dependency "rack", "~> 2"
19
19
 
20
20
  gem.add_development_dependency "dry-types", "~> 0.9"
21
21
  gem.add_development_dependency "rspec", "~> 3.0"
data/lib/evil/client.rb CHANGED
@@ -77,21 +77,6 @@ module Evil
77
77
  require_relative "client/operation"
78
78
  require_relative "client/dsl"
79
79
 
80
- extend DSL
81
- include Dry::Initializer.define -> { param :operations }
82
-
83
- # Builds a client instance with custom settings
84
- def self.new(*settings)
85
- super finalize(*settings)
86
- end
87
-
88
- private
89
-
90
- def initialize(schema)
91
- @operations = \
92
- schema[:operations].each_with_object({}) do |(key, val), hash|
93
- hash[key] = Operation.new val, schema[:connection]
94
- end
95
- end
80
+ extend DSL
96
81
  end
97
82
  end
@@ -13,6 +13,11 @@ class Evil::Client
13
13
  run Middleware::NormalizeHeaders
14
14
  end
15
15
 
16
+ # Adds [#operations] to a specific client's instances
17
+ def self.extended(klass)
18
+ klass.include Dry::Initializer.define -> { param :operations }
19
+ end
20
+
16
21
  # Helper to define params and options a for a client's constructor
17
22
  #
18
23
  # @example
@@ -78,24 +83,30 @@ class Evil::Client
78
83
  self
79
84
  end
80
85
 
81
- # Takes constructor arguments and builds a final schema for the instance
86
+ # Takes constructor arguments and builds a final schema for an instance
87
+ # (All the instantiation magics goes here)
82
88
  #
83
89
  # @param [Object] *args
84
90
  # @return [Hash<Symbol, Object>]
85
91
  #
86
- def finalize(*args)
92
+ def new(*args)
87
93
  settings = schema[:settings].new(*args)
88
- uri = URI(schema[:base_url].call(settings))
89
- client = schema[:connection].new(uri)
94
+ base_url = schema[:base_url].call(settings)
90
95
  middleware = schema[:middleware].finalize(settings)
91
- stack = Middleware.prepend.(middleware.(Middleware.append.(client)))
96
+ operations = schema[:operations].finalize(settings)
97
+ client = schema[:connection].new URI(base_url)
98
+ connection = Middleware.prepend.(middleware.(Middleware.append.(client)))
99
+
100
+ data = operations.each_with_object({}) do |(key, schema), hash|
101
+ hash[key] = Evil::Client::Operation.new schema, connection
102
+ end
92
103
 
93
- { connection: stack, operations: schema[:operations].finalize(settings) }
104
+ super(data)
94
105
  end
95
106
 
96
107
  private
97
108
 
98
- BASE_URL = -> (_) { fail NotImplementedError.new "Base url not defined" }
109
+ BASE_URL = -> (_) { fail NotImplementedError.new "Base url is not defined" }
99
110
 
100
111
  def schema
101
112
  @schema ||= {
@@ -10,8 +10,6 @@ class Evil::Client
10
10
  class Model
11
11
  class << self
12
12
  include Dry::Initializer::Mixin
13
- alias_method :attribute, :option
14
- alias_method :param, :option
15
13
 
16
14
  def new(value)
17
15
  return value if value.is_a? self
@@ -20,27 +18,33 @@ class Evil::Client
20
18
  end
21
19
  super value
22
20
  end
21
+ alias_method :call, :new
22
+ alias_method :[], :new
23
23
 
24
- def call(value)
25
- new(value).to_h
24
+ def attributes
25
+ @attributes ||= []
26
26
  end
27
- alias_method :[], :call
28
- end
29
27
 
30
- tolerant_to_unknown_options
28
+ def option(name, type = nil, **opts)
29
+ super.tap { attributes << name.to_sym }
30
+ end
31
+ alias_method :attribute, :option
32
+ alias_method :param, :option
33
+
34
+ private
35
+
36
+ def inherited(klass)
37
+ super
38
+ klass.instance_variable_set :@attributes, attributes.dup
39
+ end
40
+ end
31
41
 
32
42
  def ==(other)
33
- return false unless other.respond_to? :to_h
34
- to_h == other.to_h
43
+ other.respond_to?(:to_h) ? to_h == other.to_h : false
35
44
  end
36
45
 
37
46
  def to_h
38
- attributes = method(:initialize)
39
- .parameters
40
- .map { |item| item[1] unless item[0] == :keyrest }
41
- .compact
42
-
43
- attributes.each_with_object({}) do |key, hash|
47
+ self.class.attributes.each_with_object({}) do |key, hash|
44
48
  val = send(key)
45
49
  hash[key] = hashify(val) unless val == Dry::Initializer::UNDEFINED
46
50
  end
@@ -30,7 +30,7 @@ RSpec.describe "instantiation" do
30
30
  let(:client) { Test::Client.new(**options) }
31
31
 
32
32
  it "is rejected" do
33
- expect { client }.to raise_error(ArgumentError)
33
+ expect { client }.to raise_error(KeyError)
34
34
  end
35
35
  end
36
36
 
@@ -45,8 +45,8 @@ RSpec.describe "instantiation" do
45
45
  context "with unexpected option settings:" do
46
46
  before { options[:foo] = "bar" }
47
47
 
48
- it "is rejected" do
49
- expect { client }.to raise_error(ArgumentError)
48
+ it "is accepted" do
49
+ expect { client }.not_to raise_error
50
50
  end
51
51
  end
52
52
 
@@ -54,7 +54,7 @@ RSpec.describe "instantiation" do
54
54
  before { options.delete :user }
55
55
 
56
56
  it "is rejected" do
57
- expect { client }.to raise_error(ArgumentError)
57
+ expect { client }.to raise_error(KeyError)
58
58
  end
59
59
  end
60
60
 
@@ -140,7 +140,7 @@ RSpec.describe "operation with form body" do
140
140
  end
141
141
 
142
142
  it "requires mandatory arguments" do
143
- expect { operation.call bar: 2, baz: 3 }.to raise_error(ArgumentError)
143
+ expect { operation.call bar: 2, baz: 3 }.to raise_error(KeyError)
144
144
  end
145
145
 
146
146
  it "applies type restrictions" do
@@ -61,7 +61,7 @@ RSpec.describe "operation with headers" do
61
61
 
62
62
  it "requires mandatory headers" do
63
63
  expect { client.operations[:clear_data].call bar: "BAR", baz: "BAZ" }
64
- .to raise_error(ArgumentError)
64
+ .to raise_error(KeyError)
65
65
  end
66
66
 
67
67
  it "applies type constraints" do
@@ -138,7 +138,7 @@ RSpec.describe "operation with json body" do
138
138
  end
139
139
 
140
140
  it "requires mandatory arguments" do
141
- expect { operation.call bar: 2, baz: 3 }.to raise_error(ArgumentError)
141
+ expect { operation.call bar: 2, baz: 3 }.to raise_error(KeyError)
142
142
  end
143
143
 
144
144
  it "applies type restrictions" do
@@ -65,7 +65,7 @@ RSpec.describe "operation with query" do
65
65
 
66
66
  it "applies type restrictuions" do
67
67
  expect { client.operations[:filter].call id: 1, name: "Joe" }
68
- .to raise_error(ArgumentError)
68
+ .to raise_error(KeyError)
69
69
  end
70
70
 
71
71
  it "supports nesting in a Rails style" do
@@ -3,6 +3,8 @@ RSpec.describe Evil::Client::DSL, ".scope" do
3
3
  class Test::Foo
4
4
  extend Evil::Client::DSL
5
5
 
6
+ base_url { "https://example.com" }
7
+
6
8
  scope :foo do
7
9
  param :bar
8
10
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: evil-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Kozin (nepalez)
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-10-29 00:00:00.000000000 Z
11
+ date: 2016-11-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dry-initializer
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 0.7.0
19
+ version: 0.9.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 0.7.0
26
+ version: 0.9.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: mime-types
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -42,16 +42,16 @@ dependencies:
42
42
  name: rack
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ">="
45
+ - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '0'
47
+ version: '2'
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ">="
52
+ - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '0'
54
+ version: '2'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: dry-types
57
57
  requirement: !ruby/object:Gem::Requirement
@@ -199,7 +199,6 @@ files:
199
199
  - spec/unit/evil/client/dsl/operations_spec.rb
200
200
  - spec/unit/evil/client/dsl/scope_spec.rb
201
201
  - spec/unit/evil/client/dsl/security_spec.rb
202
- - spec/unit/evil/client/dsl_spec.rb
203
202
  - spec/unit/evil/client/middleware/merge_security_spec.rb
204
203
  - spec/unit/evil/client/middleware/normalize_headers_spec.rb
205
204
  - spec/unit/evil/client/middleware/stringify_form_spec.rb
@@ -257,7 +256,6 @@ test_files:
257
256
  - spec/unit/evil/client/dsl/operations_spec.rb
258
257
  - spec/unit/evil/client/dsl/scope_spec.rb
259
258
  - spec/unit/evil/client/dsl/security_spec.rb
260
- - spec/unit/evil/client/dsl_spec.rb
261
259
  - spec/unit/evil/client/middleware/merge_security_spec.rb
262
260
  - spec/unit/evil/client/middleware/normalize_headers_spec.rb
263
261
  - spec/unit/evil/client/middleware/stringify_form_spec.rb
@@ -1,57 +0,0 @@
1
- RSpec.describe Evil::Client::DSL do
2
- before do
3
- class Test::Middleware
4
- def initialize(app)
5
- @app = app
6
- end
7
-
8
- def call(env)
9
- app.call(env).reverse
10
- end
11
- end
12
-
13
- class Test::Foo
14
- extend Evil::Client::DSL
15
-
16
- settings do
17
- param :version, type: Dry::Types["coercible.int"]
18
- option :user, type: Dry::Types["strict.string"]
19
- option :password, type: Dry::Types["strict.string"]
20
- end
21
-
22
- base_url do |settings|
23
- "https://example.com/v#{settings.version}"
24
- end
25
-
26
- connection do |_|
27
- run Test::Middleware
28
- end
29
-
30
- operation do |settings|
31
- security { basic_auth settings.user, settings.password }
32
- end
33
-
34
- operation :find_user do |_|
35
- path { |id:| "users/#{id}" }
36
- http_method :get
37
-
38
- response 200 do |data|
39
- data.reverse
40
- end
41
- end
42
- end
43
- end
44
-
45
- subject { Test::Foo.finalize(4, user: "foo", password: "bar") }
46
-
47
- it "builds a proper schema" do
48
- operation = subject[:operations][:find_user]
49
-
50
- expect(operation[:security].call)
51
- .to eq headers: { "authorization" => "Basic Zm9vOmJhcg==" }
52
- expect(operation[:path].call(id: 3)).to eq "users/3"
53
- expect(operation[:method]).to eq "get"
54
-
55
- expect(operation[:responses][200][:coercer].call "foo").to eq "oof"
56
- end
57
- end