bima-shark-sdk 2.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (94) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +13 -0
  3. data/.rspec +1 -0
  4. data/.rubocop.yml +56 -0
  5. data/.travis.yml +10 -0
  6. data/CHANGELOG.md +131 -0
  7. data/Gemfile +6 -0
  8. data/LICENSE.txt +21 -0
  9. data/README.md +53 -0
  10. data/Rakefile +8 -0
  11. data/bima-shark-sdk.gemspec +36 -0
  12. data/bin/console +24 -0
  13. data/bin/setup +8 -0
  14. data/lib/bima-shark-sdk/rspec.rb +38 -0
  15. data/lib/bima-shark-sdk.rb +3 -0
  16. data/lib/shark/account.rb +7 -0
  17. data/lib/shark/activity.rb +11 -0
  18. data/lib/shark/asset.rb +13 -0
  19. data/lib/shark/base.rb +36 -0
  20. data/lib/shark/client/connection.rb +67 -0
  21. data/lib/shark/concerns/connected.rb +44 -0
  22. data/lib/shark/concerns/normalized_email.rb +13 -0
  23. data/lib/shark/configuration.rb +90 -0
  24. data/lib/shark/consent.rb +31 -0
  25. data/lib/shark/contact.rb +40 -0
  26. data/lib/shark/contact_log.rb +7 -0
  27. data/lib/shark/double_opt_in/execution.rb +50 -0
  28. data/lib/shark/double_opt_in/request.rb +45 -0
  29. data/lib/shark/error.rb +69 -0
  30. data/lib/shark/form_service/base.rb +11 -0
  31. data/lib/shark/form_service/form/container.rb +10 -0
  32. data/lib/shark/form_service/form/element.rb +83 -0
  33. data/lib/shark/form_service/form/multiple_choice.rb +21 -0
  34. data/lib/shark/form_service/form/rating_scale.rb +20 -0
  35. data/lib/shark/form_service/form/rating_star.rb +13 -0
  36. data/lib/shark/form_service/form/structure.rb +13 -0
  37. data/lib/shark/form_service/form/text_area.rb +10 -0
  38. data/lib/shark/form_service/form/text_field.rb +17 -0
  39. data/lib/shark/form_service/v2/base.rb +13 -0
  40. data/lib/shark/form_service/v2/form.rb +18 -0
  41. data/lib/shark/form_service/v2/form_input.rb +15 -0
  42. data/lib/shark/form_service/v2/form_version.rb +19 -0
  43. data/lib/shark/form_service.rb +21 -0
  44. data/lib/shark/group.rb +18 -0
  45. data/lib/shark/mailing_service/base.rb +11 -0
  46. data/lib/shark/mailing_service/configuration.rb +9 -0
  47. data/lib/shark/mailing_service/mail.rb +8 -0
  48. data/lib/shark/mailing_service/mailers/base_mailer.rb +96 -0
  49. data/lib/shark/mailing_service/renderers/context.rb +40 -0
  50. data/lib/shark/mailing_service/renderers/erb_renderer.rb +46 -0
  51. data/lib/shark/mailing_service.rb +23 -0
  52. data/lib/shark/membership.rb +16 -0
  53. data/lib/shark/middleware/compose_request.rb +41 -0
  54. data/lib/shark/middleware/status.rb +36 -0
  55. data/lib/shark/notification.rb +21 -0
  56. data/lib/shark/package.rb +7 -0
  57. data/lib/shark/permission.rb +7 -0
  58. data/lib/shark/rails.rb +13 -0
  59. data/lib/shark/rspec/fake_asset_service/object_cache.rb +76 -0
  60. data/lib/shark/rspec/fake_asset_service/public_id.rb +21 -0
  61. data/lib/shark/rspec/fake_asset_service/request.rb +154 -0
  62. data/lib/shark/rspec/fake_asset_service.rb +20 -0
  63. data/lib/shark/rspec/fake_consent_service/object_cache.rb +44 -0
  64. data/lib/shark/rspec/fake_consent_service/request.rb +53 -0
  65. data/lib/shark/rspec/fake_consent_service.rb +19 -0
  66. data/lib/shark/rspec/fake_contact_service/object_cache.rb +88 -0
  67. data/lib/shark/rspec/fake_contact_service/request.rb +170 -0
  68. data/lib/shark/rspec/fake_contact_service.rb +19 -0
  69. data/lib/shark/rspec/fake_double_opt_in/object_cache.rb +59 -0
  70. data/lib/shark/rspec/fake_double_opt_in/request.rb +133 -0
  71. data/lib/shark/rspec/fake_double_opt_in.rb +19 -0
  72. data/lib/shark/rspec/fake_mailing_service/request.rb +41 -0
  73. data/lib/shark/rspec/fake_mailing_service.rb +13 -0
  74. data/lib/shark/rspec/fake_notification_service/request.rb +53 -0
  75. data/lib/shark/rspec/fake_notification_service.rb +13 -0
  76. data/lib/shark/rspec/fake_subscription_service/object_cache.rb +54 -0
  77. data/lib/shark/rspec/fake_subscription_service/request.rb +100 -0
  78. data/lib/shark/rspec/fake_subscription_service.rb +19 -0
  79. data/lib/shark/rspec/fake_survey_service/object_cache.rb +36 -0
  80. data/lib/shark/rspec/fake_survey_service/request.rb +66 -0
  81. data/lib/shark/rspec/fake_survey_service.rb +19 -0
  82. data/lib/shark/rspec/fixtures/form_inputs.json +26 -0
  83. data/lib/shark/rspec/fixtures/form_structure.json +175 -0
  84. data/lib/shark/rspec/helpers/cache_helper.rb +44 -0
  85. data/lib/shark/rspec/helpers/fixtures.rb +26 -0
  86. data/lib/shark/rspec/helpers/form_service_helper.rb +27 -0
  87. data/lib/shark/rspec/helpers/response.rb +25 -0
  88. data/lib/shark/rspec/helpers.rb +26 -0
  89. data/lib/shark/subscription.rb +43 -0
  90. data/lib/shark/survey.rb +29 -0
  91. data/lib/shark/survey_participant.rb +37 -0
  92. data/lib/shark/version.rb +5 -0
  93. data/lib/shark.rb +91 -0
  94. metadata +291 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7a9cc4def17d9461b6ddc032cba99930bfa2337d
