evil-client 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (84) hide show
  1. checksums.yaml +7 -0
  2. data/.codeclimate.yml +7 -0
  3. data/.gitignore +9 -0
  4. data/.rspec +3 -0
  5. data/.rubocop.yml +98 -0
  6. data/.travis.yml +17 -0
  7. data/Gemfile +9 -0
  8. data/LICENSE.txt +21 -0
  9. data/README.md +144 -0
  10. data/Rakefile +6 -0
  11. data/docs/base_url.md +38 -0
  12. data/docs/documentation.md +9 -0
  13. data/docs/headers.md +59 -0
  14. data/docs/http_method.md +31 -0
  15. data/docs/index.md +127 -0
  16. data/docs/license.md +19 -0
  17. data/docs/model.md +173 -0
  18. data/docs/operation.md +0 -0
  19. data/docs/overview.md +0 -0
  20. data/docs/path.md +48 -0
  21. data/docs/query.md +99 -0
  22. data/docs/responses.md +66 -0
  23. data/docs/security.md +102 -0
  24. data/docs/settings.md +32 -0
  25. data/evil-client.gemspec +25 -0
  26. data/lib/evil/client.rb +97 -0
  27. data/lib/evil/client/connection.rb +35 -0
  28. data/lib/evil/client/connection/net_http.rb +57 -0
  29. data/lib/evil/client/dsl.rb +110 -0
  30. data/lib/evil/client/dsl/files.rb +37 -0
  31. data/lib/evil/client/dsl/operation.rb +102 -0
  32. data/lib/evil/client/dsl/operations.rb +41 -0
  33. data/lib/evil/client/dsl/scope.rb +34 -0
  34. data/lib/evil/client/dsl/security.rb +57 -0
  35. data/lib/evil/client/middleware.rb +81 -0
  36. data/lib/evil/client/middleware/base.rb +15 -0
  37. data/lib/evil/client/middleware/merge_security.rb +16 -0
  38. data/lib/evil/client/middleware/normalize_headers.rb +13 -0
  39. data/lib/evil/client/middleware/stringify_form.rb +36 -0
  40. data/lib/evil/client/middleware/stringify_json.rb +15 -0
  41. data/lib/evil/client/middleware/stringify_multipart.rb +32 -0
  42. data/lib/evil/client/middleware/stringify_multipart/part.rb +36 -0
  43. data/lib/evil/client/middleware/stringify_query.rb +31 -0
  44. data/lib/evil/client/model.rb +65 -0
  45. data/lib/evil/client/operation.rb +34 -0
  46. data/lib/evil/client/operation/request.rb +42 -0
  47. data/lib/evil/client/operation/response.rb +40 -0
  48. data/lib/evil/client/operation/response_error.rb +12 -0
  49. data/lib/evil/client/operation/unexpected_response_error.rb +16 -0
  50. data/mkdocs.yml +21 -0
  51. data/spec/features/instantiation_spec.rb +68 -0
  52. data/spec/features/middleware_spec.rb +75 -0
  53. data/spec/features/operation_with_documentation_spec.rb +41 -0
  54. data/spec/features/operation_with_files_spec.rb +40 -0
  55. data/spec/features/operation_with_form_body_spec.rb +158 -0
  56. data/spec/features/operation_with_headers_spec.rb +99 -0
  57. data/spec/features/operation_with_http_method_spec.rb +45 -0
  58. data/spec/features/operation_with_json_body_spec.rb +156 -0
  59. data/spec/features/operation_with_path_spec.rb +47 -0
  60. data/spec/features/operation_with_query_spec.rb +84 -0
  61. data/spec/features/operation_with_response_spec.rb +109 -0
  62. data/spec/features/operation_with_security_spec.rb +228 -0
  63. data/spec/features/scoping_spec.rb +48 -0
  64. data/spec/spec_helper.rb +23 -0
  65. data/spec/support/test_client.rb +15 -0
  66. data/spec/unit/evil/client/connection/net_http_spec.rb +38 -0
  67. data/spec/unit/evil/client/dsl/files_spec.rb +37 -0
  68. data/spec/unit/evil/client/dsl/operation_spec.rb +233 -0
  69. data/spec/unit/evil/client/dsl/operations_spec.rb +27 -0
  70. data/spec/unit/evil/client/dsl/scope_spec.rb +30 -0
  71. data/spec/unit/evil/client/dsl/security_spec.rb +135 -0
  72. data/spec/unit/evil/client/dsl_spec.rb +57 -0
  73. data/spec/unit/evil/client/middleware/merge_security_spec.rb +32 -0
  74. data/spec/unit/evil/client/middleware/normalize_headers_spec.rb +17 -0
  75. data/spec/unit/evil/client/middleware/stringify_form_spec.rb +63 -0
  76. data/spec/unit/evil/client/middleware/stringify_json_spec.rb +61 -0
  77. data/spec/unit/evil/client/middleware/stringify_multipart/part_spec.rb +59 -0
  78. data/spec/unit/evil/client/middleware/stringify_multipart_spec.rb +62 -0
  79. data/spec/unit/evil/client/middleware/stringify_query_spec.rb +40 -0
  80. data/spec/unit/evil/client/middleware_spec.rb +46 -0
  81. data/spec/unit/evil/client/model_spec.rb +100 -0
  82. data/spec/unit/evil/client/operation/request_spec.rb +49 -0
  83. data/spec/unit/evil/client/operation/response_spec.rb +61 -0
  84. metadata +271 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e3ce5ab3f46cc4b5835d2ca831403c9f7090c7df
