mangopay-v4 4.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (214) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +30 -0
  3. data/.rspec +4 -0
  4. data/.rubocop.yml +2 -0
  5. data/.travis.yml +13 -0
  6. data/Gemfile +2 -0
  7. data/LICENSE +20 -0
  8. data/README.md +380 -0
  9. data/bin/mangopay +9 -0
  10. data/lib/mangopay.rb +133 -0
  11. data/lib/mangopay/api/api.rb +9 -0
  12. data/lib/mangopay/api/api_methods.rb +137 -0
  13. data/lib/mangopay/api/auth_token_manager.rb +173 -0
  14. data/lib/mangopay/api/http_client.rb +325 -0
  15. data/lib/mangopay/api/service/bank_accounts.rb +146 -0
  16. data/lib/mangopay/api/service/cards.rb +152 -0
  17. data/lib/mangopay/api/service/client_wallets.rb +89 -0
  18. data/lib/mangopay/api/service/clients.rb +68 -0
  19. data/lib/mangopay/api/service/dispute_documents.rb +158 -0
  20. data/lib/mangopay/api/service/disputes.rb +187 -0
  21. data/lib/mangopay/api/service/e_money.rb +42 -0
  22. data/lib/mangopay/api/service/events.rb +47 -0
  23. data/lib/mangopay/api/service/hooks.rb +93 -0
  24. data/lib/mangopay/api/service/kyc_documents.rb +153 -0
  25. data/lib/mangopay/api/service/mandates.rb +142 -0
  26. data/lib/mangopay/api/service/oauth_tokens.rb +25 -0
  27. data/lib/mangopay/api/service/pay_ins.rb +260 -0
  28. data/lib/mangopay/api/service/pay_outs.rb +54 -0
  29. data/lib/mangopay/api/service/pre_authorizations.rb +69 -0
  30. data/lib/mangopay/api/service/refunds.rb +62 -0
  31. data/lib/mangopay/api/service/reports.rb +151 -0
  32. data/lib/mangopay/api/service/repudiations.rb +32 -0
  33. data/lib/mangopay/api/service/responses.rb +102 -0
  34. data/lib/mangopay/api/service/settlement_transfers.rb +55 -0
  35. data/lib/mangopay/api/service/transactions.rb +183 -0
  36. data/lib/mangopay/api/service/transfers.rb +54 -0
  37. data/lib/mangopay/api/service/ubo_declarations.rb +70 -0
  38. data/lib/mangopay/api/service/users.rb +163 -0
  39. data/lib/mangopay/api/service/wallets.rb +99 -0
  40. data/lib/mangopay/api/uri_provider.rb +35 -0
  41. data/lib/mangopay/common/json_tag_converter.rb +68 -0
  42. data/lib/mangopay/common/jsonifier.rb +249 -0
  43. data/lib/mangopay/common/log_provider.rb +34 -0
  44. data/lib/mangopay/common/rate_limit_interval.rb +17 -0
  45. data/lib/mangopay/common/read_only_fields.rb +27 -0
  46. data/lib/mangopay/common/response_error.rb +61 -0
  47. data/lib/mangopay/common/sort_direction.rb +15 -0
  48. data/lib/mangopay/common/sort_field.rb +13 -0
  49. data/lib/mangopay/common/template_url_options.rb +9 -0
  50. data/lib/mangopay/configuration.rb +39 -0
  51. data/lib/mangopay/environment.rb +66 -0
  52. data/lib/mangopay/model/address.rb +27 -0
  53. data/lib/mangopay/model/declared_ubo.rb +21 -0
  54. data/lib/mangopay/model/dispute_reason.rb +15 -0
  55. data/lib/mangopay/model/document_page_consult.rb +15 -0
  56. data/lib/mangopay/model/e_money.rb +18 -0
  57. data/lib/mangopay/model/entity/account/bank_account.rb +26 -0
  58. data/lib/mangopay/model/entity/account/ca_bank_account.rb +27 -0
  59. data/lib/mangopay/model/entity/account/gb_bank_account.rb +21 -0
  60. data/lib/mangopay/model/entity/account/iban_bank_account.rb +21 -0
  61. data/lib/mangopay/model/entity/account/other_bank_account.rb +24 -0
  62. data/lib/mangopay/model/entity/account/us_bank_account.rb +24 -0
  63. data/lib/mangopay/model/entity/card.rb +45 -0
  64. data/lib/mangopay/model/entity/card_registration.rb +43 -0
  65. data/lib/mangopay/model/entity/client.rb +63 -0
  66. data/lib/mangopay/model/entity/client_wallet.rb +23 -0
  67. data/lib/mangopay/model/entity/dispute.rb +50 -0
  68. data/lib/mangopay/model/entity/dispute_document.rb +29 -0
  69. data/lib/mangopay/model/entity/entity_base.rb +18 -0
  70. data/lib/mangopay/model/entity/hook.rb +26 -0
  71. data/lib/mangopay/model/entity/kyc_document.rb +28 -0
  72. data/lib/mangopay/model/entity/mandate.rb +51 -0
  73. data/lib/mangopay/model/entity/pay_in/bank_wire_direct_pay_in.rb +22 -0
  74. data/lib/mangopay/model/entity/pay_in/card_direct_pay_in.rb +33 -0
  75. data/lib/mangopay/model/entity/pay_in/card_pre_authorized_pay_in.rb +13 -0
  76. data/lib/mangopay/model/entity/pay_in/card_web_pay_in.rb +40 -0
  77. data/lib/mangopay/model/entity/pay_in/direct_debit_direct_pay_in.rb +23 -0
  78. data/lib/mangopay/model/entity/pay_in/direct_debit_web_pay_in.rb +38 -0
  79. data/lib/mangopay/model/entity/pay_in/pay_in.rb +16 -0
  80. data/lib/mangopay/model/entity/pay_out.rb +22 -0
  81. data/lib/mangopay/model/entity/pre_authorization.rb +68 -0
  82. data/lib/mangopay/model/entity/refund.rb +18 -0
  83. data/lib/mangopay/model/entity/report.rb +51 -0
  84. data/lib/mangopay/model/entity/repudiation.rb +18 -0
  85. data/lib/mangopay/model/entity/settlement_transfer.rb +17 -0
  86. data/lib/mangopay/model/entity/transaction.rb +52 -0
  87. data/lib/mangopay/model/entity/transfer.rb +13 -0
  88. data/lib/mangopay/model/entity/ubo_declaration.rb +32 -0
  89. data/lib/mangopay/model/entity/user/legal_user.rb +53 -0
  90. data/lib/mangopay/model/entity/user/natural_user.rb +50 -0
  91. data/lib/mangopay/model/entity/user/user.rb +18 -0
  92. data/lib/mangopay/model/entity/wallet.rb +28 -0
  93. data/lib/mangopay/model/enum/account_type.rb +24 -0
  94. data/lib/mangopay/model/enum/card_status.rb +15 -0
  95. data/lib/mangopay/model/enum/card_type.rb +33 -0
  96. data/lib/mangopay/model/enum/card_validity.rb +19 -0
  97. data/lib/mangopay/model/enum/country_iso.rb +759 -0
  98. data/lib/mangopay/model/enum/culture_code.rb +44 -0
  99. data/lib/mangopay/model/enum/currency_iso.rb +552 -0
  100. data/lib/mangopay/model/enum/declared_ubo_refused_reason_type.rb +16 -0
  101. data/lib/mangopay/model/enum/declared_ubo_status.rb +18 -0
  102. data/lib/mangopay/model/enum/deposit_type.rb +15 -0
  103. data/lib/mangopay/model/enum/direct_debit_type.rb +13 -0
  104. data/lib/mangopay/model/enum/dispute_doc_refused_reason_type.rb +25 -0
  105. data/lib/mangopay/model/enum/dispute_document_type.rb +23 -0
  106. data/lib/mangopay/model/enum/dispute_reason_type.rb +33 -0
  107. data/lib/mangopay/model/enum/dispute_status.rb +21 -0
  108. data/lib/mangopay/model/enum/dispute_type.rb +15 -0
  109. data/lib/mangopay/model/enum/document_status.rb +21 -0
  110. data/lib/mangopay/model/enum/download_format.rb +11 -0
  111. data/lib/mangopay/model/enum/event_type.rb +74 -0
  112. data/lib/mangopay/model/enum/funds_type.rb +15 -0
  113. data/lib/mangopay/model/enum/hook_status.rb +13 -0
  114. data/lib/mangopay/model/enum/hook_validity.rb +15 -0
  115. data/lib/mangopay/model/enum/income_range.rb +21 -0
  116. data/lib/mangopay/model/enum/kyc_doc_refused_reason_type.rb +37 -0
  117. data/lib/mangopay/model/enum/kyc_document_type.rb +19 -0
  118. data/lib/mangopay/model/enum/kyc_level.rb +13 -0
  119. data/lib/mangopay/model/enum/legal_person_type.rb +15 -0
  120. data/lib/mangopay/model/enum/mandate_culture_code.rb +23 -0
  121. data/lib/mangopay/model/enum/mandate_execution_type.rb +11 -0
  122. data/lib/mangopay/model/enum/mandate_scheme.rb +13 -0
  123. data/lib/mangopay/model/enum/mandate_status.rb +24 -0
  124. data/lib/mangopay/model/enum/mandate_type.rb +11 -0
  125. data/lib/mangopay/model/enum/natural_user_capacity.rb +15 -0
  126. data/lib/mangopay/model/enum/pay_in_execution_type.rb +18 -0
  127. data/lib/mangopay/model/enum/pay_in_payment_type.rb +21 -0
  128. data/lib/mangopay/model/enum/pay_out_payment_type.rb +11 -0
  129. data/lib/mangopay/model/enum/payment_status.rb +21 -0
  130. data/lib/mangopay/model/enum/person_type.rb +15 -0
  131. data/lib/mangopay/model/enum/platform_type.rb +23 -0
  132. data/lib/mangopay/model/enum/pre_authorization_execution_type.rb +11 -0
  133. data/lib/mangopay/model/enum/pre_authorization_status.rb +18 -0
  134. data/lib/mangopay/model/enum/refund_reason_type.rb +21 -0
  135. data/lib/mangopay/model/enum/report_status.rb +17 -0
  136. data/lib/mangopay/model/enum/report_type.rb +13 -0
  137. data/lib/mangopay/model/enum/secure_mode.rb +16 -0
  138. data/lib/mangopay/model/enum/transaction_nature.rb +17 -0
  139. data/lib/mangopay/model/enum/transaction_status.rb +15 -0
  140. data/lib/mangopay/model/enum/transaction_type.rb +15 -0
  141. data/lib/mangopay/model/enum/ubo_declaration_refused_reason_type.rb +19 -0
  142. data/lib/mangopay/model/enum/ubo_declaration_status.rb +21 -0
  143. data/lib/mangopay/model/event.rb +18 -0
  144. data/lib/mangopay/model/model.rb +194 -0
  145. data/lib/mangopay/model/money.rb +17 -0
  146. data/lib/mangopay/model/pay_in_web_extended_view.rb +31 -0
  147. data/lib/mangopay/model/refund_reason.rb +15 -0
  148. data/lib/mangopay/model/report_filter.rb +83 -0
  149. data/lib/mangopay/model/request/cancel_request.rb +16 -0
  150. data/lib/mangopay/model/request/complete_registration_request.rb +13 -0
  151. data/lib/mangopay/model/request/currency_request.rb +13 -0
  152. data/lib/mangopay/model/request/deactivation_request.rb +10 -0
  153. data/lib/mangopay/model/request/filter_request.rb +38 -0
  154. data/lib/mangopay/model/request/submit_document_request.rb +14 -0
  155. data/lib/mangopay/model/request/submit_ubo_declaration_request.rb +14 -0
  156. data/lib/mangopay/model/request/upload_file_request.rb +9 -0
  157. data/lib/mangopay/model/response_replica.rb +27 -0
  158. data/lib/mangopay/util/custom_formatter.rb +12 -0
  159. data/lib/mangopay/util/custom_logger.rb +34 -0
  160. data/lib/mangopay/util/enum.rb +52 -0
  161. data/lib/mangopay/util/file_encoder.rb +16 -0
  162. data/lib/mangopay/util/non_instantiable.rb +6 -0
  163. data/lib/mangopay/util/storage_strategy.rb +10 -0
  164. data/lib/mangopay/util/void_logger.rb +6 -0
  165. data/mangopay.gemspec +32 -0
  166. data/spec/context/address_context.rb +25 -0
  167. data/spec/context/bank_account_context.rb +138 -0
  168. data/spec/context/card_context.rb +46 -0
  169. data/spec/context/client_context.rb +45 -0
  170. data/spec/context/dispute_context.rb +37 -0
  171. data/spec/context/dispute_document_context.rb +20 -0
  172. data/spec/context/hook_context.rb +16 -0
  173. data/spec/context/kyc_document_context.rb +23 -0
  174. data/spec/context/mandate_context.rb +27 -0
  175. data/spec/context/pay_in_context.rb +225 -0
  176. data/spec/context/pay_out_context.rb +39 -0
  177. data/spec/context/pre_authorization_context.rb +41 -0
  178. data/spec/context/refund_context.rb +33 -0
  179. data/spec/context/report_context.rb +22 -0
  180. data/spec/context/repudiation_context.rb +19 -0
  181. data/spec/context/settlement_transfer_context.rb +28 -0
  182. data/spec/context/transfer_context.rb +51 -0
  183. data/spec/context/ubo_declaration_context.rb +25 -0
  184. data/spec/context/user_context.rb +83 -0
  185. data/spec/context/wallet_context.rb +53 -0
  186. data/spec/mangopay/bank_accounts_spec.rb +229 -0
  187. data/spec/mangopay/cards_spec.rb +135 -0
  188. data/spec/mangopay/client_wallets_spec.rb +148 -0
  189. data/spec/mangopay/clients_spec.rb +55 -0
  190. data/spec/mangopay/configuration_spec.rb +126 -0
  191. data/spec/mangopay/dispute_documents_spec.rb +174 -0
  192. data/spec/mangopay/disputes_spec.rb +265 -0
  193. data/spec/mangopay/e_money_spec.rb +38 -0
  194. data/spec/mangopay/events_spec.rb +50 -0
  195. data/spec/mangopay/hooks_spec.rb +71 -0
  196. data/spec/mangopay/kyc_documents_spec.rb +180 -0
  197. data/spec/mangopay/mandates_spec.rb +219 -0
  198. data/spec/mangopay/oauth_tokens_spec.rb +41 -0
  199. data/spec/mangopay/pay_ins_spec.rb +181 -0
  200. data/spec/mangopay/pay_outs_spec.rb +39 -0
  201. data/spec/mangopay/pre_authorizations_spec.rb +58 -0
  202. data/spec/mangopay/refunds_spec.rb +40 -0
  203. data/spec/mangopay/reports_spec.rb +121 -0
  204. data/spec/mangopay/responses_spec.rb +295 -0
  205. data/spec/mangopay/settlement_transfers_spec.rb +37 -0
  206. data/spec/mangopay/transactions_spec.rb +233 -0
  207. data/spec/mangopay/transfers_spec.rb +38 -0
  208. data/spec/mangopay/ubo_declarations_spec.rb +65 -0
  209. data/spec/mangopay/users_spec.rb +146 -0
  210. data/spec/mangopay/wallets_spec.rb +104 -0
  211. data/spec/resources/logo.png +0 -0
  212. data/spec/resources/test_pdf.pdf +0 -0
  213. data/spec/spec_helper.rb +72 -0
  214. metadata +349 -0