4
+ data.tar.gz: f6c2eeb6ee700a7ffd6a2d711ef4cfe81d7e0092
5
+ SHA512:
6
+ metadata.gz: 304bedeaea5bb6a8ed37b6c48f89c7173775d4c3e9b42c04838776b29d115c5820d7cca37d950a3d63252ea4d067b73b81f773413d12689f2d55efa6fb41787d
7
+ data.tar.gz: f3f64da6335e24a50aa4e4b7e996a2773980fafc9ca7d25076c9157f33ef884c93f2836d5a84b60a93d6e3d8362fcc3c3305f62df03c161cf8e3da6e2798d841
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .ruby-version
11
+
12
+ # rspec failure tracking
13
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/.rubocop.yml ADDED
@@ -0,0 +1,56 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.3
3
+ Exclude:
4
+ - 'config/routes.rb'
5
+ - 'db/schema.rb'
6
+ - 'lib/bima-shark-sdk.rb'
7
+ - 'node_modules/**/*'
8
+ - 'vendor/**/*'
9
+
10
+ Layout/LineLength:
11
+ Max: 100
12
+
13
+ Lint/RaiseException:
14
+ Enabled: false
15
+
16
+ Lint/StructNewOverride:
17
+ Enabled: false
18
+
19
+ Metrics/AbcSize:
20
+ Enabled: false
21
+
22
+ Metrics/BlockLength:
23
+ Enabled: false
24
+
25
+ Metrics/BlockNesting:
26
+ Enabled: false
27
+
28
+ Metrics/ClassLength:
29
+ Enabled: false
30
+
31
+ Metrics/CyclomaticComplexity:
32
+ Enabled: false
33
+
34
+ Metrics/MethodLength:
35
+ Enabled: false
36
+
37
+ Metrics/ModuleLength:
38
+ Enabled: false
39
+
40
+ Metrics/ParameterLists:
41
+ Enabled: false
42
+
43
+ Metrics/PerceivedComplexity:
44
+ Enabled: false
45
+
46
+ Style/Documentation:
47
+ Enabled: false
48
+
49
+ Style/HashEachMethods:
50
+ Enabled: true
51
+
52
+ Style/HashTransformKeys:
53
+ Enabled: true
54
+
55
+ Style/HashTransformValues:
56
+ Enabled: true
data/.travis.yml ADDED
@@ -0,0 +1,10 @@
1
+ language: ruby
2
+ cache: bundler
3
+ rvm:
4
+ - 2.3.8
5
+ - 2.5.7
6
+ before_install:
7
+ - gem install bundler -v '~> 1.17'
8
+ script:
9
+ - bundle exec rake spec
10
+ - bundle exec rubocop --config '.rubocop.yml'
data/CHANGELOG.md ADDED
@@ -0,0 +1,131 @@
1
+ ## Changelog
2
+
3
+ #### 2.3.0
4
+ - `Shark::MailingService` template can `#render` partials
5
+
6
+ #### 2.2.0
7
+ - added `Shark::ContactLog` resource
8
+ - added `Shark::MailingService::Mail#header_image` as parameter
9
+
10
+ #### 2.1.0
11
+ - added `Shark::Contact.find_by_permissions` method
12
+
13
+ #### 2.0.1
14
+ - added `Mail#reply_to` attribute
15
+
16
+ #### 2.0.0
17
+ - added `Shark::Membership.exists?` to quickly check, if a contact is member of a group
18
+ - [break] remove `SurveyService` module
19
+ - [break] rename `DoubleOptInService` => `DoubleOptIn`
20
+ - [break] remove `SubscriptionService` module
21
+ - [break] remove `NotificationService` module
22
+ - [break] remove `ContactService` module
23
+ - [break] remove `ConsentService` module
24
+ - [break] remove `AssetService` module
25
+
26
+ #### 1.0.1
27
+ - whitelist `Mail#from` as attribute
28
+
29
+ #### 1.0.0
30
+ - drop support for `json_api_client < 1.10`
31
+ - [break] drop Ruby `2.1` support
32
+ - use `Rubocop`
33
+
34
+ #### 0.14.0
35
+ - Asset Service Client supports package API
36
+
37
+ #### 0.13.4
38
+ - Add `download` link handling to the `FakeAssetService`.
39
+ - [fix] Determining the resource id in `FakeAssetService` fails when the endpoint base path is not `/`.
40
+ - [fix] `links` attribute in fake asset service response contains wrong URIs.
41
+
42
+ #### 0.13.3
43
+ - add `X-Forwarded-Proto: https` as default header
44
+
45
+ #### 0.13.2
46
+ - add `SharkSpec.stub_mailing_service`
47
+ - cleanup SharkSpec helpers with some meta programming
48
+
49
+ #### 0.13.1
50
+ - [fix] `attachments` attribute for `Mail` objects needs to be a hash
51
+
52
+ #### 0.13.0
53
+ - make all `Mail` attributes known to `BaseMailer`
54
+
55
+ #### 0.12.0
56
+ - add `MailingService.use_shark_mailer`
57
+
58
+ #### 0.11.0
59
+ - extend with MailingService
60
+
61
+ #### 0.10.3
62
+ - [fix] add backwards compatibility to Ruby 2.1
63
+ - added Travis CI
64
+
65
+ #### 0.10.2
66
+ - [fix] `ContactService::Group#has_contact?` raised exception without `includes(:contact)`
67
+
68
+ #### 0.10.1
69
+ - Double Opt In Service: added `message_footer_html` attribute to Request
70
+
71
+ #### 0.10.0
72
+ - extend with Double Opt In Service
73
+
74
+ #### 0.9.0
75
+ - extend with AssetService and SubscriptionService
76
+
77
+ #### 0.8.0
78
+ - extend with ConsentService
79
+
80
+ #### 0.7.5
81
+ - extend with NotificationService
82
+
83
+ #### 0.7.4
84
+ - [fix] `Shark::ContactService::Group#has_contact?`
85
+
86
+ #### 0.7.3
87
+ - [fix] doorkeeper spec issue
88
+ - use Rails logger when used in Rails apps
89
+
90
+ #### 0.7.2
91
+ - [fix] milacrm spec issue
92
+
93
+ #### 0.7.1
94
+ - [fix] undefined method `responds_to?` in Configuration
95
+
96
+ #### 0.7.0
97
+ - survey rating star support
98
+ - survey text field support
99
+
100
+ #### 0.6.4
101
+ - added some annotations
102
+ - [break] cleanup client and server errors
103
+
104
+ #### 0.6.3
105
+ - restructured FormService::RSpec (e.g. `SharkSpec.stub_contact_service`)
106
+ - form service:
107
+ - removed API v1 resources
108
+ - added FormVersion::form_structure to get a tree model of the pages form
109
+
110
+
111
+ #### 0.6.2
112
+ - [break] remove `URI.join()` from `settings.rb` in apps
113
+ - contact service:
114
+ - supports activity
115
+ - form service:
116
+ - supports API v2
117
+
118
+ #### 0.6.1
119
+ - fixes including of RSpec helper files
120
+
121
+ #### 0.6.0
122
+ - supports Contact Service: accounts, contacts and groups
123
+
124
+ #### 0.5.1
125
+ - added DateTime accessors to SurveyService
126
+
127
+ #### 0.5.0
128
+ - added create participant to SurveyService
129
+
130
+ #### 0.4.0
131
+ - initial version with SurveyService and FormService
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in bima-shark-sdk.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Huy Dinh
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,53 @@
1
+ # bima-shark-sdk
2
+
3
+ [![Build Status](https://travis-ci.com/infopark-customers/bima-shark-sdk.svg?token=E8GNUMCMv7q5uwHSaqs7&branch=develop)](https://travis-ci.com/infopark-customers/bima-shark-sdk)
4
+
5
+ ## Installation
6
+
7
+ Include **bima-shark-sdk** in your Gemfile and put the gem in the vendor/cache directory of your Rails application.
8
+
9
+ ```ruby
10
+ gem "bima-shark-sdk"
11
+ ```
12
+
13
+ ## Configuration
14
+
15
+ ```ruby
16
+ Shark.configure do |config|
17
+ config.asset_service.site = __ASSET_SERVICE_URL__
18
+
19
+ config.contact_service.site = __CONTACT_SERVICE_URL__
20
+
21
+ config.consent_service.site = __CONSENT_SERVICE_URL__
22
+
23
+ config.double_opt_in_service.site = __DOUBLE_OPT_IN_SERVICE_URL__
24
+
25
+ config.form_service.site = __FORM_SERVICE_URL__
26
+
27
+ config.subscription_service.site = __SUBSCRIPTION_SERVICE_URL__
28
+
29
+ config.survey_service.site = __MILACRM_URL__
30
+ end
31
+ ```
32
+
33
+ If you want to send emails in your application also add:
34
+
35
+ ```ruby
36
+ Shark::MailingService.use_shark_mailer do |mailer|
37
+ mailer.context_helpers = [
38
+ ActiveSupport::NumberHelper
39
+ ]
40
+ mailer.default_layout = 'system_2019'
41
+ mailer.default_template_root = File.expand_path('../../app/views', __dir__)
42
+ end
43
+ ```
44
+
45
+ ## Testing
46
+
47
+ ```
48
+ bundle exec rake spec
49
+ ```
50
+
51
+ ## Documentation
52
+
53
+ Please look for the documentation in the [wiki](https://github.com/infopark-customers/bima-shark-sdk/wiki/Home).
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
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'shark/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'bima-shark-sdk'
9
+ spec.version = Shark::VERSION
10
+ spec.authors = ['Huy Dinh', 'Johannes Schmeißer', 'Joergen Dahlke']
11
+ spec.email = ['bima-team@infopark.de']
12
+
13
+ spec.summary = ''
14
+ spec.description = ''
15
+ spec.homepage = 'https://github.com/infopark-customers/bima-shark-sdk'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|pkg)/})
19
+ end
20
+ spec.bindir = 'exe'
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ['lib']
23
+
24
+ spec.add_dependency 'activesupport', '>= 4.1.0'
25
+ spec.add_dependency 'faraday'
26
+ spec.add_dependency 'json_api_client', '>= 1.10.0'
27
+ spec.add_dependency 'net-http-persistent'
28
+ spec.add_dependency 'rack'
29
+
30
+ spec.add_development_dependency 'bundler'
31
+ spec.add_development_dependency 'pry'
32
+ spec.add_development_dependency 'rake', '~> 12.0'
33
+ spec.add_development_dependency 'rspec', '~> 3.0'
34
+ spec.add_development_dependency 'rubocop', '0.81.0'
35
+ spec.add_development_dependency 'webmock', '~> 2.3'
36
+ end
data/bin/console ADDED
@@ -0,0 +1,24 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'shark'
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
+ # TODO
15
+ Shark.configure do |config|
16
+ config.contact_service.site = 'https://contactservice-development.bundesimmo.de/api/'
17
+ config.survey_service.site = 'https://milacrm-development.bundesimmo.de/api/v1/'
18
+ config.form_service.site = 'https://formservice-development.bundesimmo.de/api/v1/'
19
+ config.notification_service.site = 'https://api-development.bundesimmo.de/notification-service/'
20
+ config.subscription_service.site = 'https://api-development.bundesimmo.de/subscription-service/'
21
+ end
22
+
23
+ require 'irb'
24
+ 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
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/all'
4
+
5
+ require File.expand_path('../shark/rspec/helpers', __dir__)
6
+
7
+ class SharkSpec
8
+ extend Shark::RSpec::Helpers
9
+
10
+ class << self
11
+ def method_missing(name, *args, &block)
12
+ if name.match(/^stub_/)
13
+ fake_service(name).setup
14
+ elsif name.match(/^unstub_/)
15
+ fake_service(name).reset
16
+ else
17
+ super
18
+ end
19
+ end
20
+
21
+ def respond_to_missing?(name, _include_private)
22
+ if name.match(/^stub_/)
23
+ true
24
+ elsif name.match(/^unstub_/)
25
+ true
26
+ else
27
+ super
28
+ end
29
+ end
30
+
31
+ protected
32
+
33
+ def fake_service(method)
34
+ service = method.to_s.gsub(/(un)?stub_/, '').camelize
35
+ Module.const_get("Shark::RSpec::Fake#{service}")
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'shark'
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Shark
4
+ class Account < Base
5
+ extend ContactService::Resource
6
+ end
7
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Shark
4
+ class Activity < Base
5
+ extend ContactService::Resource
6
+
7
+ def self.find_all_by_contact_id(contact_id)
8
+ where(contact_id: contact_id).all
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Shark
4
+ class Asset < Base
5
+ extend AssetService::Resource
6
+
7
+ custom_endpoint :recreate_variations, on: :member, request_method: :post
8
+
9
+ def self.where(_args = {})
10
+ raise Shark::ActionNotSupportedError, 'Shark::AssetService::Asset.where is not supported'
11
+ end
12
+ end
13
+ end
data/lib/shark/base.rb ADDED
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Shark
4
+ class Base < JsonApiClient::Resource
5
+ self.json_key_format = :underscored_key
6
+ self.connection_class = Shark::Client::Connection
7
+ self.connection_options = {
8
+ headers: {
9
+ 'Content-Type' => 'application/vnd.api+json',
10
+ 'Accept' => 'application/vnd.api+json',
11
+ 'X-Forwarded-Proto' => 'https'
12
+ }
13
+ }
14
+
15
+ class << self
16
+ def add_datetime_accessors(*method_names)
17
+ method_names.each do |name|
18
+ # get
19
+ define_method name do
20
+ value = super()
21
+ DateTime.parse(value) if value
22
+ end
23
+
24
+ # set
25
+ define_method "#{name}=" do |value|
26
+ if value.respond_to?(:iso8601)
27
+ super(value.iso8601(0))
28
+ else
29
+ super(value)
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,67 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Shark
4
+ module Client
5
+ class Connection
6
+ attr_reader :site, :connection_options
7
+
8
+ def initialize(options = {})
9
+ @site = options.fetch(:site)
10
+ @connection_options = {
11
+ headers: options[:headers] || {},
12
+ params: options[:params] || {}
13
+ }
14
+
15
+ @connection = Faraday.new do |faraday|
16
+ faraday.use Shark::Middleware::ComposeRequest
17
+ faraday.use Shark::Middleware::Status
18
+ faraday.use JsonApiClient::Middleware::ParseJson
19
+ faraday.adapter :net_http_persistent
20
+ end
21
+ end
22
+
23
+ def use(middleware, *args, &block); end
24
+
25
+ # @param request_action [Symbol] One of :get, :post, :put, :patch, :delete.
26
+ # @param path [String] The url path
27
+ # @param options [Hash]
28
+ # - params [Hash] The request params
29
+ # - headers [Hash] The request headers
30
+ # - body [Hash]
31
+ # @return [Faraday::Response]
32
+ # @api private
33
+ def run(request_action, path, params: {}, headers: {}, body: nil)
34
+ raise ArgumentError, 'Configuration :site cannot be nil' if site.blank?
35
+ raise ArgumentError, 'Parameter :path cannot be nil' if path.blank?
36
+
37
+ url = File.join(site, path)
38
+ request_headers = connection_options_headers.merge(headers || {})
39
+ request_params = connection_options_params.merge(params || {})
40
+
41
+ if Shark.service_token.present?
42
+ request_headers['Authorization'] = "Bearer #{Shark.service_token}"
43
+ end
44
+
45
+ @connection.send(request_action) do |request|
46
+ request.url(url)
47
+ request.headers.merge!(request_headers)
48
+ request.body = body
49
+ request.params.merge!(request_params)
50
+ end
51
+ end
52
+
53
+ # @see request
54
+ alias request run
55
+
56
+ private
57
+
58
+ def connection_options_headers
59
+ connection_options[:headers] || {}
60
+ end
61
+
62
+ def connection_options_params
63
+ connection_options[:params] || {}
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Shark
4
+ module Connected
5
+ extend ActiveSupport::Concern
6
+
7
+ delegate :connection, to: :class
8
+
9
+ included do
10
+ class_attribute :connection_options
11
+
12
+ self.connection_options = {
13
+ headers: {
14
+ 'Content-Type' => 'application/vnd.api+json',
15
+ 'Accept' => 'application/vnd.api+json',
16
+ 'X-Forwarded-Proto' => 'https'
17
+ }
18
+ }
19
+ end
20
+
21
+ module ClassMethods
22
+ # Return/build a connection object
23
+ #
24
+ # @return [Connection] The connection to the api server
25
+ # @api public
26
+ def connection(rebuild = false, &block)
27
+ _build_connection(rebuild, &block)
28
+ @connection
29
+ end
30
+
31
+ protected
32
+
33
+ # @api private
34
+ def _build_connection(rebuild = false)
35
+ return @connection unless @connection.nil? || rebuild
36
+
37
+ options = connection_options.merge(site: site)
38
+ @connection = Shark::Client::Connection.new(options).tap do |conn|
39
+ yield(conn) if block_given?
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Shark
4
+ module NormalizedEmail
5
+ extend ActiveSupport::Concern
6
+
7
+ class_methods do
8
+ def normalize_email(email)
9
+ email.to_s.downcase.strip
10
+ end
11
+ end
12
+ end
13
+ end