stub_requests 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 1bf4b020989fae9d4edf349c5eddb3c5aa3447ccb14f316e5e016be19636205f
4
+ data.tar.gz: 40f01c2d62daa3957f5d0331abbbca3df824f1300236cc744541427e696c9bd0
5
+ SHA512:
6
+ metadata.gz: 98f4dfc578779e820b115309a966d412054927fdb7a58487cc11dcd221e3bee013fdc772917324285039000815f8dc9d05788a8aced3b43e4e72d044de0927e1
7
+ data.tar.gz: 05376f6ec2b7ec7f4974af7c635fd6f6596f5b49beed93e2cedcb0f51db147574757bd6ffbe75bf5259123e309dae7aa0d0677f0063022ef532fc25a3cc8caa3
data/.codeclimate.yml ADDED
@@ -0,0 +1,23 @@
1
+ ---
2
+ version: '2'
3
+ plugins:
4
+ duplication:
5
+ enabled: true
6
+ config:
7
+ languages:
8
+ - ruby
9
+ fixme:
10
+ enabled: true
11
+ flog:
12
+ enabled: true
13
+ markdownlint:
14
+ enabled: true
15
+ reek:
16
+ enabled: true
17
+ config:
18
+ file: .reek.yml
19
+ rubocop:
20
+ enabled: true
21
+ channel: rubocop-0-63
22
+ config:
23
+ file: .rubocop.yml
data/.editorconfig ADDED
@@ -0,0 +1,13 @@
1
+ # EditorConfig is awesome: http://EditorConfig.org
2
+
3
+ # top-most EditorConfig file
4
+ root = true
5
+
6
+ # Unix-style newlines with a newline ending every file
7
+ [*]
8
+ charset = utf-8
9
+ end_of_line = lf
10
+ indent_size = 2
11
+ indent_style = space
12
+ insert_final_newline = true
13
+ trim_trailing_whitespace = true
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /Gemfile.lock
10
+ .rspec_status
data/.mdlrc ADDED
@@ -0,0 +1 @@
1
+ rules "~MD013"
data/.reek.yml ADDED
@@ -0,0 +1,12 @@
1
+ # Auto generated by Reeks --todo flag
2
+ ---
3
+ detectors:
4
+ ManualDispatch:
5
+ exclude:
6
+ - Object#blank?
7
+ Attribute:
8
+ exclude:
9
+ - StubRequests#logger
10
+
11
+ exclude_paths:
12
+ - vendor/bundle
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,112 @@
1
+ require:
2
+ - rubocop-rspec
3
+
4
+ inherit_mode:
5
+ merge:
6
+ - Exclude
7
+
8
+ AllCops:
9
+ TargetRubyVersion: 2.5
10
+ Include:
11
+ - "Rakefile"
12
+ - "lib/**/*"
13
+ - "bin/**/*"
14
+ - "spec/**/*"
15
+ Exclude:
16
+ - "lib/stub_requests/core_ext/**/*"
17
+ - "Gemfile.lock"
18
+ - "bin/setup"
19
+
20
+ Lint/HandleExceptions:
21
+ Enabled: true
22
+
23
+ Lint/UselessAssignment:
24
+ Enabled: true
25
+
26
+ Metrics/AbcSize:
27
+ Max: 38
28
+
29
+ Metrics/CyclomaticComplexity:
30
+ Max: 7
31
+
32
+ Metrics/LineLength:
33
+ Max: 120
34
+
35
+ Metrics/MethodLength:
36
+ Max: 13
37
+
38
+ Metrics/BlockLength:
39
+ Enabled: true
40
+ Exclude:
41
+ - '**/spec/**/*.rb'
42
+
43
+ Metrics/PerceivedComplexity:
44
+ Max: 8
45
+
46
+ Naming/AccessorMethodName:
47
+ Enabled: true
48
+
49
+ Naming/ConstantName:
50
+ Enabled: true
51
+
52
+ Naming/FileName:
53
+ Enabled: true
54
+ Exclude:
55
+ - '**/Gemfile'
56
+
57
+ Naming/UncommunicativeMethodParamName:
58
+ AllowedNames:
59
+ - ex
60
+
61
+ RSpec/AlignLeftLetBrace:
62
+ Enabled: true
63
+
64
+ RSpec/FilePath:
65
+ Enabled: true
66
+ Exclude:
67
+ - 'spec/stub_requests/webmock_builder_spec.rb'
68
+
69
+ RSpec/NestedGroups:
70
+ Max: 4
71
+ Enabled: true
72
+
73
+ Style/FrozenStringLiteralComment:
74
+ Enabled: true
75
+
76
+ Style/Documentation:
77
+ Enabled: true
78
+
79
+ Style/GlobalVars:
80
+ Enabled: true
81
+
82
+ Style/ModuleFunction:
83
+ Enabled: false
84
+
85
+ Style/StringLiterals:
86
+ Enabled: true
87
+ EnforcedStyle: double_quotes
88
+ ConsistentQuotesInMultiline: true
89
+
90
+ Style/StringLiteralsInInterpolation:
91
+ Enabled: true
92
+
93
+ Style/SymbolArray:
94
+ Enabled: true
95
+ EnforcedStyle: brackets
96
+
97
+ Style/TernaryParentheses:
98
+ Enabled: true
99
+ EnforcedStyle: require_parentheses_when_complex
100
+ AllowSafeAssignment: true
101
+
102
+ Style/TrailingCommaInArguments:
103
+ Enabled: true
104
+ EnforcedStyleForMultiline: comma
105
+
106
+ Style/TrailingCommaInArrayLiteral:
107
+ Enabled: true
108
+ EnforcedStyleForMultiline: comma
109
+
110
+ Style/TrailingCommaInHashLiteral:
111
+ Enabled: true
112
+ EnforcedStyleForMultiline: comma
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-2.5.3
data/.simplecov ADDED
@@ -0,0 +1,23 @@
1
+ require "simplecov-json"
2
+
3
+ SimpleCov.formatters = SimpleCov::Formatter::MultiFormatter.new([
4
+ SimpleCov::Formatter::HTMLFormatter,
5
+ SimpleCov::Formatter::JSONFormatter,
6
+ ])
7
+
8
+ SimpleCov.configure do
9
+ command_name "RSpec"
10
+ refuse_coverage_drop
11
+ minimum_coverage_by_file 80
12
+ maximum_coverage_drop 5
13
+ at_exit do
14
+ SimpleCov.result.format!
15
+ end
16
+ end
17
+
18
+ SimpleCov.start do
19
+ add_filter '/spec/'
20
+ add_filter '/bin/'
21
+ add_filter '/gemfiles/'
22
+ add_filter '/examples/'
23
+ end
data/.travis.yml ADDED
@@ -0,0 +1,37 @@
1
+ language: ruby
2
+ cache: bundler
3
+ env:
4
+ matrix:
5
+ - COV=false
6
+ global:
7
+ - CC_TEST_REPORTER_ID=8ef08c06ceb394c164b97b5d4c5874b016b1a85a760cf05521503dac6dde0eeb
8
+ - GIT_COMMITTED_AT=$(if [ "$TRAVIS_PULL_REQUEST" == "false" ]; then git log -1 --pretty=format:%ct;
9
+ else git log -1 --skip 1 --pretty=format:%ct; fi)
10
+ before_install:
11
+ - gem install bundler -v 2.0.1
12
+ - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
13
+ - chmod +x ./cc-test-reporter
14
+ before_script:
15
+ - "./cc-test-reporter before-build"
16
+ script:
17
+ - if [[ "${COV}" = "true" ]]; then bundle exec rubocop -P; fi;
18
+ - if [[ "${COV}" = "true" ]]; then bundle exec reek .; fi;
19
+ - bundle exec rspec
20
+ after_script:
21
+ - if [[ "${COV}" = "true" ]]; then ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT; fi;
22
+ rvm:
23
+ - 2.3.7
24
+ - 2.4.5
25
+ matrix:
26
+ fast_finish: true
27
+ include:
28
+ - rvm: 2.5.3
29
+ env: COV=true
30
+ deploy:
31
+ provider: rubygems
32
+ api_key:
33
+ secure: EhdN7pRCvnoI/WYKo4/B4NzzT+OAswhlDrW56KK0CmKmR8SrEBOwBB10MxEmOM24cMSrZGYb1MQCGqQ7yt2Erx4Z2E6OVSAlUIlWjhNaFhyTFmxQB5pP3ulH5gGJuw6BYFomPTW504BKIMay/IzMnbcx9fORsKlckU9W0WC7jMrVkmhPrMRBrrxaLW7ZJuV3CxsdVVLm4c1CQiGu3Lm6oXPBGe2L1Ctw+q/BHQvXJIDE3smJ4aNZBSpQouzlw8HSJd3Poq7m8AP8foc7oA9brrx55to3dBkhVJxeZa7fvYP+3J3IZUVL+5mOr+QXwivZnqL2NmIOZZjOisf1njQO5Mk4v/HM5wBgi8R5RMBp55A6iz7ZPca9Tj5DoVEINGfncQ0JGAeovx2/Z/g9/Dj3zr2kU9fI51tomnSJbmgxZ4wzwTP8afUdGEa013AeVcGQlXUnBljsU4MttVXufXPptWm9ABtN2oTvPNs4pYzXliGjT97d1TS4hhf7I+Fd6yUXPOyNokmrVMT68ZWou3IBM+gm8He41oNe9l/17vevG+GpOMjPo54ASpTuOc6DcuWGJPkvBPZ3ijz7kn3c/YT+LpSy7zxwjn3QA4VGj4W5JzlsLmlCSBsan9X8SIgmqY41Z/J/R86L+ndW+TY8m4oxl86LF0J2w0JAX97pcyD4Bo4=
34
+ gem: stub_requests
35
+ on:
36
+ tags: true
37
+ repo: mhenrixon/stub_requests
data/.yardopts ADDED
@@ -0,0 +1 @@
1
+ --no-private --protected lib/**/*.rb - README.md
data/CHANGELOG.md ADDED
File without changes
@@ -0,0 +1,64 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ - Using welcoming and inclusive language
18
+ - Being respectful of differing viewpoints and experiences
19
+ - Gracefully accepting constructive criticism
20
+ - Focusing on what is best for the community
21
+ - Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ - The use of sexualized language or imagery and unwelcome sexual attention or advances
26
+ - Trolling, insulting/derogatory comments, and personal or political attacks
27
+ - Public or private harassment
28
+ - Publishing others' private information, such as a physical or electronic address, without explicit permission
29
+ - Other conduct which could reasonably be considered inappropriate in a professional setting
30
+
31
+ ## Our Responsibilities
32
+
33
+ Project maintainers are responsible for clarifying the standards of acceptable
34
+ behavior and are expected to take appropriate and fair corrective action in
35
+ response to any instances of unacceptable behavior.
36
+
37
+ Project maintainers have the right and responsibility to remove, edit, or
38
+ reject comments, commits, code, wiki edits, issues, and other contributions
39
+ that are not aligned to this Code of Conduct, or to ban temporarily or
40
+ permanently any contributor for other behaviors that they deem inappropriate,
41
+ threatening, offensive, or harmful.
42
+
43
+ ## Scope
44
+
45
+ This Code of Conduct applies both within project spaces and in public spaces
46
+ when an individual is representing the project or its community. Examples of
47
+ representing a project or community include using an official project e-mail
48
+ address, posting via an official social media account, or acting as an appointed
49
+ representative at an online or offline event. Representation of a project may be
50
+ further defined and clarified by project maintainers.
51
+
52
+ ## Enforcement
53
+
54
+ Project maintainers who do not follow or enforce the Code of Conduct in good
55
+ faith may face temporary or permanent repercussions as determined by other
56
+ members of the project's leadership.
57
+
58
+ ## Attribution
59
+
60
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
61
+ available at [http://contributor-covenant.org/version/1/4][version]
62
+
63
+ [homepage]: http://contributor-covenant.org
64
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in stub_requests.gemspec
4
+ gemspec
5
+
6
+ gem "rb-readline"
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Mikael Henriksson <mikael@zoolutions.se> (mhenrixon)
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,138 @@
1
+ # StubRequests
2
+
3
+ ## Badges
4
+
5
+ [![Build Status](https://travis-ci.org/mhenrixon/stub_requests.svg?branch=master)](https://travis-ci.org/mhenrixon/stub_requests) [![Maintainability](https://api.codeclimate.com/v1/badges/c9217e458c2a77fff1bc/maintainability)](https://codeclimate.com/github/mhenrixon/stub_requests/maintainability) [![Test Coverage](https://api.codeclimate.com/v1/badges/c9217e458c2a77fff1bc/test_coverage)](https://codeclimate.com/github/mhenrixon/stub_requests/test_coverage)
6
+
7
+ This gem attempts to solve a problem I've had for the time with WebMock.
8
+
9
+ When something changes, I have to update every single stub_request.
10
+
11
+ This gem allows me to only update the crucial parts while abstracting away things like service URI's, endpoint definitions and focus on the important things.
12
+
13
+ This is achieve by keeping a registry over the service endpoints.
14
+
15
+ <!-- MarkdownTOC -->
16
+
17
+ - [Installation](#installation)
18
+ - [Usage](#usage)
19
+ - [Future Improvements](#future-improvements)
20
+ - [API Client Gem](#api-client-gem)
21
+ - [Debugging](#debugging)
22
+ - [Development](#development)
23
+ - [Contributing](#contributing)
24
+ - [License](#license)
25
+ - [Code of Conduct](#code-of-conduct)
26
+
27
+ <!-- /MarkdownTOC -->
28
+
29
+ ## Installation
30
+
31
+ Add this line to your application's Gemfile:
32
+
33
+ ```ruby
34
+ gem "stub_requests"
35
+ ```
36
+
37
+ And then execute:
38
+
39
+ `bundle`
40
+
41
+ Or install it yourself as:
42
+
43
+ `gem install stub_requests`
44
+
45
+ ## Usage
46
+
47
+ To use the gem we need to register some service endpoints. In the following example we are connecting to a rails inspired service.
48
+
49
+ The naming of the `service_id` and `endpoint_id`'s is irrelevant. This is just how we look things up in the registry.
50
+
51
+ ```ruby
52
+ StubRequests.register_service(:google_ads, "https://api.google.com/v5") do
53
+ register(:index, :get, "ads")
54
+ register(:show, :get, "ads/:id")
55
+ register(:update, :patch, "ads/:id")
56
+ register(:create, :post, "ads")
57
+ register(:destroy, :delete, "ads/:id")
58
+ end
59
+ ```
60
+
61
+ Now we have a list of endpoints we can stub.
62
+
63
+ ```ruby
64
+ StubRequests.stub_endpoint(:google_ads, :index)
65
+ .to_return(code: 204, body: "")
66
+
67
+ # This is the equivalent of doing the following in WebMock
68
+ Settings.google_ads_base_uri = "https://api.google.com/v5"
69
+
70
+ WebMock.stub_request(:get, "#{Settings.google_ads_base_uri}/ads")
71
+ .to_return(status: 204, body: "")
72
+ ```
73
+
74
+ So far so good but not much of a gain yet. The real power comes when we don't have to interpolate a bunch of URLs all the time.
75
+
76
+ ```ruby
77
+ StubRequests.stub_endpoint(:google_ads, :update, id: 1) do
78
+ with(body: request_body.to_json)
79
+ to_return(code: 200, body: response_body.to_json)
80
+ end
81
+
82
+ # This is the equivalent of doing the following in WebMock
83
+ Settings.google_ads_base_uri = "https://api.google.com/v5"
84
+
85
+ WebMock.stub_request(:patch, "#{Settings.google_ads_base_uri}/ads/#{id}")
86
+ .with(body: request_body.to_json)
87
+ .to_return(status: 200, body: response_body.to_json)
88
+ ```
89
+
90
+ First of all we reduce a lot of duplication.
91
+
92
+ Imagine a code base with thousands of stubbed request. You always have to look at the defined URL to understand which request is actually being called.
93
+
94
+ Madness!!
95
+
96
+ ## Future Improvements
97
+
98
+ ### API Client Gem
99
+
100
+ Since we have a service + endpoint registry, I was thinking it might make
101
+ sense to make this into an API client. Not sure yet, maybe this will become multiple gems in the future so that someone can pick and choose.
102
+
103
+ Anyway, the idea was to provide endpoint calls in production and stubbed
104
+ requests in tests using the same registry.
105
+
106
+ ### Debugging
107
+
108
+ I want to provide information about where a request stub was created from. In the project I am currently working this would have saved me a days work already.
109
+
110
+ ## Development
111
+
112
+ After checking out the repo, run `bin/setup` to install dependencies.
113
+
114
+ Then, run `rake spec` to run the tests. You can also run `bin/console` for an
115
+ interactive prompt that will allow you to experiment.
116
+
117
+ To install this gem onto your local machine run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
118
+
119
+ ## Contributing
120
+
121
+ Bug reports and pull requests are welcome on GitHub at:
122
+ [issues](https://github.com/mhenrixon/stub_requests).
123
+
124
+ This project is intended to be a safe, welcoming space for collaboration, and
125
+ contributors are expected to adhere to the [Contributor Covenant](cc) code of conduct.
126
+
127
+ ## License
128
+
129
+ The gem is available as open source under the terms of the [MIT License](mit).
130
+
131
+ ## Code of Conduct
132
+
133
+ Everyone interacting in the StubRequests project’s codebases, issue trackers,
134
+ chat rooms and mailing lists is expected to follow the [code of conduct](coc).
135
+
136
+ [coc]:https://github.com/mhenrixon/stub_requests/blob/master/CODE_OF_CONDUCT.md
137
+ [cc]: http://contributor-covenant.org
138
+ [mit]: https://opensource.org/licenses/MIT
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
data/_config.yml ADDED
@@ -0,0 +1 @@
1
+ theme: jekyll-theme-merlot
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "stub_requests"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/index.md ADDED
@@ -0,0 +1,37 @@
1
+ # Welcome to GitHub Pages
2
+
3
+ You can use the [editor on GitHub](https://github.com/mhenrixon/stub_requests/edit/master/index.md) to maintain and preview the content for your website in Markdown files.
4
+
5
+ Whenever you commit to this repository, GitHub Pages will run [Jekyll](https://jekyllrb.com/) to rebuild the pages in your site, from the content in your Markdown files.
6
+
7
+ ## Markdown
8
+
9
+ Markdown is a lightweight and easy-to-use syntax for styling your writing. It includes conventions for
10
+
11
+ ```markdown
12
+ Syntax highlighted code block
13
+
14
+ # Header 1
15
+ ## Header 2
16
+ ### Header 3
17
+
18
+ - Bulleted
19
+ - List
20
+
21
+ 1. Numbered
22
+ 2. List
23
+
24
+ **Bold** and _Italic_ and `Code` text
25
+
26
+ [Link](url) and ![Image](src)
27
+ ```
28
+
29
+ For more details see [GitHub Flavored Markdown](https://guides.github.com/features/mastering-markdown/).
30
+
31
+ ### Jekyll Themes
32
+
33
+ Your Pages site will use the layout and styles from the Jekyll theme you have selected in your [repository settings](https://github.com/mhenrixon/stub_requests/settings). The name of this theme is saved in the Jekyll `_config.yml` configuration file.
34
+
35
+ ### Support or Contact
36
+
37
+ Having trouble with Pages? Check out our [documentation](https://help.github.com/categories/github-pages-basics/) or [contact support](https://github.com/contact) and we’ll help you sort it out.
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ #
4
+ # Abstraction over WebMock to reduce duplication
5
+ #
6
+ # @author Mikael Henriksson <mikael@zoolutions.se>
7
+ # @since 0.1.0
8
+ #
9
+ module StubRequests
10
+ #
11
+ # Module API abstraction to reduce the amount of WebMock.stub_request
12
+ #
13
+ # @note This module can either be used by its class methods
14
+ # or included in say RSpec
15
+ #
16
+ module API
17
+ # extends "self"
18
+ # @!parse extend self
19
+ extend self
20
+
21
+ # :nodoc:
22
+ def self.included(base)
23
+ base.send(:extend, self)
24
+ end
25
+
26
+ # Register a service in the service registry
27
+ #
28
+ # @param [Symbol] service_id a descriptive id for the service
29
+ # @param [Symbol] service_uri the uri used to call the service
30
+ #
31
+ # @return [Service] a new service or a previously registered service
32
+ #
33
+ # :reek:UtilityFunction
34
+ def register_service(service_id, service_uri, &block)
35
+ service = ServiceRegistry.instance.register_service(service_id, service_uri)
36
+ Docile.dsl_eval(service.endpoint_registry, &block) if block.present?
37
+ service
38
+ end
39
+
40
+ #
41
+ # Stub a request to a registered service endpoint
42
+ #
43
+ # @param [Symbol] service_id the id of a registered service
44
+ # @param [Symbol] endpoint_id the id of a registered endpoint
45
+ # @param [Hash<Symbol>] uri_replacements a list of uri replacements
46
+ # @param [Hash<Symbol>] options
47
+ # @option options [optional, Hash<Symbol>] :request webmock request options
48
+ # @option options [optional, Hash<Symbol>] :response webmock response options
49
+ # @option options [optional, Array, Exception, StandardError, String] :error webmock error to raise
50
+ # @option options [optional, TrueClass] :timeout set to truthy to raise timeeout with webmock
51
+ #
52
+ # @example Stub a request to a registered service endpoint
53
+ # register_stub(
54
+ # :google_api,
55
+ # :get_map_location,
56
+ # { request: { headers: { "Accept" => "application/json" }}},
57
+ # { response: { body: { id: "abyasdjasd", status: "successful" }}}
58
+ # )
59
+ #
60
+ # @see #stub_http_request
61
+ # @return [WebMock::RequestStub] a mocked request
62
+ #
63
+ # :reek:UtilityFunction
64
+ # :reek:LongParameterList { max_params: 5 }
65
+ def stub_endpoint(service_id, endpoint_id, uri_replacements = {}, options = {}, &callback)
66
+ service = ServiceRegistry.instance.get_service!(service_id)
67
+ endpoint = service.get_endpoint!(endpoint_id)
68
+ uri = URI::Builder.build(service.uri, endpoint.uri_template, uri_replacements)
69
+
70
+ StubRequests::WebMockBuilder.build(endpoint.verb, uri, options, &callback)
71
+ end
72
+ end
73
+ end