@@ -0,0 +1,99 @@
1
+ require_relative '../uri_provider'
2
+ require_relative '../../model/request/filter_request'
3
+
4
+ module MangoApi
5
+
6
+ # Provides API method delegates concerning the +Wallet+ entity.
7
+ module Wallets
8
+ class << self
9
+ include UriProvider
10
+
11
+ # Creates a new wallet entity.
12
+ #
13
+ # +Wallet+ properties:
14
+ # * Required
15
+ # * owners
16
+ # * description
17
+ # * currency
18
+ # * Optional
19
+ # * tag
20
+ #
21
+ # @param +wallet+ [Wallet] model object of wallet to be created
22
+ # @param +id_key+ [String] idempotency key for future response replication
23
+ # @return [Wallet] the newly-created Wallet entity object
24
+ def create(wallet, id_key = nil)
25
+ uri = provide_uri(:create_wallet)
26
+ response = HttpClient.post(uri, wallet, id_key)
27
+ parse response
28
+ end
29
+
30
+ # Updates the wallet entity identifiable by
31
+ # the provided wallet object's ID.
32
+ #
33
+ # +Wallet+ optional properties:
34
+ # * tag
35
+ # * description
36
+ #
37
+ # @param +wallet+ [Wallet] wallet object with corresponding ID
38
+ # and updated data
39
+ # @return [Wallet] updated wallet entity
40
+ def update(wallet)
41
+ uri = provide_uri(:update_wallet, wallet)
42
+ response = HttpClient.put(uri, wallet)
43
+ parse response
44
+ end
45
+
46
+ # Retrieves a wallet entity.
47
+ #
48
+ # @param +id+ [String] ID of the wallet to be retrieved
49
+ # @return [Wallet] the requested entity
50
+ def get(id)
51
+ uri = provide_uri(:get_wallet, id)
52
+ response = HttpClient.get(uri)
53
+ parse response
54
+ end
55
+
56
+ # Retrieves pages of wallet entities belonging to a certain user entity.
57
+ # Allows configuration of paging and sorting parameters by
58
+ # yielding a filtering object to a provided block. When no filters
59
+ # are specified, will retrieve the first page of 10 newest results.
60
+ #
61
+ # Allowed +FilterRequest+ params:
62
+ # * page
63
+ # * per_page
64
+ # * sort_field and sort_direction
65
+ #
66
+ # @param +id+ [String] ID of the user whose wallets to retrieve
67
+ # @return [Array] the requested entities
68
+ def of_user(id)
69
+ uri = provide_uri(:get_users_wallets, id)
70
+ filter_request = nil
71
+ yield filter_request = FilterRequest.new if block_given?
72
+ results = HttpClient.get(uri, filter_request)
73
+ parse_results results
74
+ end
75
+
76
+ private
77
+
78
+ # Parses an array of JSON-originating hashes into the corresponding
79
+ # Wallet entity objects.
80
+ #
81
+ # @param +results+ [Array] JSON-originating data hashes
82
+ # @return [Array] parsed Wallet entity objects
83
+ def parse_results(results)
84
+ results.collect do |entity|
85
+ parse entity
86
+ end
87
+ end
88
+
89
+ # Parses a JSON-originating hash into the corresponding
90
+ # Wallet entity object.
91
+ #
92
+ # @param +response+ [Hash] JSON-originating data hash
93
+ # @return [Wallet] corresponding Wallet entity object
94
+ def parse(response)
95
+ MangoModel::Wallet.new.dejsonify response
96
+ end
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,35 @@
1
+ require_relative 'api_methods'
2
+
3
+ module MangoApi
4
+
5
+ # Provides full contextual URL details for API calls.
6
+ module UriProvider
7
+
8
+ # Returns the URI for a given API endpoint. Calls `eval` on the
9
+ # declared API method string if the URL contains parameter placeholders,
10
+ # replacing them with data as passed into the method with the two
11
+ # 'param' params.
12
+ #
13
+ # @param [Symbol] +api_method_symbol+ API endpoint symbol key,
14
+ # as specified in ApiMethods
15
+ # @param [Object] +_param1+ First entity containing data necessary
16
+ # for parsing the URL (i.e. resource ID)
17
+ # @param [Object] +_param2+ Second entity containing necessary parsing data
18
+ #
19
+ # @return [URI] URI for the API endpoint specified by Symbol
20
+ def provide_uri(api_method_symbol,
21
+ _param1 = nil,
22
+ _param2 = nil)
23
+ _, unparsed = ApiMethods[api_method_symbol]
24
+ raise("No method declared for key :#{api_method_symbol}") unless unparsed
25
+ parsed_path = unparsed.include?('%') ? eval(unparsed) : unparsed
26
+ config = MangoPay.configuration
27
+ full_url = [config.root_url,
28
+ config.api_version,
29
+ config.client_id,
30
+ parsed_path]
31
+ .join('/')
32
+ URI(full_url)
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,68 @@
1
+ module MangoPay
2
+
3
+ # Converts field names between Ruby and API formats.
4
+ module JsonTagConverter
5
+ class << self
6
+
7
+ # Converts a snake_case-named field name to
8
+ # its API UpperCamelCase counterpart.
9
+ def to_json_tag(field)
10
+ field.split('_').collect do |word|
11
+ apply_capitalization word
12
+ end.join
13
+ end
14
+
15
+ # Applies necessary capitalization to a word
16
+ # in order to match API conventions.
17
+ def apply_capitalization(word)
18
+ word.sub!('kyc', 'KYC')
19
+ word.sub!('url', 'URL')
20
+ word.sub!('iban', 'IBAN')
21
+ word.sub!('bic', 'BIC')
22
+ word.sub!('aba', 'ABA')
23
+ word.sub!('ubo', 'UBO')
24
+ word[0] = word[0].upcase
25
+ word
26
+ end
27
+
28
+ # Converts an API-returned UpperCamelCase-named JSON tag
29
+ # to its Ruby-standard snake_case counterpart.
30
+ def from_json_tag(tag)
31
+ tag = tag.sub('UBO', 'Ubo')
32
+ parts = tag.split(/(?=[A-Z])/)
33
+ parts = compress_upcase_strings(parts)
34
+ field = ''
35
+ parts.each.with_index do |part, index|
36
+ decapitalize part
37
+ field << '_' if !field.empty? && (part.length > 1\
38
+ || (part == 'e' && parts[index + 1] == 'Money'))
39
+ field << part
40
+ end
41
+ field
42
+ end
43
+
44
+ # Takes an array of strings and sticks together those
45
+ # which are single uppercase letters in order to form
46
+ # the actual words they compose.
47
+ def compress_upcase_strings(strings)
48
+ result = []
49
+ current = ''
50
+ strings.each do |string|
51
+ if string.length > 1
52
+ result << current unless current.empty?
53
+ current = ''
54
+ result << string
55
+ next
56
+ end
57
+ current << (current.empty? ? string : string.downcase)
58
+ end
59
+ current.empty? ? result : result << current
60
+ end
61
+
62
+ def decapitalize(word)
63
+ word[0] = word[0].downcase
64
+ word
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,249 @@
1
+ require 'json'
2
+ require_relative 'read_only_fields'
3
+ require_relative '../common/json_tag_converter'
4
+ require_relative 'log_provider'
5
+ require_relative '../model/model'
6
+ require_relative '../model/entity/account/bank_account'
7
+
8
+ module MangoPay
9
+
10
+ # To be included in order to apply JSON de/serialization methods
11
+ module Jsonifier
12
+ # require here to satisfy circular dependency
13
+ require_relative '../model/entity/card_registration'
14
+ require_relative '../model/entity/pre_authorization'
15
+ require_relative '../model/entity/account/iban_bank_account'
16
+ require_relative '../model/entity/account/us_bank_account'
17
+ require_relative '../model/entity/account/ca_bank_account'
18
+ require_relative '../model/entity/account/gb_bank_account'
19
+ require_relative '../model/entity/account/other_bank_account'
20
+ require_relative '../model/entity/mandate'
21
+ require_relative '../model/entity/kyc_document'
22
+ require_relative '../model/entity/pay_out'
23
+ require_relative '../model/entity/ubo_declaration'
24
+ require_relative '../model/entity/hook'
25
+ require_relative '../model/entity/dispute'
26
+ require_relative '../model/entity/dispute_document'
27
+ require_relative '../model/entity/report'
28
+ require_relative '../model/declared_ubo'
29
+ LOG = LogProvider.provide(self)
30
+
31
+ # Serializes an object to a JSON string.
32
+ # Read-only fields will be removed
33
+ #
34
+ # @return [String] JSON string of non-read-only fields and values
35
+ def jsonify!
36
+ json = hashed_variables.to_json
37
+ # hash.to_json() sometimes adds
38
+ # unnecessary backslashes and double quotes
39
+ json.delete!('\\')
40
+ json.gsub!(/"{/, '{')
41
+ json.gsub!(/}"/, '}')
42
+ LOG.debug 'JSONIFIED {}', inspect
43
+ LOG.debug 'INTO {}', json
44
+ json
45
+ end
46
+
47
+ # De-serializes an object from a JSON-originating hash.
48
+ #
49
+ # @param +hash+ [Hash] hash converted from an API-returned JSON string
50
+ # @return [Object] corresponding object (typed)
51
+ def dejsonify(hash)
52
+ hash.each do |tag, value|
53
+ field = JsonTagConverter.from_json_tag tag
54
+ field_value = nil
55
+ if field == 'type'
56
+ if is_a? MangoModel::BankAccount
57
+ field_value = MangoModel::AccountType.value_of value
58
+ elsif is_a? MangoModel::KycDocument
59
+ field_value = MangoModel::KycDocumentType.value_of value
60
+ elsif is_a? MangoModel::DisputeDocument
61
+ field_value = MangoModel::DisputeDocumentType.value_of value
62
+ end
63
+ end
64
+ if field == 'status'
65
+ if is_a? MangoModel::CardRegistration
66
+ field_value = MangoModel::CardStatus.value_of value
67
+ elsif is_a? MangoModel::PreAuthorization
68
+ field_value = MangoModel::PreAuthorizationStatus.value_of value
69
+ elsif is_a?(MangoModel::KycDocument) || is_a?(MangoModel::DisputeDocument)
70
+ field_value = MangoModel::DocumentStatus.value_of value
71
+ elsif is_a? MangoModel::UboDeclaration
72
+ field_value = MangoModel::UboDeclarationStatus.value_of value
73
+ elsif is_a? MangoModel::DeclaredUbo
74
+ field_value = MangoModel::DeclaredUboStatus.value_of value
75
+ elsif is_a? MangoModel::Hook
76
+ field_value = MangoModel::HookStatus.value_of value
77
+ elsif is_a? MangoModel::Dispute
78
+ field_value = MangoModel::DisputeStatus.value_of value
79
+ elsif is_a? MangoModel::Report
80
+ field_value = MangoModel::ReportStatus.value_of value
81
+ end
82
+ end
83
+ if is_a? MangoModel::Mandate
84
+ if field == 'culture'
85
+ field_value = MangoModel::MandateCultureCode.value_of value
86
+ elsif field == 'status'
87
+ field_value = MangoModel::MandateStatus.value_of value
88
+ elsif field == 'execution_type'
89
+ field_value = MangoModel::MandateExecutionType.value_of value
90
+ end
91
+ end
92
+ if field == 'execution_type' && is_a?(MangoModel::PreAuthorization)
93
+ field_value = MangoModel::PreAuthorizationExecutionType.value_of value
94
+ end
95
+ if field == 'payment_type' && is_a?(MangoModel::PayOut)
96
+ field_value = MangoModel::PayOutPaymentType.value_of value
97
+ end
98
+ if field == 'refused_reason_type'
99
+ if is_a? MangoModel::DeclaredUbo
100
+ field_value = MangoModel::DeclaredUboRefusedReasonType.value_of value
101
+ elsif is_a? MangoModel::DisputeDocument
102
+ field_value = MangoModel::DisputeDocRefusedReasonType.value_of value
103
+ end
104
+ end
105
+ if field == 'validity' && is_a?(MangoModel::Hook)
106
+ field_value = MangoModel::HookValidity.value_of value
107
+ end
108
+ if field == 'date' && is_a?(MangoModel::Event)
109
+ field_value = value
110
+ end
111
+ if field == 'declared_ubos' && is_a?(MangoModel::UboDeclaration)
112
+ field_value = []
113
+ value.each do |declared_ubo|
114
+ field_value << MangoModel::DeclaredUbo.new.dejsonify(declared_ubo)
115
+ end
116
+ end
117
+ if field == 'bank_account' && is_a?(MangoModel::BankWireDirectPayIn)
118
+ field_value = case value['Type']
119
+ when MangoModel::AccountType::IBAN.to_s
120
+ MangoModel::IbanBankAccount.new.dejsonify value
121
+ when MangoModel::AccountType::US.to_s
122
+ MangoModel::UsBankAccount.new.dejsonify value
123
+ when MangoModel::AccountType::CA.to_s
124
+ MangoModel::CaBankAccount.new.dejsonify value
125
+ when MangoModel::AccountType::GB.to_s
126
+ MangoModel::GbBankAccount.new.dejsonify value
127
+ when MangoModel::AccountType::OTHER.to_s
128
+ MangoModel::OtherBankAccount.new.dejsonify value
129
+ else
130
+ raise 'Unrecognized bank account type: ' + value['Type']
131
+ end
132
+ end
133
+ unless field_value
134
+ field_value = case field
135
+ when *MangoModel.fields_of_type(MangoModel::Address)
136
+ MangoModel::Address.new.dejsonify value
137
+ when *MangoModel.fields_of_type(MangoModel::Money)
138
+ MangoModel::Money.new.dejsonify value
139
+ when *MangoModel.fields_of_type(MangoModel::RefundReason)
140
+ MangoModel::RefundReason.new.dejsonify value
141
+ when *MangoModel.fields_of_type(MangoModel::DisputeReason)
142
+ MangoModel::DisputeReason.new.dejsonify value
143
+ when *MangoModel.fields_of_type(MangoModel::PersonType)
144
+ MangoModel::PersonType.value_of value
145
+ when *MangoModel.fields_of_type(MangoModel::KycLevel)
146
+ MangoModel::KycLevel.value_of value
147
+ when *MangoModel.fields_of_type(MangoModel::LegalPersonType)
148
+ MangoModel::LegalPersonType.value_of value
149
+ when *MangoModel.fields_of_type(MangoModel::CountryIso)
150
+ MangoModel::CountryIso.value_of value
151
+ when *MangoModel.fields_of_type(MangoModel::DepositType)
152
+ MangoModel::DepositType.value_of value
153
+ when *MangoModel.fields_of_type(MangoModel::CurrencyIso)
154
+ MangoModel::CurrencyIso.value_of value
155
+ when *MangoModel.fields_of_type(MangoModel::FundsType)
156
+ MangoModel::FundsType.value_of value
157
+ when *MangoModel.fields_of_type(MangoModel::PlatformType)
158
+ MangoModel::PlatformType.value_of value
159
+ when *MangoModel.fields_of_type(MangoModel::TransactionType)
160
+ MangoModel::TransactionType.value_of value
161
+ when *MangoModel.fields_of_type(MangoModel::TransactionNature)
162
+ MangoModel::TransactionNature.value_of value
163
+ when *MangoModel.fields_of_type(MangoModel::TransactionStatus)
164
+ MangoModel::TransactionStatus.value_of value
165
+ when *MangoModel.fields_of_type(MangoModel::PayInPaymentType)
166
+ MangoModel::PayInPaymentType.value_of value
167
+ when *MangoModel.fields_of_type(MangoModel::PayInExecutionType)
168
+ MangoModel::PayInExecutionType.value_of value
169
+ when *MangoModel.fields_of_type(MangoModel::CardType)
170
+ MangoModel::CardType.value_of value
171
+ when *MangoModel.fields_of_type(MangoModel::SecureMode)
172
+ MangoModel::SecureMode.value_of value
173
+ when *MangoModel.fields_of_type(MangoModel::CultureCode)
174
+ MangoModel::CultureCode.value_of value
175
+ when *MangoModel.fields_of_type(MangoModel::CardValidity)
176
+ MangoModel::CardValidity.value_of value
177
+ when *MangoModel.fields_of_type(MangoModel::PaymentStatus)
178
+ MangoModel::PaymentStatus.value_of value
179
+ when *MangoModel.fields_of_type(MangoModel::DirectDebitType)
180
+ MangoModel::DirectDebitType.value_of value
181
+ when *MangoModel.fields_of_type(MangoModel::MandateScheme)
182
+ MangoModel::MandateScheme.value_of value
183
+ when *MangoModel.fields_of_type(MangoModel::MandateType)
184
+ MangoModel::MandateType.value_of value
185
+ when *MangoModel.fields_of_type(MangoModel::KycDocRefusedReasonType)
186
+ MangoModel::KycDocRefusedReasonType.value_of value
187
+ when *MangoModel.fields_of_type(MangoModel::NaturalUserCapacity)
188
+ MangoModel::NaturalUserCapacity.value_of value
189
+ when *MangoModel.fields_of_type(MangoModel::EventType)
190
+ MangoModel::EventType.value_of value
191
+ when *MangoModel.fields_of_type(MangoModel::RefundReasonType)
192
+ MangoModel::RefundReasonType.value_of value
193
+ when *MangoModel.fields_of_type(MangoModel::DisputeType)
194
+ MangoModel::DisputeType.value_of value
195
+ when *MangoModel.fields_of_type(MangoModel::DisputeReasonType)
196
+ MangoModel::DisputeReasonType.value_of value
197
+ when *MangoModel.fields_of_type(MangoModel::DisputeStatus)
198
+ MangoModel::DisputeStatus.value_of value
199
+ when *MangoModel.fields_of_type(MangoModel::DownloadFormat)
200
+ MangoModel::DownloadFormat.value_of value
201
+ when *MangoModel.fields_of_type(MangoModel::ReportType)
202
+ MangoModel::ReportType.value_of value
203
+ when *MangoModel.fields_of_type(DateTime)
204
+ DateTime.parse value
205
+ else
206
+ value
207
+ end
208
+ end
209
+ instance_variable_set "@#{field}", field_value
210
+ end
211
+ LOG.debug 'DE-JSONIFIED {}', hash
212
+ LOG.debug 'INTO {}', inspect
213
+ self
214
+ end
215
+
216
+ private
217
+
218
+ # Prepares a hash mapping API-standard
219
+ # instance variable names to their JSON values.
220
+ # Removes any of the fields which are considered
221
+ # read-only by the API
222
+ def hashed_variables
223
+ hash = {}
224
+ instance_variables.each do |var|
225
+ key = JsonTagConverter.to_json_tag var.to_s[1..-1]
226
+ value = json_value_for var
227
+ hash[key] = value
228
+ end
229
+ ReadOnlyFields.remove_from! hash
230
+ hash
231
+ end
232
+
233
+ # Converts the value in a specified instance variable
234
+ # to its JSON representation.
235
+ def json_value_for(var_name)
236
+ var_value = instance_variable_get var_name
237
+ begin
238
+ if var_value.singleton_class.included_modules.include? Jsonifier
239
+ var_value.jsonify!
240
+ else
241
+ var_value
242
+ end
243
+ rescue TypeError
244
+ # raised by .singleton_class() when called on primitives
245
+ var_value
246
+ end
247
+ end
248
+ end
249
+ end