redox 1.8.6 → 1.8.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +12 -0
  3. data/Gemfile +13 -0
  4. data/Rakefile +2 -0
  5. data/lib/redox/authentication.rb +34 -25
  6. data/lib/redox/connection.rb +5 -3
  7. data/lib/redox/models/administration.rb +6 -4
  8. data/lib/redox/models/component.rb +5 -3
  9. data/lib/redox/models/financial.rb +5 -3
  10. data/lib/redox/models/media/notification.rb +6 -4
  11. data/lib/redox/models/media.rb +26 -26
  12. data/lib/redox/models/media_upload.rb +5 -3
  13. data/lib/redox/models/medication.rb +12 -10
  14. data/lib/redox/models/medications.rb +5 -4
  15. data/lib/redox/models/meta.rb +18 -16
  16. data/lib/redox/models/model.rb +63 -54
  17. data/lib/redox/models/note.rb +7 -5
  18. data/lib/redox/models/notes.rb +4 -4
  19. data/lib/redox/models/ordering_provider.rb +6 -4
  20. data/lib/redox/models/patient/contacts.rb +7 -5
  21. data/lib/redox/models/patient/demographics.rb +13 -11
  22. data/lib/redox/models/patient/identifier.rb +4 -2
  23. data/lib/redox/models/patient/insurance.rb +13 -11
  24. data/lib/redox/models/patient/p_c_p.rb +3 -1
  25. data/lib/redox/models/patient.rb +14 -10
  26. data/lib/redox/models/potential_matches.rb +3 -1
  27. data/lib/redox/models/provider.rb +6 -4
  28. data/lib/redox/models/scheduling.rb +5 -3
  29. data/lib/redox/models/transaction.rb +17 -15
  30. data/lib/redox/models/visit.rb +18 -15
  31. data/lib/redox/redox_exception.rb +17 -13
  32. data/lib/redox/request/financial.rb +5 -1
  33. data/lib/redox/request/media.rb +5 -1
  34. data/lib/redox/request/medications.rb +5 -1
  35. data/lib/redox/request/notes.rb +5 -1
  36. data/lib/redox/request/patient_admin.rb +10 -4
  37. data/lib/redox/request/patient_search.rb +7 -2
  38. data/lib/redox/request/provider.rb +5 -1
  39. data/lib/redox/request/request.rb +3 -1
  40. data/lib/redox/request/scheduling.rb +14 -4
  41. data/lib/redox/version.rb +3 -1
  42. data/lib/redox.rb +12 -9
  43. data/redox.gemspec +8 -9
  44. metadata +9 -98
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5742373796299f7b326bcdf0092a6cc6048ea5af88f9b99352393078fa2274c3
4
- data.tar.gz: 61af84c3bf51295755730f5e8f12db9be8290341ec41c53b42cbea26bfbc0ed3
3
+ metadata.gz: 2bc256b17ea2a9745930f73c9f2dc82dcf1168e30da66de044c504ebee800a78
4
+ data.tar.gz: 5308af92aebf6176a20cbc4915a5cd1aa2a47a023cab6426980059a0dd7178c0
5
5
  SHA512:
6
- metadata.gz: d09ec3e490dc6f82c147e2e54e68f5ad66466b7488a84ffad9eae5300cdc30e1f5260e1132b0713a8955edc1528e2228b5152035f42820bb75f004d9e465fc5d
7
- data.tar.gz: bd8b9a01d13eaccbe0a8dcfafc90de10ee1dc88b63add310b06436a1aa2d4453c8a77dc89db5e71c9737a3fee8571cb6500251f977dd680bafac5a29b5d6998e
6
+ metadata.gz: 4179504877fa5e41874f3de73537d49f9612b01360a45d96ed90d666b9b5e60964f0616419b0ce90463550e8d1dceb4e87f953cb0696b44b45a8fe55b616149c
7
+ data.tar.gz: 7ec7560d7e7087c5bcccb0a5d20bee3c57a99f309ab88bc8a9270244d2e4e9f1d266a8889edac501211dbd35213a2e23b54edeb393b5dd64fc4f2ee9443249f7
data/CHANGELOG.md CHANGED
@@ -4,6 +4,17 @@ All notable changes to this project will be documented in this file.
4
4
  The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
5
5
  and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
6
6
 
7
+ ## [1.8.7] - 2025-07-30
8
+ ### Changed
9
+ - Gemspec dependency httparty version
10
+ - Gemfile rake version
11
+ - Moved development dependencies to Gemfile
12
+ - GitHub Actions CI
13
+ - Rubocop suggestions
14
+
15
+ ### Added
16
+ - Gemfile rubocop, rubocop-minitest, rubocop-performance
17
+
7
18
  ## [1.8.6] - 2023-05-12
8
19
  ### Changed
9
20
  - Updated test JSON files for update retiring Meta.Message.ID and Meta.Transmission.ID
@@ -207,6 +218,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
207
218
  ### Added
208
219
  - Initial Release
209
220
 
221
+ [1.8.7]: https://github.com/WeInfuse/redox/compare/v1.8.6...v1.8.7
210
222
  [1.8.6]: https://github.com/WeInfuse/redox/compare/v1.8.5...v1.8.6
211
223
  [1.8.5]: https://github.com/WeInfuse/redox/compare/v1.8.4...v1.8.5
212
224
  [1.8.4]: https://github.com/WeInfuse/redox/compare/v1.8.3...v1.8.4
data/Gemfile CHANGED
@@ -1,4 +1,17 @@
1
+ # frozen_string_literal: true
2
+
1
3
  source 'https://rubygems.org'
2
4
 
3
5
  # Specify your gem's dependencies in redox.gemspec
4
6
  gemspec
7
+
8
+ gem 'bundler', '>=1', '<3'
9
+ gem 'byebug', '~> 11'
10
+ gem 'minitest', '~> 5.0'
11
+ gem 'ostruct', '~> 0.6.3'
12
+ gem 'rake', '~> 13.3'
13
+ gem 'rubocop'
14
+ gem 'rubocop-minitest'
15
+ gem 'rubocop-performance'
16
+ gem 'webmock', '~> 3.1'
17
+ gem 'yard', '~> 0.9'
data/Rakefile CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'bundler/gem_tasks'
2
4
  require 'rake/testtask'
3
5
 
@@ -1,11 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ # rubocop:disable Lint/MissingSuper
4
+ # rubocop:disable Style/ClassVars
5
+ # rubocop:disable Metrics/MethodLength
1
6
  module Redox
2
7
  class Authentication < Connection
3
8
  attr_accessor :response
4
9
 
5
- BASE_ENDPOINT = '/auth'.freeze
10
+ BASE_ENDPOINT = '/auth'
6
11
 
7
- AUTH_ENDPOINT = "#{BASE_ENDPOINT}/authenticate".freeze
8
- REFRESH_ENDPOINT = "#{BASE_ENDPOINT}/refreshToken".freeze
12
+ AUTH_ENDPOINT = "#{BASE_ENDPOINT}/authenticate"
13
+ REFRESH_ENDPOINT = "#{BASE_ENDPOINT}/refreshToken"
9
14
 
10
15
  class << self
11
16
  attr_accessor :token_expiry_padding
@@ -18,22 +23,22 @@ module Redox
18
23
  end
19
24
 
20
25
  def authenticate
21
- if (self.expires?)
22
- if (self.refresh_token)
23
- request = {
24
- body: { apiKey: Redox.configuration.api_key, refreshToken: self.refresh_token },
25
- endpoint: REFRESH_ENDPOINT
26
- }
27
- else
28
- request = {
29
- body: { apiKey: Redox.configuration.api_key, secret: Redox.configuration.secret },
30
- endpoint: AUTH_ENDPOINT
31
- }
32
- end
26
+ if expires?
27
+ request = if refresh_token
28
+ {
29
+ body: { apiKey: Redox.configuration.api_key, refreshToken: refresh_token },
30
+ endpoint: REFRESH_ENDPOINT
31
+ }
32
+ else
33
+ {
34
+ body: { apiKey: Redox.configuration.api_key, secret: Redox.configuration.secret },
35
+ endpoint: AUTH_ENDPOINT
36
+ }
37
+ end
33
38
 
34
39
  response = self.request(**request, auth: false)
35
40
 
36
- if (false == response.ok?)
41
+ if false == response.ok?
37
42
  @response = nil
38
43
  raise RedoxException.from_response(response, msg: 'Authentication')
39
44
  else
@@ -41,32 +46,33 @@ module Redox
41
46
  end
42
47
  end
43
48
 
44
- return self
49
+ self
45
50
  end
46
51
 
47
52
  def access_token
