ynab 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (194) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +7 -0
  3. data/Gemfile.lock +44 -0
  4. data/LICENSE.md +201 -0
  5. data/README.md +57 -0
  6. data/Rakefile +31 -0
  7. data/config.json +10 -0
  8. data/docs/Account.md +16 -0
  9. data/docs/AccountResponse.md +8 -0
  10. data/docs/AccountWrapper.md +8 -0
  11. data/docs/AccountsApi.md +121 -0
  12. data/docs/AccountsResponse.md +8 -0
  13. data/docs/AccountsWrapper.md +8 -0
  14. data/docs/BudgetDetail.md +22 -0
  15. data/docs/BudgetDetailResponse.md +8 -0
  16. data/docs/BudgetDetailWrapper.md +9 -0
  17. data/docs/BudgetSummary.md +12 -0
  18. data/docs/BudgetSummaryResponse.md +8 -0
  19. data/docs/BudgetSummaryWrapper.md +8 -0
  20. data/docs/BudgetsApi.md +116 -0
  21. data/docs/BulkIdWrapper.md +8 -0
  22. data/docs/BulkIds.md +9 -0
  23. data/docs/BulkResponse.md +8 -0
  24. data/docs/BulkTransactionCreateResponse.md +8 -0
  25. data/docs/BulkTransactionIds.md +8 -0
  26. data/docs/BulkTransactions.md +8 -0
  27. data/docs/CategoriesApi.md +121 -0
  28. data/docs/CategoriesResponse.md +8 -0
  29. data/docs/Category.md +15 -0
  30. data/docs/CategoryGroup.md +10 -0
  31. data/docs/CategoryGroupWithCategories.md +11 -0
  32. data/docs/CategoryGroupsWrapper.md +8 -0
  33. data/docs/CategoryResponse.md +8 -0
  34. data/docs/CategoryWrapper.md +8 -0
  35. data/docs/CurrencyFormat.md +8 -0
  36. data/docs/DateFormat.md +8 -0
  37. data/docs/ErrorDetail.md +10 -0
  38. data/docs/ErrorResponse.md +8 -0
  39. data/docs/MonthDetail.md +12 -0
  40. data/docs/MonthDetailResponse.md +8 -0
  41. data/docs/MonthDetailWrapper.md +8 -0
  42. data/docs/MonthSummariesResponse.md +8 -0
  43. data/docs/MonthSummariesWrapper.md +8 -0
  44. data/docs/MonthSummary.md +11 -0
  45. data/docs/MonthsApi.md +121 -0
  46. data/docs/Payee.md +10 -0
  47. data/docs/PayeeLocation.md +11 -0
  48. data/docs/PayeeLocationResponse.md +8 -0
  49. data/docs/PayeeLocationWrapper.md +8 -0
  50. data/docs/PayeeLocationsApi.md +179 -0
  51. data/docs/PayeeLocationsResponse.md +8 -0
  52. data/docs/PayeeLocationsWrapper.md +8 -0
  53. data/docs/PayeeResponse.md +8 -0
  54. data/docs/PayeeWrapper.md +8 -0
  55. data/docs/PayeesApi.md +121 -0
  56. data/docs/PayeesResponse.md +8 -0
  57. data/docs/PayeesWrapper.md +8 -0
  58. data/docs/SaveTransaction.md +18 -0
  59. data/docs/SaveTransactionWrapper.md +8 -0
  60. data/docs/ScheduledSubTransaction.md +14 -0
  61. data/docs/ScheduledTransactionDetail.md +19 -0
  62. data/docs/ScheduledTransactionResponse.md +8 -0
  63. data/docs/ScheduledTransactionSummary.md +18 -0
  64. data/docs/ScheduledTransactionWrapper.md +8 -0
  65. data/docs/ScheduledTransactionsApi.md +121 -0
  66. data/docs/ScheduledTransactionsResponse.md +8 -0
  67. data/docs/ScheduledTransactionsWrapper.md +8 -0
  68. data/docs/SubTransaction.md +14 -0
  69. data/docs/TransactionDetail.md +20 -0
  70. data/docs/TransactionResponse.md +8 -0
  71. data/docs/TransactionSummary.md +19 -0
  72. data/docs/TransactionWrapper.md +8 -0
  73. data/docs/TransactionsApi.md +428 -0
  74. data/docs/TransactionsResponse.md +8 -0
  75. data/docs/TransactionsWrapper.md +8 -0
  76. data/examples/budget-list.rb +25 -0
  77. data/examples/budget-month.rb +36 -0
  78. data/examples/category-balance.rb +27 -0
  79. data/lib/ynab.rb +136 -0
  80. data/lib/ynab/api/accounts_api.rb +135 -0
  81. data/lib/ynab/api/budgets_api.rb +126 -0
  82. data/lib/ynab/api/categories_api.rb +135 -0
  83. data/lib/ynab/api/months_api.rb +135 -0
  84. data/lib/ynab/api/payee_locations_api.rb +194 -0
  85. data/lib/ynab/api/payees_api.rb +135 -0
  86. data/lib/ynab/api/scheduled_transactions_api.rb +135 -0
  87. data/lib/ynab/api/transactions_api.rb +451 -0
  88. data/lib/ynab/api_client.rb +392 -0
  89. data/lib/ynab/api_error.rb +38 -0
  90. data/lib/ynab/configuration.rb +209 -0
  91. data/lib/ynab/models/account.rb +343 -0
  92. data/lib/ynab/models/account_response.rb +193 -0
  93. data/lib/ynab/models/account_wrapper.rb +193 -0
  94. data/lib/ynab/models/accounts_response.rb +193 -0
  95. data/lib/ynab/models/accounts_wrapper.rb +195 -0
  96. data/lib/ynab/models/budget_detail.rb +345 -0
  97. data/lib/ynab/models/budget_detail_response.rb +193 -0
  98. data/lib/ynab/models/budget_detail_wrapper.rb +208 -0
  99. data/lib/ynab/models/budget_summary.rb +235 -0
  100. data/lib/ynab/models/budget_summary_response.rb +193 -0
  101. data/lib/ynab/models/budget_summary_wrapper.rb +195 -0
  102. data/lib/ynab/models/bulk_id_wrapper.rb +193 -0
  103. data/lib/ynab/models/bulk_ids.rb +213 -0
  104. data/lib/ynab/models/bulk_response.rb +193 -0
  105. data/lib/ynab/models/bulk_transaction_create_response.rb +193 -0
  106. data/lib/ynab/models/bulk_transaction_ids.rb +195 -0
  107. data/lib/ynab/models/bulk_transactions.rb +195 -0
  108. data/lib/ynab/models/categories_response.rb +193 -0
  109. data/lib/ynab/models/category.rb +295 -0
  110. data/lib/ynab/models/category_group.rb +222 -0
  111. data/lib/ynab/models/category_group_with_categories.rb +239 -0
  112. data/lib/ynab/models/category_groups_wrapper.rb +195 -0
  113. data/lib/ynab/models/category_response.rb +193 -0
  114. data/lib/ynab/models/category_wrapper.rb +193 -0
  115. data/lib/ynab/models/currency_format.rb +193 -0
  116. data/lib/ynab/models/date_format.rb +193 -0
  117. data/lib/ynab/models/error_detail.rb +221 -0
  118. data/lib/ynab/models/error_response.rb +193 -0
  119. data/lib/ynab/models/month_detail.rb +253 -0
  120. data/lib/ynab/models/month_detail_response.rb +193 -0
  121. data/lib/ynab/models/month_detail_wrapper.rb +193 -0
  122. data/lib/ynab/models/month_summaries_response.rb +193 -0
  123. data/lib/ynab/models/month_summaries_wrapper.rb +195 -0
  124. data/lib/ynab/models/month_summary.rb +236 -0
  125. data/lib/ynab/models/payee.rb +222 -0
  126. data/lib/ynab/models/payee_location.rb +235 -0
  127. data/lib/ynab/models/payee_location_response.rb +193 -0
  128. data/lib/ynab/models/payee_location_wrapper.rb +193 -0
  129. data/lib/ynab/models/payee_locations_response.rb +193 -0
  130. data/lib/ynab/models/payee_locations_wrapper.rb +195 -0
  131. data/lib/ynab/models/payee_response.rb +193 -0
  132. data/lib/ynab/models/payee_wrapper.rb +193 -0
  133. data/lib/ynab/models/payees_response.rb +193 -0
  134. data/lib/ynab/models/payees_wrapper.rb +195 -0
  135. data/lib/ynab/models/save_transaction.rb +346 -0
  136. data/lib/ynab/models/save_transaction_wrapper.rb +193 -0
  137. data/lib/ynab/models/scheduled_sub_transaction.rb +279 -0
  138. data/lib/ynab/models/scheduled_transaction_detail.rb +387 -0
  139. data/lib/ynab/models/scheduled_transaction_response.rb +193 -0
  140. data/lib/ynab/models/scheduled_transaction_summary.rb +370 -0
  141. data/lib/ynab/models/scheduled_transaction_wrapper.rb +193 -0
  142. data/lib/ynab/models/scheduled_transactions_response.rb +193 -0
  143. data/lib/ynab/models/scheduled_transactions_wrapper.rb +195 -0
  144. data/lib/ynab/models/sub_transaction.rb +279 -0
  145. data/lib/ynab/models/transaction_detail.rb +401 -0
  146. data/lib/ynab/models/transaction_response.rb +193 -0
  147. data/lib/ynab/models/transaction_summary.rb +384 -0
  148. data/lib/ynab/models/transaction_wrapper.rb +193 -0
  149. data/lib/ynab/models/transactions_response.rb +193 -0
  150. data/lib/ynab/models/transactions_wrapper.rb +195 -0
  151. data/lib/ynab/version.rb +15 -0
  152. data/pkg/ynab-0.1.0.gem +0 -0
  153. data/spec-v1-swagger.json +2121 -0
  154. data/spec/api/accounts_spec.rb +56 -0
  155. data/spec/api/budgets_spec.rb +55 -0
  156. data/spec/api/categories_spec.rb +57 -0
  157. data/spec/api/months_spec.rb +57 -0
  158. data/spec/api/payee_locations_spec.rb +57 -0
  159. data/spec/api/payees_spec.rb +56 -0
  160. data/spec/api/scheduled_transactions_spec.rb +56 -0
  161. data/spec/api/transactions_spec.rb +122 -0
  162. data/spec/fixtures/.DS_Store +0 -0
  163. data/spec/fixtures/vcr_cassettes/account.yml +46 -0
  164. data/spec/fixtures/vcr_cassettes/accounts.yml +46 -0
  165. data/spec/fixtures/vcr_cassettes/accounts_unauthorized.yml +49 -0
  166. data/spec/fixtures/vcr_cassettes/budget.yml +1413 -0
  167. data/spec/fixtures/vcr_cassettes/budgets.yml +49 -0
  168. data/spec/fixtures/vcr_cassettes/budgets_unauthorized.yml +49 -0
  169. data/spec/fixtures/vcr_cassettes/bulk_transactions.yml +46 -0
  170. data/spec/fixtures/vcr_cassettes/categories.yml +67 -0
  171. data/spec/fixtures/vcr_cassettes/categories_unauthorized.yml +49 -0
  172. data/spec/fixtures/vcr_cassettes/category.yml +46 -0
  173. data/spec/fixtures/vcr_cassettes/create_transaction.yml +46 -0
  174. data/spec/fixtures/vcr_cassettes/month.yml +59 -0
  175. data/spec/fixtures/vcr_cassettes/months.yml +47 -0
  176. data/spec/fixtures/vcr_cassettes/months_unauthorized.yml +49 -0
  177. data/spec/fixtures/vcr_cassettes/payee.yml +47 -0
  178. data/spec/fixtures/vcr_cassettes/payee_location.yml +46 -0
  179. data/spec/fixtures/vcr_cassettes/payee_locations.yml +46 -0
  180. data/spec/fixtures/vcr_cassettes/payee_locations_unauthorized.yml +49 -0
  181. data/spec/fixtures/vcr_cassettes/payees.yml +51 -0
  182. data/spec/fixtures/vcr_cassettes/payees_unauthorized.yml +49 -0
  183. data/spec/fixtures/vcr_cassettes/scheduled_transaction.yml +46 -0
  184. data/spec/fixtures/vcr_cassettes/scheduled_transactions.yml +46 -0
  185. data/spec/fixtures/vcr_cassettes/scheduled_transactions_unauthorized.yml +49 -0
  186. data/spec/fixtures/vcr_cassettes/transaction.yml +46 -0
  187. data/spec/fixtures/vcr_cassettes/transactions.yml +46 -0
  188. data/spec/fixtures/vcr_cassettes/transactions_unauthorized.yml +49 -0
  189. data/spec/fixtures/vcr_cassettes/update_transaction.yml +46 -0
  190. data/spec/spec_helper.rb +18 -0
  191. data/swagger-templates/api_client.mustache +386 -0
  192. data/swagger-templates/gemspec.mustache +37 -0
  193. data/ynab.gemspec +40 -0
  194. metadata +353 -0