4
+ data.tar.gz: 0a46913313a0cb465864f58a0af5ca8c69862b2d
5
+ SHA512:
6
+ metadata.gz: 58cd0f896ad69d342e01eb8d0839012154ab09940138ad337c723bc5b2f9c8e1eb35fa7726b9544e9451c9ac179faa94da5286f3e645711453db67e611ddfaed
7
+ data.tar.gz: 7a300cbf1de16050958f698ab869679babac7888998b3eacc27de369f8df654ea2b9a26a60c02f0124925fc18e65932536b3e6b5685315f44cbdaae4f54f2243
data/.codeclimate.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ engines:
3
+ rubocop:
4
+ enabled: true
5
+ checks:
6
+ Rubocop/Style/FrozenStringLiteralComment:
7
+ enabled: false
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --require spec_helper
2
+ --format documentation
3
+ --color
data/.rubocop.yml ADDED
@@ -0,0 +1,98 @@
1
+ ---
2
+ AllCops:
3
+ DisplayCopNames: true
4
+ DisplayStyleGuide: true
5
+ StyleGuideCopsOnly: true
6
+ TargetRubyVersion: 2.3
7
+
8
+ Lint/HandleExceptions:
9
+ Exclude:
10
+ - spec/**/*_spec.rb
11
+
12
+ Lint/RescueException:
13
+ Exclude:
14
+ - spec/**/*_spec.rb
15
+
16
+ Metrics/LineLength:
17
+ Max: 80
18
+ Exclude:
19
+ - spec/**/*_spec.rb
20
+
21
+ Style/AccessorMethodName:
22
+ Exclude:
23
+ - spec/**/*_spec.rb
24
+
25
+ Style/Alias:
26
+ EnforcedStyle: prefer_alias_method
27
+
28
+ Style/AsciiComments:
29
+ Enabled: false
30
+
31
+ Style/CaseEquality:
32
+ Enabled: false
33
+
34
+ Style/ClassAndModuleChildren:
35
+ Enabled: false
36
+
37
+ Style/Documentation:
38
+ Enabled: false
39
+
40
+ Style/DoubleNegation:
41
+ Enabled: false
42
+
43
+ Style/EmptyLinesAroundClassBody:
44
+ Enabled: false
45
+
46
+ Style/EmptyLinesAroundModuleBody:
47
+ Enabled: false
48
+
49
+ Style/EmptyLineBetweenDefs:
50
+ Enabled: false
51
+
52
+ Style/FileName:
53
+ Enabled: false
54
+
55
+ Style/Lambda:
56
+ Exclude:
57
+ - spec/**/*_spec.rb
58
+
59
+ Style/LambdaCall:
60
+ Enabled: false
61
+
62
+ Style/ModuleFunction:
63
+ Enabled: false
64
+
65
+ Style/RaiseArgs:
66
+ EnforcedStyle: compact
67
+
68
+ Style/Semicolon:
69
+ Exclude:
70
+ - spec/**/*_spec.rb
71
+
72
+ Style/SignalException:
73
+ EnforcedStyle: semantic
74
+
75
+ Style/SingleLineBlockParams:
76
+ Enabled: false
77
+
78
+ Style/SingleLineMethods:
79
+ Exclude:
80
+ - spec/**/*_spec.rb
81
+
82
+ Style/SpaceBeforeFirstArg:
83
+ Enabled: false
84
+
85
+ Style/SpecialGlobalVars:
86
+ Exclude:
87
+ - Gemfile
88
+ - dry-initializer.gemspec
89
+
90
+ Style/StringLiterals:
91
+ EnforcedStyle: double_quotes
92
+
93
+ Style/StringLiteralsInInterpolation:
94
+ EnforcedStyle: double_quotes
95
+
96
+ Style/TrivialAccessors:
97
+ Exclude:
98
+ - spec/**/*_spec.rb
data/.travis.yml ADDED
@@ -0,0 +1,17 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ script:
6
+ - bundle exec rspec
7
+ - bundle exec rubocop
8
+ rvm:
9
+ - '2.3.0'
10
+ - ruby-head
11
+ - jruby-9.1.0.0
12
+ - jruby-head
13
+ before_install: gem install bundler -v 1.12.5
14
+ matrix:
15
+ allow_failures:
16
+ - rvm: ruby-head
17
+ - rvm: jruby-head
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in evil-client.gemspec
4
+ gemspec
5
+
6
+ group :development, :test do
7
+ gem "pry", platform: :mri
8
+ gem "pry-byebug", platform: :mri
9
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Andrew Kozin (aka nepalez), andrew.kozin@gmail.com
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,144 @@
1
+ # Evil::Client
2
+
3
+ Human-friendly DSL for writing HTTP(s) clients in Ruby
4
+
5
+ <a href="https://evilmartians.com/">
6
+ <img src="https://evilmartians.com/badges/sponsored-by-evil-martians.svg" alt="Sponsored by Evil Martians" width="236" height="54"></a>
7
+
8
+ [![Gem Version][gem-badger]][gem]
9
+ [![Build Status][travis-badger]][travis]
10
+ [![Dependency Status][gemnasium-badger]][gemnasium]
11
+ [![Code Climate][codeclimate-badger]][codeclimate]
12
+ [![Inline docs][inch-badger]][inch]
13
+
14
+ ## Intro
15
+
16
+ The gem allows writing http(s) clients in a way close to [Swagger][swagger] specifications. Like in Swagger, you need to specify models and operations in domain-specific terms. In addition, the gem supports settings and scopes for instantiating clients and sending requests in idiomatic Ruby.
17
+
18
+ The gem stands away from mutable states and monkey patching when possible. To support multithreading all instances are immutable (though not frozen to avoid performance loss). Its DSL is backed on top of [dry-initializer][dry-initializer] gem, and supposes heavy usage of [dry-types][dry-types] system of contracts.
19
+
20
+ For now the DSL supports clients to **json** and **form data** APIs out of the box. Because of high variance of XML-based APIs, building corresponding clients require more efforts on a middleware level.
21
+
22
+ [swagger]: http://swagger.io
23
+ [dry-initializer]: http://dry-rb.org/gems./dry-initializer
24
+ [dry-types]: http://dry-rb.org/gems./dry-types
25
+
26
+ ## Installation
27
+
28
+ Add this line to your application's Gemfile:
29
+
30
+ ```ruby
31
+ gem 'evil-client'
32
+ ```
33
+
34
+ And then execute:
35
+
36
+ ```shell
37
+ $ bundle
38
+ ```
39
+
40
+ Or install it yourself as:
41
+
42
+ ```shell
43
+ $ gem install evil-client
44
+ ```
45
+
46
+ ## Synopsis
47
+
48
+ The following example gives an idea of how a client to remote API looks like when written on top of `Evil::Client` using [dry-types][dry-types]-based contracts.
49
+
50
+ ```ruby
51
+ require "evil-client"
52
+ require "dry-types"
53
+
54
+ class CatsClient < Evil::Client
55
+ # describe a client-specific model of cat (the furry pinnacle of evolution)
56
+ class Cat < Evil::Client::Model
57
+ attribute :name, type: Dry::Types["strict.string"], optional: true
58
+ attribute :color, type: Dry::Types["strict.string"]
59
+ attribute :age, type: Dry::Types["coercible.int"], default: proc { 0 }
60
+ end
61
+
62
+ # Define settings the client initialized with
63
+ # The settings parameterizes operations when necessary
64
+ settings do
65
+ param :domain, type: Dry::Types["strict.string"] # required!
66
+ option :version, type: Dry::Types["coercible.int"], default: proc { 0 }
67
+ option :user, type: Dry::Types["strict.string"] # required!
68
+ option :password, type: Dry::Types["strict.string"] # required!
69
+ end
70
+
71
+ # Define a base url using
72
+ base_url do |settings|
73
+ "https://#{settings.domain}.example.com/api/v#{settings.version}/"
74
+ end
75
+
76
+ # Definitions shared by all operations
77
+ operation do |settings|
78
+ security { basic_auth settings.user, settings.password }
79
+ end
80
+
81
+ # Operation-specific definition to update a cat by id
82
+ # This provides low-level DSL `operations[:update_cat].call`
83
+ operation :update_cat do |settings|
84
+ http_method :patch
85
+ path { |id:, **| "cats/#{id}" } # id will be taken from request parameters
86
+
87
+ body format: "json" do
88
+ attribute :name, optional: true
89
+ attribute :color, optional: true
90
+ attribute :age, optional: true
91
+ end
92
+
93
+ response 200 do |body:, **|
94
+ Cat.new JSON.parse(body) # define that the body should be wrapped to cat
95
+ end
96
+
97
+ response 422, raise: true do |body:, **|
98
+ JSON.parse(body) # expect 422 to return json data
99
+ end
100
+ end
101
+
102
+ # Add top-level DSL
103
+ scope :cats do
104
+ scope do |id|
105
+ def find(**data)
106
+ operations[:update_cat].call(id: id, **data)
107
+ end
108
+ end
109
+ end
110
+ end
111
+
112
+ # Instantiate a client with concrete settings
113
+ cat_client = CatClient.new "awesome-cats", # domain
114
+ version: 1,
115
+ user: "cat_lover",
116
+ password: "purr"
117
+
118
+ # Use low-level DSL to send requests
119
+ cat_client.operations[:update_cat].call id: 4,
120
+ age: 10,
121
+ name: "Agamemnon",
122
+ color: "tabby"
123
+
124
+ # Use top-level DSL for the same request
125
+ cat_client.cats[4].call(age: 10, name: "Agamemnon", color: "tabby")
126
+
127
+ # Both the methods send `PATCH https://awesom-cats.example.com/api/v1/cats/7`
128
+ # with a specified body and headers (authorization via basic_auth)
129
+ ```
130
+
131
+ ## License
132
+
133
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
134
+
135
+ [codeclimate-badger]: https://img.shields.io/codeclimate/github/evilmartians/evil-client.svg?style=flat
136
+ [codeclimate]: https://codeclimate.com/github/evilmartians/evil-client
137
+ [gem-badger]: https://img.shields.io/gem/v/evil-client.svg?style=flat
138
+ [gem]: https://rubygems.org/gems/evil-client
139
+ [gemnasium-badger]: https://img.shields.io/gemnasium/evilmartians/evil-client.svg?style=flat
140
+ [gemnasium]: https://gemnasium.com/evilmartians/evil-client
141
+ [inch-badger]: http://inch-ci.org/github/evilmartians/evil-client.svg
142
+ [inch]: https://inch-ci.org/github/evilmartians/evil-client
143
+ [travis-badger]: https://img.shields.io/travis/evilmartians/evil-client/master.svg?style=flat
144
+ [travis]: https://travis-ci.org/evilmartians/evil-client
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
data/docs/base_url.md ADDED
@@ -0,0 +1,38 @@
1
+ Use `base_url` to define a base url, [operation paths][path] are relative to.
2
+
3
+ You can use [`settings`][settings] as the only argument of the declaration block.
4
+
5
+ ```ruby
6
+ require "evil-client"
7
+ require "dry-types"
8
+
9
+ class CatsClient < Evil::Client
10
+ settings do
11
+ option :version, type: Dry::Types["coercible.int"], default: proc { 1 }
12
+ end
13
+
14
+ base_url do |settings|
15
+ "https://cats.example.com/v#{settings.version}"
16
+ end
17
+
18
+ operation :find_cats do |_settings|
19
+ http_method :get
20
+ path { "cats" }
21
+ end
22
+ end
23
+ ```
24
+
25
+ After a client's instantiation...
26
+
27
+ ```ruby
28
+ client = CatsClient.new version: 3
29
+ ```
30
+
31
+ ...the following call will send a request `GET https://example.com/v3/cats`.
32
+
33
+ ```ruby
34
+ client.operations[:find_cat].call
35
+ ```
36
+
37
+ [settings]:
38
+ [path]:
@@ -0,0 +1,9 @@
1
+ Use the `documentation` to provide reference to online docs for the current operation.
2
+
3
+ ```ruby
4
+ operation :find_cat do |settings| # remember that you have access to settings
5
+ documentation "https://cats.example.com/v#{settings.version}/docs/find_cats"
6
+ end
7
+ ```
8
+
9
+ The link will be shown in exceptions risen when either request or response mismatches type constraints.
data/docs/headers.md ADDED
@@ -0,0 +1,59 @@
1
+ Use method `headers` to add several headers to a request. The declaration should have a block with several `attributes` describing corresponding headers.
2
+
3
+ ```ruby
4
+ operation :find_cat do |settings|
5
+ headers do
6
+ attribute :token if settings.version > 1
7
+ attribute :id
8
+ end
9
+ end
10
+ ```
11
+
12
+ The syntax of the attribute declaration is exactly the same as of [Evil::Client::Model][model]. Type constraints and default values are available.
13
+
14
+ All values for the headers will be taken from a request options:
15
+
16
+ ```ruby
17
+ # Sends a request with headers { "id" => 43 }
18
+ client.options[:find_cat].call id: 43
19
+ ```
20
+
21
+ As a rule, you shouldn't define authorization headers in this way. Use [the security method][security] instead.
22
+
23
+ Default headers can be declared for every request via anonymous operation. **Notice** that a default headers can be reloaded for specific operation as a whole. New declaration will overwrite all the default set headers instead of merging to them.
24
+
25
+ ```ruby
26
+ operation do
27
+ headers do
28
+ attribute :id
29
+ end
30
+ end
31
+
32
+ operation :find_cat do
33
+ headers do
34
+ attribute :cat_id
35
+ end
36
+ end
37
+
38
+ # later at the runtime the following call
39
+ # will send request with { "cat_id" => 4 } header only
40
+ client.operations[:find_cat].call id: 1, cat_id: 4
41
+ ```
42
+
43
+ According to [RFC-2616][rfc-2616], headers are case-insensitive. Inside [middleware][middleware] they are forced to lower case.
44
+
45
+ For example, while the following declaration is valid by itself, only value from `Foo` option will be sent to remote server:
46
+
47
+ ```ruby
48
+ operation do
49
+ headers do
50
+ attribute :foo
51
+ attribute :Foo
52
+ end
53
+ end
54
+ ```
55
+
56
+ [rfc-2616]: https://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2
57
+ [security]:
58
+ [model]:
59
+ [middleware]: