my_api_client 0.13.0 → 0.16.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (82) hide show
  1. checksums.yaml +4 -4
  2. data/.circleci/config.yml +102 -36
  3. data/.dependabot/config.yml +34 -0
  4. data/.envrc.skeleton +1 -0
  5. data/.rubocop.yml +8 -10
  6. data/.rubocop_challenge.yml +3 -0
  7. data/.rubocop_todo.yml +5 -10
  8. data/CHANGELOG.md +229 -0
  9. data/Gemfile.lock +48 -43
  10. data/README.jp.md +98 -23
  11. data/bin/console +4 -0
  12. data/example/api_clients/application_api_client.rb +13 -0
  13. data/example/api_clients/my_error_api_client.rb +34 -0
  14. data/example/api_clients/my_errors.rb +27 -0
  15. data/example/api_clients/my_pagination_api_client.rb +18 -0
  16. data/example/api_clients/my_rest_api_client.rb +48 -0
  17. data/example/api_clients/my_status_api_client.rb +22 -0
  18. data/lib/generators/rails/templates/application_api_client.rb.erb +0 -11
  19. data/lib/my_api_client.rb +10 -2
  20. data/lib/my_api_client/base.rb +2 -18
  21. data/lib/my_api_client/config.rb +0 -29
  22. data/lib/my_api_client/default_error_handlers.rb +64 -0
  23. data/lib/my_api_client/error_handling.rb +13 -23
  24. data/lib/my_api_client/error_handling/generator.rb +30 -10
  25. data/lib/my_api_client/error_handling/{process_retry_option.rb → retry_option_processor.rb} +1 -1
  26. data/lib/my_api_client/errors.rb +0 -53
  27. data/lib/my_api_client/errors/api_limit_error.rb +6 -0
  28. data/lib/my_api_client/errors/client_error.rb +93 -0
  29. data/lib/my_api_client/errors/network_error.rb +43 -0
  30. data/lib/my_api_client/errors/server_error.rb +42 -0
  31. data/lib/my_api_client/params/request.rb +7 -10
  32. data/lib/my_api_client/request.rb +48 -70
  33. data/lib/my_api_client/request/basic.rb +32 -0
  34. data/lib/my_api_client/request/executor.rb +89 -0
  35. data/lib/my_api_client/request/logger.rb +37 -0
  36. data/lib/my_api_client/request/pagination.rb +39 -0
  37. data/lib/my_api_client/rspec/matcher_helper.rb +2 -2
  38. data/lib/my_api_client/rspec/matchers/be_handled_as_an_error.rb +2 -0
  39. data/lib/my_api_client/rspec/matchers/request_to.rb +3 -4
  40. data/lib/my_api_client/version.rb +1 -1
  41. data/my_api/.envrc.skeleton +3 -0
  42. data/my_api/.gitignore +14 -0
  43. data/my_api/.jetskeep +1 -0
  44. data/my_api/.rspec +3 -0
  45. data/my_api/.ruby-version +1 -0
  46. data/my_api/Gemfile +23 -0
  47. data/my_api/Gemfile.lock +243 -0
  48. data/my_api/Procfile +7 -0
  49. data/my_api/README.md +54 -0
  50. data/my_api/Rakefile +4 -0
  51. data/my_api/app/controllers/application_controller.rb +5 -0
  52. data/my_api/app/controllers/error_controller.rb +21 -0
  53. data/my_api/app/controllers/pagination_controller.rb +58 -0
  54. data/my_api/app/controllers/rest_controller.rb +60 -0
  55. data/my_api/app/controllers/status_controller.rb +11 -0
  56. data/my_api/app/helpers/application_helper.rb +5 -0
  57. data/my_api/app/jobs/application_job.rb +7 -0
  58. data/my_api/app/models/application_item.rb +5 -0
  59. data/my_api/config.ru +7 -0
  60. data/my_api/config/application.rb +73 -0
  61. data/my_api/config/dynamodb.yml +22 -0
  62. data/my_api/config/environments/development.rb +9 -0
  63. data/my_api/config/environments/production.rb +11 -0
  64. data/my_api/config/environments/test.rb +9 -0
  65. data/my_api/config/routes.rb +17 -0
  66. data/my_api/db/.gitkeep +0 -0
  67. data/my_api/public/404.html +67 -0
  68. data/my_api/public/422.html +67 -0
  69. data/my_api/public/500.html +66 -0
  70. data/my_api/public/favicon.ico +0 -0
  71. data/my_api/public/index.html +91 -0
  72. data/my_api/spec/controllers/error_controller_spec.rb +43 -0
  73. data/my_api/spec/controllers/pagination_controller_spec.rb +73 -0
  74. data/my_api/spec/controllers/rest_controller_spec.rb +99 -0
  75. data/my_api/spec/controllers/status_controller_spec.rb +47 -0
  76. data/my_api/spec/fixtures/payloads/posts-index.json +51 -0
  77. data/my_api/spec/fixtures/payloads/posts-show.json +53 -0
  78. data/my_api/spec/spec_helper.rb +31 -0
  79. data/my_api_client.gemspec +1 -1
  80. metadata +62 -9
  81. data/lib/my_api_client/logger.rb +0 -36
  82. data/renovate.json +0 -5