48
- return @response['accessToken'] if @response
53
+ @response['accessToken'] if @response
49
54
  end
50
55
 
51
56
  def expiry
52
- return @response['expires'] if @response
57
+ @response['expires'] if @response
53
58
  end
54
59
 
55
60
  def refresh_token
56
- return @response['refreshToken'] if @response
61
+ @response['refreshToken'] if @response
57
62
  end
58
63
 
59
64
  def expires?(seconds_from_now = Authentication.token_expiry_padding)
60
- if (self.expiry)
61
- return DateTime.strptime(self.expiry, Models::Meta::FROM_DATETIME_FORMAT).to_time.utc <= (Time.now + seconds_from_now).utc
65
+ if expiry
66
+ DateTime.strptime(expiry,
67
+ Models::Meta::FROM_DATETIME_FORMAT).to_time.utc <= (Time.now + seconds_from_now).utc
62
68
  else
63
- return true
69
+ true
64
70
  end
65
71
  end
66
72
 
67
73
  def access_header
68
- return {
69
- 'Authorization' => "Bearer #{self.access_token}",
74
+ {
75
+ 'Authorization' => "Bearer #{access_token}"
70
76
  }
71
77
  end
72
78
 
@@ -75,3 +81,6 @@ module Redox
75
81
  end
76
82
  end
77
83
  end
84
+ # rubocop:enable Metrics/MethodLength
85
+ # rubocop:enable Style/ClassVars
86
+ # rubocop:enable Lint/MissingSuper
@@ -1,10 +1,12 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Redox
2
4
  class Connection
3
- DEFAULT_ENDPOINT = '/endpoint'.freeze
5
+ DEFAULT_ENDPOINT = '/endpoint'
4
6
 
5
7
  include HTTParty
6
8
 
7
- base_uri 'https://api.redoxengine.com/'.freeze
9
+ base_uri 'https://api.redoxengine.com/'
8
10
 
9
11
  headers 'Content-Type' => 'application/json'
10
12
 
@@ -22,7 +24,7 @@ module Redox
22
24
  def auth_header
23
25
  @auth ||= Authentication.new
24
26
 
25
- return @auth.authenticate.access_header
27
+ @auth.authenticate.access_header
26
28
  end
27
29
  end
28
30
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Redox
2
4
  module Models
3
5
  class Administration < AbstractModel
@@ -6,10 +8,10 @@ module Redox
6
8
  property :StartDate, required: false, from: :start_date
7
9
  property :EndDate, required: false, from: :end_date
8
10
 
9
- alias_method :status, :Status
10
- alias_method :medication, :Medication
11
- alias_method :start_date, :StartDate
12
- alias_method :end_date, :EndDate
11
+ alias status Status
12
+ alias medication Medication
13
+ alias start_date StartDate
14
+ alias end_date EndDate
13
15
  end
14
16
  end
15
17
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Redox
2
4
  module Models
3
5
  class Component < AbstractModel
@@ -5,9 +7,9 @@ module Redox
5
7
  property :Name, required: false, from: :name
6
8
  property :Value, required: false, from: :value
7
9
 
8
- alias_method :id, :ID
9
- alias_method :name, :Name
10
- alias_method :value, :Value
10
+ alias id ID
11
+ alias name Name
12
+ alias value Value
11
13
  end
12
14
  end
13
15
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Redox
2
4
  module Models
3
5
  class Financial < AbstractModel
@@ -5,9 +7,9 @@ module Redox
5
7
  property :Patient, required: false, from: :patient, default: Redox::Models::Patient.new
6
8
  property :Transactions, required: false, from: :transactions, default: []
7
9
 
8
- alias_method :patient, :Patient
9
- alias_method :visit, :Visit
10
- alias_method :transactions, :Transactions
10
+ alias patient Patient
11
+ alias visit Visit
12
+ alias transactions Transactions
11
13
  end
12
14
  end
13
15
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Redox
2
4
  module Models
3
5
  class Notification < AbstractModel
@@ -7,10 +9,10 @@ module Redox
7
9
  property :IDType, from: :id_type
8
10
  property :LastName, required: false, from: :last_name
9
11
 
10
- alias_method :first_name, :FirstName
11
- alias_method :id_type, :IDType
12
- alias_method :id, :ID
13
- alias_method :last_name, :LastName
12
+ alias first_name FirstName
13
+ alias id_type IDType
14
+ alias id ID
15
+ alias last_name LastName
14
16
  end
15
17
  end
16
18
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Redox
2
4
  module Models
3
5
  class Media < AbstractModel
@@ -15,37 +17,35 @@ module Redox
15
17
  property :Provider, from: :provider, required: false
16
18
  property :ServiceDateTime, from: :service_date_time, required: false
17
19
 
18
- alias_method :availability, :Availability
19
- alias_method :direct_address_from, :DirectAddressFrom
20
- alias_method :direct_address_to, :DirectAddressTo
21
- alias_method :document_id, :DocumentID
22
- alias_method :document_type, :DocumentType
23
- alias_method :file_contents, :FileContents
24
- alias_method :file_name, :FileName
25
- alias_method :file_type, :FileType
26
- alias_method :provider, :Provider
27
- alias_method :service_date_time, :ServiceDateTime
28
- alias_method :notifications, :Notifications
20
+ alias availability Availability
21
+ alias direct_address_from DirectAddressFrom
22
+ alias direct_address_to DirectAddressTo
23
+ alias document_id DocumentID
24
+ alias document_type DocumentType
25
+ alias file_contents FileContents
26
+ alias file_name FileName
27
+ alias file_type FileType
28
+ alias provider Provider
29
+ alias service_date_time ServiceDateTime
30
+ alias notifications Notifications
29
31
 
30
32
  def availability=(value)
31
- case value
32
- when true
33
- self[:Availability] = 'Available'
34
- when false
35
- self[:Availability] = 'Unavailable'
36
- else
37
- self[:Availability] = value
38
- end
33
+ self[:Availability] = case value
34
+ when true
35
+ 'Available'
36
+ when false
37
+ 'Unavailable'
38
+ else
39
+ value
40
+ end
39
41
  end
40
42
 
41
43
  def add_filepath(path)
42
- if File.size(path) > BLOB_REQUIRED_SIZE
43
- raise 'Not implemented'
44
- else
45
- self.file_contents = Base64::encode64(File.read(path))
46
- self.file_type = "#{File.extname(path)}".delete_prefix('.').upcase
47
- self.file_name = File.basename(path, '.*')
48
- end
44
+ raise 'Not implemented' if File.size(path) > BLOB_REQUIRED_SIZE
45
+
46
+ self.file_contents = Base64.encode64(File.read(path))
47
+ self.file_type = File.extname(path).to_s.delete_prefix('.').upcase
48
+ self.file_name = File.basename(path, '.*')
49
49
  end
50
50
  end
51
51
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Redox
2
4
  module Models
3
5
  class MediaUpload < AbstractModel
@@ -5,9 +7,9 @@ module Redox
5
7
  property :Visit, required: false, from: :visit, default: Redox::Models::Visit.new
6
8
  property :Media, required: false, from: :media, default: Redox::Models::Media.new
7
9
 
8
- alias_method :patient, :Patient
9
- alias_method :visit, :Visit
10
- alias_method :media, :Media
10
+ alias patient Patient
11
+ alias visit Visit
12
+ alias media Media
11
13
  end
12
14
  end
13
15
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Redox
2
4
  module Models
3
5
  class Medication < AbstractModel
@@ -12,16 +14,16 @@ module Redox
12
14
  property :Indications, required: false, from: :indications, default: []
13
15
  property :AdministeringProvider, required: false, from: :administering_provider, default: {}
14
16
 
15
- alias_method :order, :Order
16
- alias_method :lot_number, :LotNumber
17
- alias_method :dose, :Dose
18
- alias_method :rate, :Rate
19
- alias_method :route, :Route
20
- alias_method :product, :Product
21
- alias_method :components, :Components
22
- alias_method :ordered_by, :OrderedBy
23
- alias_method :indications, :Indications
24
- alias_method :administering_provider, :AdministeringProvider
17
+ alias order Order
18
+ alias lot_number LotNumber
19
+ alias dose Dose
20
+ alias rate Rate
21
+ alias route Route
22
+ alias product Product
23
+ alias components Components
24
+ alias ordered_by OrderedBy
25
+ alias indications Indications
26
+ alias administering_provider AdministeringProvider
25
27
  end
26
28
  end
27
29
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Redox
2
4
  module Models
3
5
  class Medications < AbstractModel
@@ -5,14 +7,13 @@ module Redox
5
7
  property :Patient, required: false, from: :patient, default: Redox::Models::Patient.new
6
8
  property :Administrations, required: false, from: :administrations, default: []
7
9
 
8
- alias_method :patient, :Patient
9
- alias_method :visit, :Visit
10
- alias_method :administrations, :Administrations
10
+ alias patient Patient
11
+ alias visit Visit
12
+ alias administrations Administrations
11
13
 
12
14
  def create(meta: Meta.new)
13
15
  Redox::Request::Medications.administration(patient: self, meta: meta)
14
16
  end
15
-
16
17
  end
17
18
  end
18
19
  end
@@ -1,41 +1,43 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Redox
2
4
  module Models
3
5
  class Meta < Model
4
- TO_DATETIME_FORMAT = '%Y-%m-%dT%H:%M:%S.%6NZ'.freeze
5
- FROM_DATETIME_FORMAT = '%Y-%m-%dT%H:%M:%S.%N%Z'.freeze
6
+ TO_DATETIME_FORMAT = '%Y-%m-%dT%H:%M:%S.%6NZ'
7
+ FROM_DATETIME_FORMAT = '%Y-%m-%dT%H:%M:%S.%N%Z'
6
8
 
7
9
  property :DataModel, from: :data_model, required: false
8
10
  property :EventType, from: :event_type, required: false
9
- property :EventDateTime, from: :event_date_time, default: ->() { Time.now.utc.strftime(TO_DATETIME_FORMAT) }
11
+ property :EventDateTime, from: :event_date_time, default: -> { Time.now.utc.strftime(TO_DATETIME_FORMAT) }
10
12
  property :Test, from: :test, default: true
11
13
  property :Source, from: :source, required: false
12
14
  property :Destinations, from: :destinations, required: false
13
15
  property :FacilityCode, from: :facility_code, required: false
14
16
 
15
- alias_method :data_model, :DataModel
16
- alias_method :event_type, :EventType
17
- alias_method :event_date_time, :EventDateTime
18
- alias_method :test, :Test
19
- alias_method :source, :Source
20
- alias_method :destinations, :Destinations
21
- alias_method :facility_code, :FacilityCode
17
+ alias data_model DataModel
18
+ alias event_type EventType
19
+ alias event_date_time EventDateTime
20
+ alias test Test
21
+ alias source Source
22
+ alias destinations Destinations
23
+ alias facility_code FacilityCode
22
24
 
23
- def add_destination(name: , id: )
25
+ def add_destination(name:, id:)
24
26
  self[:Destinations] ||= []
25
27
  self[:Destinations] << Meta.build_subscription(name: name, id: id)
26
28
 
27
- return self
29
+ self
28
30
  end
29
31
 
30
- def set_source(name: , id: )
32
+ def set_source(name:, id:)
31
33
  self[:Source] = Meta.build_subscription(name: name, id: id)
32
34
 
33
- return self
35
+ self
34
36
  end
35
37
 
36
38
  class << self
37
- def build_subscription(name: , id:)
38
- return {
39
+ def build_subscription(name:, id:)
40
+ {
39
41
  'ID' => id,
40
42
  'Name' => name
41
43
  }
@@ -1,10 +1,12 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Redox
2
4
  module Models
3
- def self.format_datetime(d)
4
- if d.respond_to?(:strftime)
5
- d.strftime(Redox::Models::Meta::TO_DATETIME_FORMAT)
5
+ def self.format_datetime(date)
6
+ if date.respond_to?(:strftime)
7
+ date.strftime(Redox::Models::Meta::TO_DATETIME_FORMAT)
6
8
  else
7
- d
9
+ date
8
10
  end
9
11
  end
10
12
 
@@ -12,7 +14,7 @@ module Redox
12
14
  include Hashie::Extensions::IgnoreUndeclared
13
15
  include Hashie::Extensions::IndifferentAccess
14
16
 
15
- HIGH_LEVEL_KEYS = %w[Meta Patient Visit PotentialMatches]
17
+ HIGH_LEVEL_KEYS = %w[Meta Patient Visit PotentialMatches].freeze
16
18
 
17
19
  property :Meta, from: :meta, required: false
18
20
  property :Patient, from: :patient, required: false
@@ -21,72 +23,78 @@ module Redox
21
23
  property :Extensions, from: :extensions, required: false
22
24
  property :response, required: false
23
25
 
24
- alias_method :potential_matches, :PotentialMatches
25
- alias_method :patient, :Patient
26
- alias_method :visit, :Visit
27
- alias_method :meta, :Meta
26
+ alias potential_matches PotentialMatches
27
+ alias patient Patient
28
+ alias visit Visit
29
+ alias meta Meta
28
30
 
29
- def to_json(args = {})
30
- return self.to_h.to_json
31
+ def to_json(_args = {})
32
+ to_h.to_json
31
33
  end
32
34
 
33
35
  def insurances
34
- (self.patient&.insurances || []) + (self.visit&.insurances || [])
36
+ (patient&.insurances || []) + (visit&.insurances || [])
35
37
  end
36
38
 
37
39
  def self.from_response(response)
38
40
  model = Model.new
39
41
  model.response = response
40
42
 
43
+ # rubocop:disable Lint/SuppressedException
41
44
  HIGH_LEVEL_KEYS.each do |k|
42
- begin
43
- model.send("#{k}=", Module.const_get("Redox::Models::#{k}").new(response[k])) if response[k]
44
- rescue
45
- end
45
+ model.send("#{k}=", Module.const_get("Redox::Models::#{k}").new(response[k])) if response[k]
46
+ rescue StandardError
46
47
  end
48
+ # rubocop:enable Lint/SuppressedException
47
49
 
48
- return model
50
+ model
49
51
  end
50
52
 
51
53
  def self.from_response_inflected(response)
52
- model = self.from_response(response)
53
-
54
- if (model.response.ok?)
55
- data = model.response.parsed_response
56
-
57
- if data.respond_to?(:keys)
58
- model_class = nil
59
-
60
- if model.meta&.data_model
61
- model_class = "Redox::Models::#{model.meta.data_model}"
62
-
63
- begin
64
- model_class = Object.const_get(model_class)
65
- rescue NameError
66
- model_class = nil
67
- end
68
- end
69
-
70
- data.keys.each do |key|
71
- next if HIGH_LEVEL_KEYS.include?(key.to_s)
72
-
73
- helper_name = key.to_s.downcase.to_sym
74
-
75
- if model_class.nil?
76
- model.define_singleton_method(helper_name) { data[key] }
77
- else
78
- if data[key].is_a?(Array)
79
- model.define_singleton_method(helper_name) { data[key].map {|obj| model_class.new(obj) } }
80
- else
81
- model.define_singleton_method(helper_name) { model_class.new(data[key]) }
82
- end
83
- end
84
- end
54
+ model = from_response(response)
55
+
56
+ return model unless model.response.ok?
57
+
58
+ data = model.response.parsed_response
59
+
60
+ return model unless data.respond_to?(:keys)
61
+
62
+ add_helpers(model, data)
63
+ end
64
+
65
+ def self.get_inflected_class(data_model)
66
+ return if data_model.nil?
67
+
68
+ model_class = "Redox::Models::#{data_model}"
69
+
70
+ begin
71
+ Object.const_get(model_class)
72
+ rescue NameError
73
+ nil
74
+ end
75
+ end
76
+
77
+ # rubocop:disable Metrics/AbcSize
78
+ def self.add_helpers(model, data)
79
+ model_class = get_inflected_class(model.meta&.data_model)
80
+
81
+ data.each_key do |key|
82
+ next if HIGH_LEVEL_KEYS.include?(key.to_s)
83
+
84
+ helper_name = key.to_s.downcase.to_sym
85
+
86
+ if model_class.nil?
87
+ model.define_singleton_method(helper_name) { data[key] }
88
+ elsif data[key].is_a?(Array)
89
+ model.define_singleton_method(helper_name) { data[key].map { |obj| model_class.new(obj) } }
90
+ else
91
+ model.define_singleton_method(helper_name) { model_class.new(data[key]) }
85
92
  end
86
93
  end
87
94
 
88
- return model
95
+ model
89
96
  end
97
+ # rubocop:enable Metrics/AbcSize
90
98
  end
91
99
 
92
100
  class Model < AbstractModel
@@ -99,16 +107,17 @@ module Redox
99
107
  end
100
108
  end
101
109
 
102
- super(data)
110
+ super
103
111
  end
104
112
 
105
113
  def to_h
106
- return { key => super.to_h }
114
+ { key => super.to_h }
107
115
  end
108
116
 
109
117
  private
118
+
110
119
  def key
111
- return self.class.to_s.split('::').last.to_s
120
+ self.class.to_s.split('::').last.to_s
112
121
  end
113
122
  end
114
123
  end