@@ -0,0 +1,46 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: get
5
+ uri: http://api.localhost:3000/v1/budgets/f419ac25-6217-4175-88dc-c3136ff5f6fd/transactions/81c374ff-74ab-4d6d-8d5a-ba3ad3fa68e4
6
+ body:
7
+ encoding: US-ASCII
8
+ string: ''
9
+ headers:
10
+ User-Agent:
11
+ - api_client/ruby/0.1.0
12
+ Content-Type:
13
+ - application/json
14
+ Accept:
15
+ - application/json
16
+ Authorization:
17
+ - Bearer 9f1a2c4842b614a771aaae9220fc54ae835e298c4654dc2c9205fc1d7bd1a045
18
+ Expect:
19
+ - ''
20
+ response:
21
+ status:
22
+ code: 200
23
+ message: OK
24
+ headers:
25
+ Content-Type:
26
+ - application/json; charset=utf-8
27
+ Vary:
28
+ - Accept-Encoding, Origin
29
+ ETag:
30
+ - W/"1e46ae4f1882d274e3bc56617bede19d"
31
+ Cache-Control:
32
+ - max-age=0, private, must-revalidate
33
+ X-Request-Id:
34
+ - 1754d660-f78f-4aad-a6ec-47d834f47efd
35
+ X-Runtime:
36
+ - '0.180542'
37
+ Transfer-Encoding:
38
+ - chunked
39
+ body:
40
+ encoding: UTF-8
41
+ string: '{"data":{"transaction":{"id":"81c374ff-74ab-4d6d-8d5a-ba3ad3fa68e4","date":"2018-02-15","amount":-2000,"memo":"","cleared":"uncleared","approved":true,"flag_color":null,"account_id":"5982e895-98e5-41ca-9681-0b6de1036a1c","payee_id":"200e6eb1-02fc-4af6-be26-27c5740bb949","category_id":null,"transfer_account_id":null,"import_id":null,"subtransactions":[]}}}'
42
+ http_version: '1.1'
43
+ adapter_metadata:
44
+ effective_url: http://api.localhost:3000/v1/budgets/f419ac25-6217-4175-88dc-c3136ff5f6fd/transactions/81c374ff-74ab-4d6d-8d5a-ba3ad3fa68e4
45
+ recorded_at: Thu, 15 Feb 2018 19:00:01 GMT
46
+ recorded_with: VCR 3.0.3
@@ -0,0 +1,46 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: get
5
+ uri: http://api.localhost:3000/v1/budgets/f419ac25-6217-4175-88dc-c3136ff5f6fd/transactions
6
+ body:
7
+ encoding: US-ASCII
8
+ string: ''
9
+ headers:
10
+ User-Agent:
11
+ - api_client/ruby/0.1.0
12
+ Content-Type:
13
+ - application/json
14
+ Accept:
15
+ - application/json
16
+ Authorization:
17
+ - Bearer 9f1a2c4842b614a771aaae9220fc54ae835e298c4654dc2c9205fc1d7bd1a045
18
+ Expect:
19
+ - ''
20
+ response:
21
+ status:
22
+ code: 200
23
+ message: OK
24
+ headers:
25
+ Content-Type:
26
+ - application/json; charset=utf-8
27
+ Vary:
28
+ - Accept-Encoding, Origin
29
+ ETag:
30
+ - W/"faa32c94f0e94eebee572fe3def08d83"
31
+ Cache-Control:
32
+ - max-age=0, private, must-revalidate
33
+ X-Request-Id:
34
+ - d290c58c-2261-4564-a267-5ea1ec878fdf
35
+ X-Runtime:
36
+ - '0.197369'
37
+ Transfer-Encoding:
38
+ - chunked
39
+ body:
40
+ encoding: UTF-8
41
+ string: '{"data":{"transactions":[{"id":"1f9d45f6-8ba5-476b-8cff-209b3791bd83","date":"2018-02-15","amount":1000000,"memo":null,"cleared":"cleared","approved":true,"flag_color":null,"account_id":"5982e895-98e5-41ca-9681-0b6de1036a1c","payee_id":"2141b5d8-5b9b-4a17-be45-3f4a9ac397ec","category_id":"26513650-523e-4e7a-ac0d-f6e78d0a8a03","transfer_account_id":null,"import_id":null,"subtransactions":[]},{"id":"81c374ff-74ab-4d6d-8d5a-ba3ad3fa68e4","date":"2018-02-15","amount":-2000,"memo":"","cleared":"uncleared","approved":true,"flag_color":null,"account_id":"5982e895-98e5-41ca-9681-0b6de1036a1c","payee_id":"200e6eb1-02fc-4af6-be26-27c5740bb949","category_id":null,"transfer_account_id":null,"import_id":null,"subtransactions":[]}]}}'
42
+ http_version: '1.1'
43
+ adapter_metadata:
44
+ effective_url: http://api.localhost:3000/v1/budgets/f419ac25-6217-4175-88dc-c3136ff5f6fd/transactions
45
+ recorded_at: Thu, 15 Feb 2018 19:00:01 GMT
46
+ recorded_with: VCR 3.0.3
@@ -0,0 +1,49 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: get
5
+ uri: http://api.localhost:3000/v1/budgets/f419ac25-6217-4175-88dc-c3136ff5f6fd/transactions
6
+ body:
7
+ encoding: US-ASCII
8
+ string: ''
9
+ headers:
10
+ User-Agent:
11
+ - api_client/ruby/0.1.0
12
+ Content-Type:
13
+ - application/json
14
+ Accept:
15
+ - application/json
16
+ Authorization:
17
+ - Bearer not_valid_access_token
18
+ Expect:
19
+ - ''
20
+ response:
21
+ status:
22
+ code: 401
23
+ message: Unauthorized
24
+ headers:
25
+ Cache-Control:
26
+ - no-store
27
+ Pragma:
28
+ - no-cache
29
+ WWW-Authenticate:
30
+ - Bearer realm="Doorkeeper", error="invalid_token", error_description="The access
31
+ token is invalid"
32
+ Content-Type:
33
+ - application/json; charset=utf-8
34
+ Vary:
35
+ - Accept-Encoding, Origin
36
+ X-Request-Id:
37
+ - d1ee51ca-8277-44db-bfd4-c2bd375e28ac
38
+ X-Runtime:
39
+ - '0.183205'
40
+ Transfer-Encoding:
41
+ - chunked
42
+ body:
43
+ encoding: UTF-8
44
+ string: '{"error":{"id":"401","name":"unauthorized","detail":"Unauthorized"}}'
45
+ http_version: '1.1'
46
+ adapter_metadata:
47
+ effective_url: http://api.localhost:3000/v1/budgets/f419ac25-6217-4175-88dc-c3136ff5f6fd/transactions
48
+ recorded_at: Thu, 15 Feb 2018 19:00:01 GMT
49
+ recorded_with: VCR 3.0.3
@@ -0,0 +1,46 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: put
5
+ uri: http://api.localhost:3000/v1/budgets/f419ac25-6217-4175-88dc-c3136ff5f6fd/transactions/4cd63d34-3814-4f50-abd0-59ce05b40d91
6
+ body:
7
+ encoding: UTF-8
8
+ string: '{"transaction":{"date":"2018-01-02","account_id":"5982e895-98e5-41ca-9681-0b6de1036a1c","amount":30000}}'
9
+ headers:
10
+ User-Agent:
11
+ - api_client/ruby/0.1.0
12
+ Content-Type:
13
+ - application/json
14
+ Accept:
15
+ - application/json
16
+ Authorization:
17
+ - Bearer 9f1a2c4842b614a771aaae9220fc54ae835e298c4654dc2c9205fc1d7bd1a045
18
+ Expect:
19
+ - ''
20
+ response:
21
+ status:
22
+ code: 200
23
+ message: OK
24
+ headers:
25
+ Content-Type:
26
+ - application/json; charset=utf-8
27
+ Vary:
28
+ - Accept-Encoding, Origin
29
+ ETag:
30
+ - W/"7302aaaf3d3c80e9fadc152a3e84504d"
31
+ Cache-Control:
32
+ - max-age=0, private, must-revalidate
33
+ X-Request-Id:
34
+ - 6eedb96f-419a-4c61-8fdb-540c8f5ee47f
35
+ X-Runtime:
36
+ - '0.192178'
37
+ Transfer-Encoding:
38
+ - chunked
39
+ body:
40
+ encoding: UTF-8
41
+ string: '{"data":{"transaction":{"id":"4cd63d34-3814-4f50-abd0-59ce05b40d91","date":"2018-01-02","amount":30000,"memo":null,"cleared":"uncleared","approved":false,"flag_color":null,"account_id":"5982e895-98e5-41ca-9681-0b6de1036a1c","payee_id":null,"category_id":null,"transfer_account_id":null,"import_id":null,"subtransactions":[]}}}'
42
+ http_version: '1.1'
43
+ adapter_metadata:
44
+ effective_url: http://api.localhost:3000/v1/budgets/f419ac25-6217-4175-88dc-c3136ff5f6fd/transactions/4cd63d34-3814-4f50-abd0-59ce05b40d91
45
+ recorded_at: Thu, 15 Feb 2018 19:11:01 GMT
46
+ recorded_with: VCR 3.0.3
@@ -0,0 +1,18 @@
1
+ require 'ynab'
2
+ require 'vcr'
3
+
4
+ VCR.configure do |config|
5
+ config.allow_http_connections_when_no_cassette = true
6
+ config.cassette_library_dir = "spec/fixtures/vcr_cassettes"
7
+ config.hook_into :typhoeus
8
+ end
9
+
10
+ RSpec.configure do |config|
11
+ config.expect_with :rspec do |expectations|
12
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
13
+ end
14
+
15
+ config.mock_with :rspec do |mocks|
16
+ mocks.verify_partial_doubles = true
17
+ end
18
+ end
@@ -0,0 +1,386 @@
1
+ =begin
2
+ {{> api_info}}
3
+ =end
4
+
5
+ require 'date'
6
+ require 'json'
7
+ require 'logger'
8
+ require 'tempfile'
9
+ require 'typhoeus'
10
+ require 'uri'
11
+
12
+ module {{moduleName}}
13
+ class ApiClient
14
+ # The Configuration object holding settings to be used in the API client.
15
+ attr_accessor :config
16
+
17
+ # Defines the headers to be used in HTTP requests of all API calls by default.
18
+ #
19
+ # @return [Hash]
20
+ attr_accessor :default_headers
21
+
22
+ attr_accessor :last_request
23
+
24
+ # Initializes the ApiClient
25
+ # @option config [Configuration] Configuration for initializing the object, default to Configuration.default
26
+ def initialize(config = Configuration.default)
27
+ @config = config
28
+ @user_agent = "{{#httpUserAgent}}{{{.}}}{{/httpUserAgent}}{{^httpUserAgent}}api_client/ruby/#{VERSION}{{/httpUserAgent}}"
29
+ @default_headers = {
30
+ 'Content-Type' => "application/json",
31
+ 'User-Agent' => @user_agent
32
+ }
33
+ end
34
+
35
+ def self.default
36
+ @@default ||= ApiClient.new
37
+ end
38
+
39
+ # Call an API with given options.
40
+ #
41
+ # @return [Array<(Object, Fixnum, Hash)>] an array of 3 elements:
42
+ # the data deserialized from response body (could be nil), response status code and response headers.
43
+ def call_api(http_method, path, opts = {})
44
+ request = build_request(http_method, path, opts)
45
+ @last_request = request
46
+ response = request.run
47
+
48
+ if @config.debugging
49
+ @config.logger.debug "HTTP response body ~BEGIN~\n#{response.body}\n~END~\n"
50
+ end
51
+
52
+ unless response.success?
53
+ if response.timed_out?
54
+ fail ApiError.new('Connection timed out')
55
+ elsif response.code == 0
56
+ # Errors from libcurl will be made visible here
57
+ fail ApiError.new(:code => 0,
58
+ :message => response.return_message)
59
+ else
60
+ fail ApiError.new(:code => response.code,
61
+ :response_headers => response.headers,
62
+ :response_body => response.body),
63
+ response.status_message
64
+ end
65
+ end
66
+
67
+ if opts[:return_type]
68
+ data = deserialize(response, opts[:return_type])
69
+ else
70
+ data = nil
71
+ end
72
+ return data, response.code, response.headers
73
+ end
74
+
75
+ # Builds the HTTP request
76
+ #
77
+ # @param [String] http_method HTTP method/verb (e.g. POST)
78
+ # @param [String] path URL path (e.g. /account/new)
79
+ # @option opts [Hash] :header_params Header parameters
80
+ # @option opts [Hash] :query_params Query parameters
81
+ # @option opts [Hash] :form_params Query parameters
82
+ # @option opts [Object] :body HTTP body (JSON/XML)
83
+ # @return [Typhoeus::Request] A Typhoeus Request
84
+ def build_request(http_method, path, opts = {})
85
+ url = build_request_url(path)
86
+ http_method = http_method.to_sym.downcase
87
+
88
+ header_params = @default_headers.merge(opts[:header_params] || {})
89
+ query_params = opts[:query_params] || {}
90
+ form_params = opts[:form_params] || {}
91
+
92
+ {{#hasAuthMethods}}
93
+ update_params_for_auth! header_params, query_params, opts[:auth_names]
94
+ {{/hasAuthMethods}}
95
+
96
+ # set ssl_verifyhosts option based on @config.verify_ssl_host (true/false)
97
+ _verify_ssl_host = @config.verify_ssl_host ? 2 : 0
98
+
99
+ req_opts = {
100
+ :method => http_method,
101
+ :headers => header_params,
102
+ :params => query_params,
103
+ :params_encoding => @config.params_encoding,
104
+ :timeout => @config.timeout,
105
+ :ssl_verifypeer => @config.verify_ssl,
106
+ :ssl_verifyhost => _verify_ssl_host,
107
+ :sslcert => @config.cert_file,
108
+ :sslkey => @config.key_file,
109
+ :verbose => @config.debugging
110
+ }
111
+
112
+ # set custom cert, if provided
113
+ req_opts[:cainfo] = @config.ssl_ca_cert if @config.ssl_ca_cert
114
+
115
+ if [:post, :patch, :put, :delete].include?(http_method)
116
+ req_body = build_request_body(header_params, form_params, opts[:body])
117
+ req_opts.update :body => req_body
118
+ if @config.debugging
119
+ @config.logger.debug "HTTP request body param ~BEGIN~\n#{req_body}\n~END~\n"
120
+ end
121
+ end
122
+
123
+ request = Typhoeus::Request.new(url, req_opts)
124
+ download_file(request) if opts[:return_type] == 'File'
125
+ request
126
+ end
127
+
128
+ # Check if the given MIME is a JSON MIME.
129
+ # JSON MIME examples:
130
+ # application/json
131
+ # application/json; charset=UTF8
132
+ # APPLICATION/JSON
133
+ # */*
134
+ # @param [String] mime MIME
135
+ # @return [Boolean] True if the MIME is application/json
136
+ def json_mime?(mime)
137
+ (mime == "*/*") || !(mime =~ /Application\/.*json(?!p)(;.*)?/i).nil?
138
+ end
139
+
140
+ # Deserialize the response to the given return type.
141
+ #
142
+ # @param [Response] response HTTP response
143
+ # @param [String] return_type some examples: "User", "Array[User]", "Hash[String,Integer]"
144
+ def deserialize(response, return_type)
145
+ body = response.body
146
+
147
+ # handle file downloading - return the File instance processed in request callbacks
148
+ # note that response body is empty when the file is written in chunks in request on_body callback
149
+ return @tempfile if return_type == 'File'
150
+
151
+ return nil if body.nil? || body.empty?
152
+
153
+ # return response body directly for String return type
154
+ return body if return_type == 'String'
155
+
156
+ # ensuring a default content type
157
+ content_type = response.headers['Content-Type'] || 'application/json'
158
+
159
+ fail "Content-Type is not supported: #{content_type}" unless json_mime?(content_type)
160
+
161
+ begin
162
+ data = JSON.parse("[#{body}]", :symbolize_names => true)[0]
163
+ rescue JSON::ParserError => e
164
+ if %w(String Date DateTime).include?(return_type)
165
+ data = body
166
+ else
167
+ raise e
168
+ end
169
+ end
170
+
171
+ convert_to_type data, return_type
172
+ end
173
+
174
+ # Convert data to the given return type.
175
+ # @param [Object] data Data to be converted
176
+ # @param [String] return_type Return type
177
+ # @return [Mixed] Data in a particular type
178
+ def convert_to_type(data, return_type)
179
+ return nil if data.nil?
180
+ case return_type
181
+ when 'String'
182
+ data.to_s
183
+ when 'Integer'
184
+ data.to_i
185
+ when 'Float'
186
+ data.to_f
187
+ when 'BOOLEAN'
188
+ data == true
189
+ when 'DateTime'
190
+ # parse date time (expecting ISO 8601 format)
191
+ DateTime.parse data
192
+ when 'Date'
193
+ # parse date time (expecting ISO 8601 format)
194
+ Date.parse data
195
+ when 'Object'
196
+ # generic object (usually a Hash), return directly
197
+ data
198
+ when /\AArray<(.+)>\z/
199
+ # e.g. Array<Pet>
200
+ sub_type = $1
201
+ data.map {|item| convert_to_type(item, sub_type) }
202
+ when /\AHash\<String, (.+)\>\z/
203
+ # e.g. Hash<String, Integer>
204
+ sub_type = $1
205
+ {}.tap do |hash|
206
+ data.each {|k, v| hash[k] = convert_to_type(v, sub_type) }
207
+ end
208
+ else
209
+ # models, e.g. Pet
210
+ {{moduleName}}.const_get(return_type).new.tap do |model|
211
+ model.build_from_hash data
212
+ end
213
+ end
214
+ end
215
+
216
+ # Save response body into a file in (the defined) temporary folder, using the filename
217
+ # from the "Content-Disposition" header if provided, otherwise a random filename.
218
+ # The response body is written to the file in chunks in order to handle files which
219
+ # size is larger than maximum Ruby String or even larger than the maximum memory a Ruby
220
+ # process can use.
221
+ #
222
+ # @see Configuration#temp_folder_path
223
+ def download_file(request)
224
+ tempfile = nil
225
+ encoding = nil
226
+ request.on_headers do |response|
227
+ content_disposition = response.headers['Content-Disposition']
228
+ if content_disposition and content_disposition =~ /filename=/i
229
+ filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1]
230
+ prefix = sanitize_filename(filename)
231
+ else
232
+ prefix = 'download-'
233
+ end
234
+ prefix = prefix + '-' unless prefix.end_with?('-')
235
+ encoding = response.body.encoding
236
+ tempfile = Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding)
237
+ @tempfile = tempfile
238
+ end
239
+ request.on_body do |chunk|
240
+ chunk.force_encoding(encoding)
241
+ tempfile.write(chunk)
242
+ end
243
+ request.on_complete do |response|
244
+ tempfile.close
245
+ @config.logger.info "Temp file written to #{tempfile.path}, please copy the file to a proper folder "\
246
+ "with e.g. `FileUtils.cp(tempfile.path, '/new/file/path')` otherwise the temp file "\
247
+ "will be deleted automatically with GC. It's also recommended to delete the temp file "\
248
+ "explicitly with `tempfile.delete`"
249
+ end
250
+ end
251
+
252
+ # Sanitize filename by removing path.
253
+ # e.g. ../../sun.gif becomes sun.gif
254
+ #
255
+ # @param [String] filename the filename to be sanitized
256
+ # @return [String] the sanitized filename
257
+ def sanitize_filename(filename)
258
+ filename.gsub(/.*[\/\\]/, '')
259
+ end
260
+
261
+ def build_request_url(path)
262
+ # Add leading and trailing slashes to path
263
+ path = "/#{path}".gsub(/\/+/, '/')
264
+ URI.encode(@config.base_url + path)
265
+ end
266
+
267
+ # Builds the HTTP request body
268
+ #
269
+ # @param [Hash] header_params Header parameters
270
+ # @param [Hash] form_params Query parameters
271
+ # @param [Object] body HTTP body (JSON/XML)
272
+ # @return [String] HTTP body data in the form of string
273
+ def build_request_body(header_params, form_params, body)
274
+ # http form
275
+ if header_params['Content-Type'] == 'application/x-www-form-urlencoded' ||
276
+ header_params['Content-Type'] == 'multipart/form-data'
277
+ data = {}
278
+ form_params.each do |key, value|
279
+ case value
280
+ when ::File, ::Array, nil
281
+ # let typhoeus handle File, Array and nil parameters
282
+ data[key] = value
283
+ else
284
+ data[key] = value.to_s
285
+ end
286
+ end
287
+ elsif body
288
+ data = body.is_a?(String) ? body : body.to_json
289
+ else
290
+ data = nil
291
+ end
292
+ data
293
+ end
294
+
295
+ # Update hearder and query params based on authentication settings.
296
+ #
297
+ # @param [Hash] header_params Header parameters
298
+ # @param [Hash] query_params Query parameters
299
+ # @param [String] auth_names Authentication scheme name
300
+ def update_params_for_auth!(header_params, query_params, auth_names)
301
+ Array(auth_names).each do |auth_name|
302
+ auth_setting = @config.auth_settings[auth_name]
303
+ next unless auth_setting
304
+ case auth_setting[:in]
305
+ when 'header' then header_params[auth_setting[:key]] = auth_setting[:value]
306
+ when 'query' then query_params[auth_setting[:key]] = auth_setting[:value]
307
+ else fail ArgumentError, 'Authentication token must be in `query` of `header`'
308
+ end
309
+ end
310
+ end
311
+
312
+ # Sets user agent in HTTP header
313
+ #
314
+ # @param [String] user_agent User agent (e.g. swagger-codegen/ruby/1.0.0)
315
+ def user_agent=(user_agent)
316
+ @user_agent = user_agent
317
+ @default_headers['User-Agent'] = @user_agent
318
+ end
319
+
320
+ # Return Accept header based on an array of accepts provided.
321
+ # @param [Array] accepts array for Accept
322
+ # @return [String] the Accept header (e.g. application/json)
323
+ def select_header_accept(accepts)
324
+ return nil if accepts.nil? || accepts.empty?
325
+ # use JSON when present, otherwise use all of the provided
326
+ json_accept = accepts.find { |s| json_mime?(s) }
327
+ return json_accept || accepts.join(',')
328
+ end
329
+
330
+ # Return Content-Type header based on an array of content types provided.
331
+ # @param [Array] content_types array for Content-Type
332
+ # @return [String] the Content-Type header (e.g. application/json)
333
+ def select_header_content_type(content_types)
334
+ # use application/json by default
335
+ return 'application/json' if content_types.nil? || content_types.empty?
336
+ # use JSON when present, otherwise use the first one
337
+ json_content_type = content_types.find { |s| json_mime?(s) }
338
+ return json_content_type || content_types.first
339
+ end
340
+
341
+ # Convert object (array, hash, object, etc) to JSON string.
342
+ # @param [Object] model object to be converted into JSON string
343
+ # @return [String] JSON string representation of the object
344
+ def object_to_http_body(model)
345
+ return model if model.nil? || model.is_a?(String)
346
+ local_body = nil
347
+ if model.is_a?(Array)
348
+ local_body = model.map{|m| object_to_hash(m) }
349
+ else
350
+ local_body = object_to_hash(model)
351
+ end
352
+ local_body.to_json
353
+ end
354
+
355
+ # Convert object(non-array) to hash.
356
+ # @param [Object] obj object to be converted into JSON string
357
+ # @return [String] JSON string representation of the object
358
+ def object_to_hash(obj)
359
+ if obj.respond_to?(:to_hash)
360
+ obj.to_hash
361
+ else
362
+ obj
363
+ end
364
+ end
365
+
366
+ # Build parameter value according to the given collection format.
367
+ # @param [String] collection_format one of :csv, :ssv, :tsv, :pipes and :multi
368
+ def build_collection_param(param, collection_format)
369
+ case collection_format
370
+ when :csv
371
+ param.join(',')
372
+ when :ssv
373
+ param.join(' ')
374
+ when :tsv
375
+ param.join("\t")
376
+ when :pipes
377
+ param.join('|')
378
+ when :multi
379
+ # return the array directly as typhoeus will handle it as expected
380
+ param
381
+ else
382
+ fail "unknown collection format: #{collection_format.inspect}"
383
+ end
384
+ end
385
+ end
386
+ end