@@ -0,0 +1,54 @@
1
+ # My API
2
+
3
+ This is the real API for integration testing with `my_api_client`.
4
+
5
+ It's built by [Ruby on Jets](https://rubyonjets.com/).
6
+
7
+ ## APIs
8
+
9
+ ### My Rest API
10
+
11
+ This is a simple REST API that returns a specified response.
12
+
13
+ * `GET rest`
14
+ * `GET rest/:id`
15
+ * `POST rest`
16
+ * `POST/PUT/PATCH rest/:id`
17
+ * `DELETE rest/:id`
18
+
19
+ ### My Status API
20
+
21
+ This API returns arbitrary status code.
22
+
23
+ * `GET status/:status`
24
+
25
+ ### My Error API
26
+
27
+ This API returns arbitrary error code as JSON.
28
+
29
+ * `GET error/:code`
30
+
31
+ ### My Pagination API
32
+
33
+ This API returns a response including pagination links.
34
+
35
+ * `GET pagination?page=1`
36
+
37
+ ## Deployment
38
+
39
+ You need to prepare following environment variables:
40
+
41
+ * `AWS_REGION`
42
+ * `AWS_ACCESS_KEY_ID`
43
+ * `AWS_SECRET_ACCESS_KEY`
44
+
45
+ For information on how to create an AWS access key and secret, see the following site:
46
+
47
+ :link: [Minimal Deploy IAM Policy \- Jets Ruby Serverless Framework](https://rubyonjets.com/docs/extras/minimal-deploy-iam/)
48
+
49
+
50
+ And execute following command:
51
+
52
+ ```sh
53
+ $ bundle exec jets deploy
54
+ ```
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'jets'
4
+ Jets.load_tasks
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ # The root of the controller
4
+ class ApplicationController < Jets::Controller::Base
5
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ # THe error code API
4
+ class ErrorController < ApplicationController
5
+ # GET error/:code
6
+ def show
7
+ render status: :bad_request, json: error_response
8
+ end
9
+
10
+ private
11
+
12
+ def error_response
13
+ code = params[:code].to_i
14
+ {
15
+ error: {
16
+ code: code,
17
+ message: "You requested error code: #{code}",
18
+ },
19
+ }
20
+ end
21
+ end
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ # A Pagination API
4
+ class PaginationController < ApplicationController
5
+ # GET pagination
6
+ def index
7
+ case params[:page]
8
+ when '1', nil
9
+ render status: :ok, json: first_page
10
+ when '2'
11
+ render status: :ok, json: second_page
12
+ when '3'
13
+ render status: :ok, json: third_page
14
+ else
15
+ render status: :not_found, json: ''
16
+ end
17
+ end
18
+
19
+ private
20
+
21
+ def first_page
22
+ {
23
+ links: {
24
+ next: page_link(2),
25
+ },
26
+ page: 1,
27
+ }
28
+ end
29
+
30
+ def second_page
31
+ {
32
+ links: {
33
+ next: page_link(3),
34
+ previous: page_link(1),
35
+ },
36
+ page: 2,
37
+ }
38
+ end
39
+
40
+ def third_page
41
+ {
42
+ links: {
43
+ previous: page_link(2),
44
+ },
45
+ page: 3,
46
+ }
47
+ end
48
+
49
+ # NOTE: `#pagination_url` returns `http://xxxxx.execute-api.ap-northeast-1.amazonaws.com/pagination`
50
+ # but it should be `https://xxxxx.execute-api.ap-northeast-1.amazonaws.com/dev/pagination`.
51
+ # So this is workaround.
52
+ def page_link(page)
53
+ query_strings = '?' + { page: page }.to_query
54
+ uri = File.join(ENV['JETS_HOST'], ENV['JETS_STAGE'], pagination_path)
55
+ uri.sub!('http://', 'https://')
56
+ URI.join(uri, query_strings)
57
+ end
58
+ end
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ # A REST API
4
+ class RestController < ApplicationController
5
+ # GET rest
6
+ def index
7
+ result = params[:order] == 'desc' ? posts.reverse : posts
8
+ render status: :ok, json: result
9
+ end
10
+
11
+ # GET rest/:id
12
+ def show
13
+ render status: :ok, json: find_post(id: id)
14
+ end
15
+
16
+ # POST rest
17
+ def create
18
+ render status: :created,
19
+ json: create_post(title: params[:title])
20
+ end
21
+
22
+ # POST/PUT/PATCH rest/:id
23
+ def update
24
+ render status: :ok,
25
+ json: update_post(id: id, title: params[:title])
26
+ end
27
+
28
+ # DELETE rest/:id
29
+ def delete
30
+ render status: :no_content
31
+ end
32
+
33
+ private
34
+
35
+ def id
36
+ params[:id].to_i
37
+ end
38
+
39
+ def posts
40
+ [
41
+ { id: 1, title: 'Title 1' },
42
+ { id: 2, title: 'Title 2' },
43
+ { id: 3, title: 'Title 3' },
44
+ ]
45
+ end
46
+
47
+ def find_post(id:)
48
+ posts.find { |p| p[:id] == id }
49
+ end
50
+
51
+ def create_post(title:)
52
+ { id: 4, title: title }
53
+ end
54
+
55
+ def update_post(id:, title:)
56
+ find_post(id: id).tap do |post|
57
+ post[:title] = title
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ # THe status API
4
+ class StatusController < ApplicationController
5
+ # GET status/:status
6
+ def show
7
+ status = params[:status].to_i
8
+ render status: status,
9
+ json: { message: "You requested status code: #{status}" }
10
+ end
11
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ # The root of the helper module
4
+ module ApplicationHelper
5
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ # The root of the job class
4
+ class ApplicationJob < Jets::Job::Base
5
+ # Adjust to increase the default timeout for all Job classes
6
+ class_timeout 60
7
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ # The root of the model
4
+ class ApplicationItem < Dynomite::Item
5
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This file is used by Rack-based servers to start the application.
4
+
5
+ require 'jets'
6
+ Jets.boot
7
+ run Jets.application
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ Jets.application.configure do
4
+ config.project_name = 'my_api'
5
+ config.mode = 'api'
6
+
7
+ config.prewarm.enable = true # default is true
8
+ # config.prewarm.rate = '30 minutes' # default is '30 minutes'
9
+ # config.prewarm.concurrency = 2 # default is 2
10
+ # config.prewarm.public_ratio = 3 # default is 3
11
+
12
+ # config.env_extra = 2 # can also set this with JETS_ENV_EXTRA
13
+ # config.autoload_paths = []
14
+
15
+ # config.asset_base_url = 'https://cloudfront.domain.com/assets' # example
16
+
17
+ # config.cors = true # for '*'' # defaults to false
18
+ # config.cors = '*.mydomain.com' # for specific domain
19
+
20
+ # config.function.timeout = 30 # defaults to 30
21
+ # config.function.role = "arn:aws:iam::#{Jets.aws.account}:role/service-role/pre-created"
22
+ # config.function.memory_size = 1536
23
+
24
+ # Default is 'EDGE'
25
+ # https://docs.aws.amazon.com/apigateway/api-reference/link-relation/restapi-create/#endpointConfiguration
26
+ # config.api.endpoint_type = 'PRIVATE'
27
+
28
+ # config.function.environment = {
29
+ # global_app_key1: "global_app_value1",
30
+ # global_app_key2: "global_app_value2",
31
+ # }
32
+ # More examples:
33
+ # config.function.dead_letter_config = { target_arn: "arn" }
34
+ # config.function.vpc_config = {
35
+ # security_group_ids: %w[sg-1 sg-2],
36
+ # subnet_ids: %w[subnet-1 subnet-2],
37
+ # }
38
+ # The config.function settings to the CloudFormation Lambda Function properties.
39
+ # http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-lambda-function.html
40
+ # Underscored format can be used for keys to make it look more ruby-ish.
41
+
42
+ # Assets settings
43
+ # The config.assets.folders are folders within the public folder that will be set
44
+ # to public-read on s3 and served directly. IE: public/assets public/images public/packs
45
+ # config.assets.folders = %w[assets images packs]
46
+ # config.assets.max_age = 3600 # when to expire assets
47
+ #
48
+ # # IE: "public, max-age=3600" # override max_age for more fine-grain control.
49
+ # config.assets.cache_control = nil
50
+ #
51
+ # IE: https://cloudfront.com/my/base/path, defaults to the s3 bucket url
52
+ # IE: https://s3-us-west-2.amazonaws.com/demo-dev-s3bucket-1inlzkvujq8zb
53
+ # config.assets.base_url = nil
54
+ #
55
+
56
+ # config.api.endpoint_type = 'PRIVATE' # Default is 'EDGE' https://amzn.to/2r0Iu2L
57
+ # config.api.authorization_type = "AWS_IAM" # default is 'NONE' https://amzn.to/2qZ7zLh
58
+
59
+ # More info: http://rubyonjets.com/docs/routing/custom-domain/
60
+ # config.domain.hosted_zone_name = "example.com"
61
+ # us-west-2 REGIONAL endpoint - takes 2 minutes
62
+ # config.domain.cert_arn = "arn:aws:acm:us-west-2:112233445566:certificate/8d8919ce-a710-4050-976b-b33da991e123"
63
+ # us-east-1 EDGE endpoint - takes 10-15 minutes
64
+ # config.domain.cert_arn = "arn:aws:acm:us-east-1:112233445566:certificate/d68472ba-04f8-45ba-b9db-14f839d57123"
65
+ # config.domain.endpoint_type = "EDGE"
66
+
67
+ # By default logger needs to log to $stderr for CloudWatch to receive Lambda messages, but for
68
+ # local testing environment you may want to log these messages to 'test.log' file to keep your
69
+ # testing suite output readable.
70
+ # config.logger = Jets::Logger.new($strerr)
71
+
72
+ config.controllers.default_protect_from_forgery = false
73
+ end
@@ -0,0 +1,22 @@
1
+ # Jets::Config.project_namespace is special value results in using the project namespace. Example :
2
+ # table_namespace: <%= Jets.config.project_namespace %>
3
+ # This is the default value.
4
+
5
+ development:
6
+ table_namespace: <%= Jets.config.table_namespace %>
7
+ # More examples:
8
+ # table_namespace: demo-dev
9
+
10
+ endpoint: http://localhost:8000 # comment out if want to test with real dynamodb
11
+ # on AWS. You can also set the DYNAMODB_ENDPOINT environment variable.
12
+ # You can also are actually deploying a development environment is to
13
+ # change bin/server and export DYNAMODB_ENDPOINT=http://localhost:8000 at the top
14
+ # there.
15
+
16
+ test:
17
+ # table_namespace: proj # do not include the env
18
+ endpoint: http://localhost:8000
19
+ table_namespace: <%= Jets.config.table_namespace %>
20
+
21
+ production:
22
+ table_namespace: <%= Jets.config.table_namespace %>
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ Jets.application.configure do
4
+ # Example:
5
+ # config.function.memory_size = 1536
6
+
7
+ # config.action_mailer.raise_delivery_errors = false
8
+ # Docs: http://rubyonjets.com/docs/email-sending/
9
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ Jets.application.configure do
4
+ # Example:
5
+ # config.function.memory_size = 2048
6
+
7
+ # Ignore bad email addresses and do not raise email delivery errors.
8
+ # Set this to true and configure the email server for immediate delivery to raise delivery errors.
9
+ # Docs: http://rubyonjets.com/docs/email-sending/
10
+ # config.action_mailer.raise_delivery_errors = false
11
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ Jets.application.configure do
4
+ # Tell Action Mailer not to deliver emails to the real world.
5
+ # The :test delivery method accumulates sent emails in the
6
+ # ActionMailer::Base.deliveries array.
7
+ # Docs: http://rubyonjets.com/docs/email-sending/
8
+ config.action_mailer.delivery_method = :test
9
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ Jets.application.routes.draw do
4
+ root 'jets/public#show'
5
+
6
+ resources 'rest', only: %i[index show create update delete]
7
+
8
+ get 'status/:status', to: 'status#show'
9
+ get 'error/:code', to: 'error#show'
10
+ get 'pagination', to: 'pagination#index'
11
+
12
+ # The jets/public#show controller can serve static utf8 content out of the public folder.
13
+ # Note, as part of the deploy process Jets uploads files in the public folder to s3
14
+ # and serves them out of s3 directly. S3 is well suited to serve static assets.
15
+ # More info here: https://rubyonjets.com/docs/extras/assets-serving/
16
+ any '*catchall', to: 'jets/public#show'
17
+ end
File without changes
@@ -0,0 +1,67 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>The page you were looking for doesn't exist (404)</title>
5
+ <meta name="viewport" content="width=device-width,initial-scale=1">
6
+ <style>
7
+ .rails-default-error-page {
8
+ background-color: #EFEFEF;
9
+ color: #2E2F30;
10
+ text-align: center;
11
+ font-family: arial, sans-serif;
12
+ margin: 0;
13
+ }
14
+
15
+ .rails-default-error-page div.dialog {
16
+ width: 95%;
17
+ max-width: 33em;
18
+ margin: 4em auto 0;
19
+ }
20
+
21
+ .rails-default-error-page div.dialog > div {
22
+ border: 1px solid #CCC;
23
+ border-right-color: #999;
24
+ border-left-color: #999;
25
+ border-bottom-color: #BBB;
26
+ border-top: #B00100 solid 4px;
27
+ border-top-left-radius: 9px;
28
+ border-top-right-radius: 9px;
29
+ background-color: white;
30
+ padding: 7px 12% 0;
31
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
32
+ }
33
+
34
+ .rails-default-error-page h1 {
35
+ font-size: 100%;
36
+ color: #730E15;
37
+ line-height: 1.5em;
38
+ }
39
+
40
+ .rails-default-error-page div.dialog > p {
41
+ margin: 0 0 1em;
42
+ padding: 1em;
43
+ background-color: #F7F7F7;
44
+ border: 1px solid #CCC;
45
+ border-right-color: #999;
46
+ border-left-color: #999;
47
+ border-bottom-color: #999;
48
+ border-bottom-left-radius: 4px;
49
+ border-bottom-right-radius: 4px;
50
+ border-top-color: #DADADA;
51
+ color: #666;
52
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
53
+ }
54
+ </style>
55
+ </head>
56
+
57
+ <body class="rails-default-error-page">
58
+ <!-- This file lives in public/404.html -->
59
+ <div class="dialog">
60
+ <div>
61
+ <h1>The page you were looking for doesn't exist.</h1>
62
+ <p>You may have mistyped the address or the page may have moved.</p>
63
+ </div>
64
+ <p>If you are the application owner check the logs for more information.</p>
65
+ </div>
66
+ </body>
67
+ </html>