noths 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (114) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile.lock +2 -2
  3. data/README.md +10 -23
  4. data/bin/generate_gem +3 -1
  5. data/bin/refresh_schemas +22 -16
  6. data/docs/AcceptanceBulkModel.md +2 -2
  7. data/docs/DispatchBulkModel.md +1 -1
  8. data/docs/Financials.md +1 -1
  9. data/docs/Order.md +12 -11
  10. data/docs/OrdersApi.md +104 -151
  11. data/docs/RefundsApi.md +8 -12
  12. data/git_push.sh +55 -0
  13. data/lib/noths.rb +1 -11
  14. data/lib/noths/api/orders_api.rb +234 -114
  15. data/lib/noths/api/refunds_api.rb +25 -15
  16. data/lib/noths/api_client.rb +3 -12
  17. data/lib/noths/api_error.rb +16 -11
  18. data/lib/noths/configuration.rb +3 -20
  19. data/lib/noths/models/acceptance_bulk_model.rb +3 -14
  20. data/lib/noths/models/currency.rb +1 -12
  21. data/lib/noths/models/decline_bulk_model.rb +1 -12
  22. data/lib/noths/models/delivery_address.rb +1 -12
  23. data/lib/noths/models/delivery_service.rb +1 -12
  24. data/lib/noths/models/delivery_zone.rb +1 -12
  25. data/lib/noths/models/dispatch_bulk_model.rb +2 -13
  26. data/lib/noths/models/dispatch_notes_bulk_model.rb +1 -12
  27. data/lib/noths/models/enquiry.rb +1 -12
  28. data/lib/noths/models/enquiry_message.rb +1 -12
  29. data/lib/noths/models/error.rb +1 -12
  30. data/lib/noths/models/financials.rb +5 -14
  31. data/lib/noths/models/financials_details.rb +1 -12
  32. data/lib/noths/models/image_url.rb +1 -12
  33. data/lib/noths/models/item.rb +1 -12
  34. data/lib/noths/models/item_financials.rb +1 -12
  35. data/lib/noths/models/link.rb +1 -12
  36. data/lib/noths/models/manual_payment.rb +1 -12
  37. data/lib/noths/models/money.rb +1 -12
  38. data/lib/noths/models/order.rb +28 -28
  39. data/lib/noths/models/order_detail.rb +1 -12
  40. data/lib/noths/models/order_details_bulk_model.rb +1 -12
  41. data/lib/noths/models/order_index.rb +1 -12
  42. data/lib/noths/models/order_refund.rb +1 -12
  43. data/lib/noths/models/product.rb +1 -12
  44. data/lib/noths/models/product_option.rb +1 -12
  45. data/lib/noths/models/refund_response.rb +1 -12
  46. data/lib/noths/models/search_result_meta.rb +1 -12
  47. data/lib/noths/models/user.rb +1 -12
  48. data/lib/noths/version.rb +2 -12
  49. data/noths.gemspec +1 -11
  50. data/schemas/api-docs.json +3 -7
  51. data/schemas/orders.json +163 -88
  52. data/schemas/refunds.json +10 -5
  53. data/spec/api/orders_api_spec.rb +53 -99
  54. data/spec/api/refunds_api_spec.rb +6 -28
  55. data/spec/api_client_spec.rb +2 -14
  56. data/spec/configuration_spec.rb +9 -25
  57. data/spec/models/acceptance_bulk_model_spec.rb +7 -38
  58. data/spec/models/currency_spec.rb +7 -38
  59. data/spec/models/decline_bulk_model_spec.rb +5 -32
  60. data/spec/models/delivery_address_spec.rb +9 -44
  61. data/spec/models/delivery_service_spec.rb +5 -32
  62. data/spec/models/delivery_zone_spec.rb +5 -32
  63. data/spec/models/dispatch_bulk_model_spec.rb +8 -41
  64. data/spec/models/dispatch_notes_bulk_model_spec.rb +4 -29
  65. data/spec/models/enquiry_message_spec.rb +4 -29
  66. data/spec/models/enquiry_spec.rb +6 -35
  67. data/spec/models/error_spec.rb +5 -32
  68. data/spec/models/financials_details_spec.rb +6 -35
  69. data/spec/models/financials_spec.rb +21 -80
  70. data/spec/models/image_url_spec.rb +9 -44
  71. data/spec/models/item_financials_spec.rb +9 -44
  72. data/spec/models/item_spec.rb +11 -50
  73. data/spec/models/link_spec.rb +6 -35
  74. data/spec/models/manual_payment_spec.rb +7 -38
  75. data/spec/models/money_spec.rb +5 -32
  76. data/spec/models/order_detail_spec.rb +5 -32
  77. data/spec/models/order_details_bulk_model_spec.rb +4 -29
  78. data/spec/models/order_index_spec.rb +6 -35
  79. data/spec/models/order_refund_spec.rb +4 -29
  80. data/spec/models/order_spec.rb +53 -164
  81. data/spec/models/product_option_spec.rb +5 -32
  82. data/spec/models/product_spec.rb +8 -41
  83. data/spec/models/refund_response_spec.rb +6 -35
  84. data/spec/models/search_result_meta_spec.rb +5 -32
  85. data/spec/models/user_spec.rb +6 -35
  86. data/spec/spec_helper.rb +1 -11
  87. data/swagger_config.json +1 -1
  88. data/templates/Gemfile.mustache +7 -0
  89. data/templates/README.mustache +127 -0
  90. data/templates/Rakefile.mustache +8 -0
  91. data/templates/api.mustache +194 -0
  92. data/templates/api_client.mustache +383 -0
  93. data/templates/api_client_spec.mustache +214 -0
  94. data/templates/api_doc.mustache +79 -0
  95. data/templates/api_error.mustache +43 -0
  96. data/templates/api_info.mustache +12 -0
  97. data/templates/api_test.mustache +29 -0
  98. data/templates/base_object.mustache +102 -0
  99. data/templates/base_object_spec.mustache +107 -0
  100. data/templates/configuration.mustache +221 -0
  101. data/templates/configuration_spec.mustache +26 -0
  102. data/templates/gem.mustache +38 -0
  103. data/templates/gemspec.mustache +41 -0
  104. data/templates/git_push.sh.mustache +55 -0
  105. data/templates/gitignore.mustache +35 -0
  106. data/templates/model.mustache +7 -0
  107. data/templates/model_doc.mustache +9 -0
  108. data/templates/model_test.mustache +19 -0
  109. data/templates/partial_model_enum_class.mustache +13 -0
  110. data/templates/partial_model_generic.mustache +275 -0
  111. data/templates/rspec.mustache +2 -0
  112. data/templates/spec_helper.mustache +101 -0
  113. data/templates/version.mustache +5 -0
  114. metadata +30 -3
@@ -0,0 +1,79 @@
1
+ # {{moduleName}}::{{classname}}{{#description}}
2
+ {{description}}{{/description}}
3
+
4
+ All URIs are relative to https://api.notonthehighstreet.com
5
+
6
+ Method | HTTP request | Description
7
+ ------------- | ------------- | -------------
8
+ {{#operations}}{{#operation}}[**{{operationId}}**]({{classname}}.md#{{operationId}}) | **{{httpMethod}}** {{path}} | {{#summary}}{{summary}}{{/summary}}
9
+ {{/operation}}{{/operations}}
10
+
11
+ {{#operations}}
12
+ {{#operation}}
13
+ # **{{operationId}}**
14
+ > {{#returnType}}{{returnType}} {{/returnType}}{{operationId}}{{#hasParams}}({{#allParams}}{{#required}}{{{paramName}}}{{#vendorExtensions.x-codegen-hasMoreRequired}}, {{/vendorExtensions.x-codegen-hasMoreRequired}}{{/required}}{{/allParams}}{{#hasOptionalParams}}{{#vendorExtensions.x-codegen-hasRequiredParams}}, {{/vendorExtensions.x-codegen-hasRequiredParams}}opts{{/hasOptionalParams}}){{/hasParams}}
15
+
16
+ {{{summary}}}{{#notes}}
17
+
18
+ {{{notes}}}{{/notes}}
19
+
20
+ ### Example
21
+ ```ruby
22
+ # load the gem
23
+ require '{{{gemName}}}'
24
+ {{#hasAuthMethods}}
25
+ # setup authorization
26
+ {{{moduleName}}}.configure do |config|{{#authMethods}}{{#isBasic}}
27
+ # Configure HTTP basic authorization: {{{name}}}
28
+ config.username = 'YOUR USERNAME'
29
+ config.password = 'YOUR PASSWORD'{{/isBasic}}{{#isApiKey}}
30
+ # Configure API key authorization: {{{name}}}
31
+ config.api_key['{{{keyParamName}}}'] = 'YOUR API KEY'
32
+ # Uncomment the following line to set a prefix for the API key, e.g. 'Bearer' (defaults to nil)
33
+ #config.api_key_prefix['{{{keyParamName}}}'] = 'Bearer'{{/isApiKey}}{{#isOAuth}}
34
+ # Configure OAuth2 access token for authorization: {{{name}}}
35
+ config.access_token = 'YOUR ACCESS TOKEN'{{/isOAuth}}
36
+ {{/authMethods}}end
37
+ {{/hasAuthMethods}}
38
+
39
+ api_instance = {{{moduleName}}}::{{{classname}}}.new{{#hasParams}}
40
+ {{#vendorExtensions.x-codegen-hasRequiredParams}}{{#allParams}}{{#required}}
41
+ {{{paramName}}} = {{{example}}} # {{{dataType}}} | {{{description}}}
42
+ {{/required}}{{/allParams}}{{/vendorExtensions.x-codegen-hasRequiredParams}}{{#hasOptionalParams}}
43
+ opts = { {{#allParams}}{{^required}}
44
+ {{{paramName}}}: {{{example}}}{{#vendorExtensions.x-codegen-hasMoreOptional}},{{/vendorExtensions.x-codegen-hasMoreOptional}} # {{{dataType}}} | {{{description}}}{{/required}}{{/allParams}}
45
+ }{{/hasOptionalParams}}{{/hasParams}}
46
+
47
+ begin
48
+ {{#summary}} #{{{.}}}
49
+ {{/summary}} {{#returnType}}result = {{/returnType}}api_instance.{{{operationId}}}{{#hasParams}}({{#allParams}}{{#required}}{{{paramName}}}{{#vendorExtensions.x-codegen-hasMoreRequired}}, {{/vendorExtensions.x-codegen-hasMoreRequired}}{{/required}}{{/allParams}}{{#hasOptionalParams}}{{#vendorExtensions.x-codegen-hasRequiredParams}}, {{/vendorExtensions.x-codegen-hasRequiredParams}}opts{{/hasOptionalParams}}){{/hasParams}}{{#returnType}}
50
+ p result{{/returnType}}
51
+ rescue {{{moduleName}}}::ApiError => e
52
+ puts "Exception when calling {{classname}}->{{{operationId}}}: #{e}"
53
+ end
54
+ ```
55
+
56
+ ### Parameters
57
+ {{^allParams}}This endpoint does not need any parameter.{{/allParams}}{{#allParams}}{{#-last}}
58
+ Name | Type | Description | Notes
59
+ ------------- | ------------- | ------------- | -------------{{/-last}}{{/allParams}}
60
+ {{#allParams}} **{{paramName}}** | {{#isPrimitiveType}}**{{dataType}}**{{/isPrimitiveType}}{{^isPrimitiveType}}{{#isFile}}**{{dataType}}**{{/isFile}}{{^isFile}}[**{{dataType}}**]({{baseType}}.md){{/isFile}}{{/isPrimitiveType}}| {{description}} | {{^required}}[optional] {{/required}}{{#defaultValue}}[default to {{defaultValue}}]{{/defaultValue}}
61
+ {{/allParams}}
62
+
63
+ ### Return type
64
+
65
+ {{#returnType}}{{#returnTypeIsPrimitive}}**{{returnType}}**{{/returnTypeIsPrimitive}}{{^returnTypeIsPrimitive}}[**{{returnType}}**]({{returnBaseType}}.md){{/returnTypeIsPrimitive}}{{/returnType}}{{^returnType}}nil (empty response body){{/returnType}}
66
+
67
+ ### Authorization
68
+
69
+ {{^authMethods}}No authorization required{{/authMethods}}{{#authMethods}}[{{name}}](../README.md#{{name}}){{^-last}}, {{/-last}}{{/authMethods}}
70
+
71
+ ### HTTP request headers
72
+
73
+ - **Content-Type**: {{#consumes}}{{{mediaType}}}{{#hasMore}}, {{/hasMore}}{{/consumes}}{{^consumes}}Not defined{{/consumes}}
74
+ - **Accept**: {{#produces}}{{{mediaType}}}{{#hasMore}}, {{/hasMore}}{{/produces}}{{^produces}}Not defined{{/produces}}
75
+
76
+
77
+
78
+ {{/operation}}
79
+ {{/operations}}
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ module {{moduleName}}
4
+ class ApiError < StandardError
5
+ attr_reader :code, :response_headers, :response_body
6
+
7
+ # Usage examples:
8
+ # ApiError.new
9
+ # ApiError.new("message")
10
+ # ApiError.new(:code => 500, :response_headers => {}, :response_body => "")
11
+ # ApiError.new(:code => 404, :message => "Not Found")
12
+ def initialize(arg = nil)
13
+ if arg.is_a? Hash
14
+ if arg.key?(:message) || arg.key?('message')
15
+ super(arg[:message] || arg['message'])
16
+ else
17
+ super arg
18
+ end
19
+
20
+ arg.each do |k, v|
21
+ instance_variable_set "@#{k}", v
22
+ end
23
+ else
24
+ super arg
25
+ end
26
+ end
27
+
28
+ def to_s
29
+ [
30
+ super,
31
+ response
32
+ ].compact.join(': ')
33
+ end
34
+
35
+ def response
36
+ return if response_headers.blank?
37
+
38
+ return response_body unless response_headers['Content-Type'].include?('application/json')
39
+
40
+ JSON.parse(response_body, symbolize_names: true)
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,12 @@
1
+ {{#appName}}
2
+ #{{{appName}}}
3
+
4
+ {{/appName}}
5
+ {{#appDescription}}
6
+ #{{{appDescription}}}
7
+
8
+ {{/appDescription}}
9
+ {{#version}}OpenAPI spec version: {{version}}{{/version}}
10
+ {{#infoEmail}}Contact: {{{infoEmail}}}{{/infoEmail}}
11
+ Generated by: https://github.com/swagger-api/swagger-codegen.git
12
+ Swagger Codegen version: {{{generatorVersion}}}
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Unit tests for {{moduleName}}::{{classname}}
4
+ # Automatically generated by swagger-codegen (github.com/swagger-api/swagger-codegen)
5
+ # Please update as you see appropriate
6
+ {{#operations}}describe '{{classname}}' do
7
+ let(:instance) { {{moduleName}}::{{classname}}.new }
8
+
9
+ describe 'test an instance of {{classname}}' do
10
+ it 'should create an instance of {{classname}}' do
11
+ expect(instance).to be_instance_of({{moduleName}}::{{classname}})
12
+ end
13
+ end
14
+
15
+ {{#operation}}
16
+ # unit tests for {{operationId}}
17
+ # {{summary}}
18
+ # {{notes}}
19
+ {{#allParams}}{{#required}} # @param {{paramName}} {{description}}
20
+ {{/required}}{{/allParams}} # @param [Hash] opts the optional parameters
21
+ {{#allParams}}{{^required}} # @option opts [{{{dataType}}}] :{{paramName}} {{description}}
22
+ {{/required}}{{/allParams}} # @return [{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}nil{{/returnType}}]
23
+ describe '{{operationId}} test' do
24
+ it 'should work'
25
+ end
26
+
27
+ {{/operation}}
28
+ end
29
+ {{/operations}}
@@ -0,0 +1,102 @@
1
+ # Builds the object from hash
2
+ # @param [Hash] attributes Model attributes in the form of hash
3
+ # @return [Object] Returns the model itself
4
+ def build_from_hash(attributes)
5
+ return nil unless attributes.is_a?(Hash)
6
+ self.class.swagger_types.each_pair do |key, type|
7
+ if type =~ /\AArray<(.*)>/i
8
+ # check to ensure the input is an array given that the the attribute
9
+ # is documented as an array but the input is not
10
+ if attributes[self.class.attribute_map[key]].is_a?(Array)
11
+ self.send("#{key}=", attributes[self.class.attribute_map[key]].map{ |v| _deserialize($1, v) } )
12
+ end
13
+ elsif !attributes[self.class.attribute_map[key]].nil?
14
+ self.send("#{key}=", _deserialize(type, attributes[self.class.attribute_map[key]]))
15
+ end # or else data not found in attributes(hash), not an issue as the data can be optional
16
+ end
17
+
18
+ self
19
+ end
20
+
21
+ # Deserializes the data based on type
22
+ # @param string type Data type
23
+ # @param string value Value to be deserialized
24
+ # @return [Object] Deserialized data
25
+ def _deserialize(type, value)
26
+ case type.to_sym
27
+ when :DateTime
28
+ DateTime.parse(value)
29
+ when :Date
30
+ Date.parse(value)
31
+ when :String
32
+ value.to_s
33
+ when :Integer
34
+ value.to_i
35
+ when :Float
36
+ value.to_f
37
+ when :BOOLEAN
38
+ if value.to_s =~ /\A(true|t|yes|y|1)\z/i
39
+ true
40
+ else
41
+ false
42
+ end
43
+ when :Object
44
+ # generic object (usually a Hash), return directly
45
+ value
46
+ when /\AArray<(?<inner_type>.+)>\z/
47
+ inner_type = Regexp.last_match[:inner_type]
48
+ value.map { |v| _deserialize(inner_type, v) }
49
+ when /\AHash<(?<k_type>.+?), (?<v_type>.+)>\z/
50
+ k_type = Regexp.last_match[:k_type]
51
+ v_type = Regexp.last_match[:v_type]
52
+ {}.tap do |hash|
53
+ value.each do |k, v|
54
+ hash[_deserialize(k_type, k)] = _deserialize(v_type, v)
55
+ end
56
+ end
57
+ else # model
58
+ temp_model = {{moduleName}}.const_get(type).new
59
+ temp_model.build_from_hash(value)
60
+ end
61
+ end
62
+
63
+ # Returns the string representation of the object
64
+ # @return [String] String presentation of the object
65
+ def to_s
66
+ to_hash.to_s
67
+ end
68
+
69
+ # to_body is an alias to to_hash (backward compatibility)
70
+ # @return [Hash] Returns the object in the form of hash
71
+ def to_body
72
+ to_hash
73
+ end
74
+
75
+ # Returns the object in the form of hash
76
+ # @return [Hash] Returns the object in the form of hash
77
+ def to_hash
78
+ hash = {}
79
+ self.class.attribute_map.each_pair do |attr, param|
80
+ value = self.send(attr)
81
+ hash[param] = _to_hash(value)
82
+ end
83
+ hash
84
+ end
85
+
86
+ # Outputs non-array value in the form of hash
87
+ # For object, use to_hash. Otherwise, just return the value
88
+ # @param [Object] value Any valid value
89
+ # @return [Hash] Returns the value in the form of hash
90
+ def _to_hash(value)
91
+ if value.is_a?(Array)
92
+ value.compact.map{ |v| _to_hash(v) }
93
+ elsif value.is_a?(Hash)
94
+ {}.tap do |hash|
95
+ value.each { |k, v| hash[k] = _to_hash(v) }
96
+ end
97
+ elsif value.respond_to? :to_hash
98
+ value.to_hash
99
+ else
100
+ value
101
+ end
102
+ end
@@ -0,0 +1,107 @@
1
+ class ArrayMapObject < Petstore::Category
2
+ attr_accessor :int_arr, :pet_arr, :int_map, :pet_map, :int_arr_map, :pet_arr_map, :boolean_true_arr, :boolean_false_arr
3
+
4
+ def self.attribute_map
5
+ {
6
+ :int_arr => :int_arr,
7
+ :pet_arr => :pet_arr,
8
+ :int_map => :int_map,
9
+ :pet_map => :pet_map,
10
+ :int_arr_map => :int_arr_map,
11
+ :pet_arr_map => :pet_arr_map,
12
+ :boolean_true_arr => :boolean_true_arr,
13
+ :boolean_false_arr => :boolean_false_arr,
14
+ }
15
+ end
16
+
17
+ def self.swagger_types
18
+ {
19
+ :int_arr => :'Array<Integer>',
20
+ :pet_arr => :'Array<Pet>',
21
+ :int_map => :'Hash<String, Integer>',
22
+ :pet_map => :'Hash<String, Pet>',
23
+ :int_arr_map => :'Hash<String, Array<Integer>>',
24
+ :pet_arr_map => :'Hash<String, Array<Pet>>',
25
+ :boolean_true_arr => :'Array<BOOLEAN>',
26
+ :boolean_false_arr => :'Array<BOOLEAN>',
27
+ }
28
+ end
29
+ end
30
+
31
+ describe 'BaseObject' do
32
+ describe 'boolean values' do
33
+ let(:obj) { Petstore::Cat.new({declawed: false}) }
34
+
35
+ it 'should have values set' do
36
+ expect(obj.declawed).not_to be_nil
37
+ expect(obj.declawed).to eq(false)
38
+ end
39
+ end
40
+
41
+ describe 'array and map properties' do
42
+ let(:obj) { ArrayMapObject.new }
43
+
44
+ let(:data) do
45
+ {int_arr: [123, 456],
46
+ pet_arr: [{name: 'Kitty'}],
47
+ int_map: {'int' => 123},
48
+ pet_map: {'pet' => {name: 'Kitty'}},
49
+ int_arr_map: {'int_arr' => [123, 456]},
50
+ pet_arr_map: {'pet_arr' => [{name: 'Kitty'}]},
51
+ boolean_true_arr: [true, "true", "TruE", 1, "y", "yes", "1", "t", "T"],
52
+ boolean_false_arr: [false, "", 0, "0", "f", nil, "null"],
53
+ }
54
+ end
55
+
56
+ it 'works for #build_from_hash' do
57
+ obj.build_from_hash(data)
58
+
59
+ expect(obj.int_arr).to match_array([123, 456])
60
+
61
+ expect(obj.pet_arr).to be_instance_of(Array)
62
+ expect(obj.pet_arr).to be_instance_of(1)
63
+
64
+ pet = obj.pet_arr.first
65
+ expect(pet).to be_instance_of(Petstore::Pet)
66
+ expect(pet.name).to eq('Kitty')
67
+
68
+ expect(obj.int_map).to be_instance_of(Hash)
69
+ expect(obj.int_map).to eq({'int' => 123})
70
+
71
+ expect(obj.pet_map).to be_instance_of(Hash)
72
+ pet = obj.pet_map['pet']
73
+ expect(pet).to be_instance_of(Petstore::Pet)
74
+ expect(pet.name).to eq('Kitty')
75
+
76
+ expect(obj.int_arr_map).to be_instance_of(Hash)
77
+ arr = obj.int_arr_map['int_arr']
78
+ expect(arr).to match_array([123, 456])
79
+
80
+ expect(obj.pet_arr_map).to be_instance_of(Hash)
81
+ arr = obj.pet_arr_map['pet_arr']
82
+ expect(arr).to be_instance_of(Array)
83
+ expect(arr.size).to eq(1)
84
+ pet = arr.first
85
+ expect(pet).to be_instance_of(Petstore::Pet)
86
+ expect(pet.name).to eq('Kitty')
87
+
88
+ expect(obj.boolean_true_arr).to be_instance_of(Array)
89
+ obj.boolean_true_arr.each do |b|
90
+ expect(b).to eq(true)
91
+ end
92
+
93
+ expect(obj.boolean_false_arr).to be_instance_of(Array)
94
+ obj.boolean_false_arr.each do |b|
95
+ expect(b).to eq(false)
96
+ end
97
+ end
98
+
99
+ it 'works for #to_hash' do
100
+ obj.build_from_hash(data)
101
+ expect_data = data.dup
102
+ expect_data[:boolean_true_arr].map! {true}
103
+ expect_data[:boolean_false_arr].map! {false}
104
+ expect(obj.to_hash).to eq(expect_data)
105
+ end
106
+ end
107
+ end
@@ -0,0 +1,221 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'uri'
4
+
5
+ module {{moduleName}}
6
+ class Configuration
7
+ # Defines url scheme
8
+ attr_accessor :scheme
9
+
10
+ # Defines url host
11
+ attr_accessor :host
12
+
13
+ # Defines url base path
14
+ attr_accessor :base_path
15
+
16
+ # Defines API keys used with API Key authentications.
17
+ #
18
+ # @return [Hash] key: parameter name, value: parameter value (API key)
19
+ #
20
+ # @example parameter name is "api_key", API key is "xxx" (e.g. "api_key=xxx" in query string)
21
+ # config.api_key['api_key'] = 'xxx'
22
+ attr_accessor :api_key
23
+
24
+ # Defines API key prefixes used with API Key authentications.
25
+ #
26
+ # @return [Hash] key: parameter name, value: API key prefix
27
+ #
28
+ # @example parameter name is "Authorization", API key prefix is "Token" (e.g. "Authorization: Token xxx" in headers)
29
+ # config.api_key_prefix['api_key'] = 'Token'
30
+ attr_accessor :api_key_prefix
31
+
32
+ # Defines the username used with HTTP basic authentication.
33
+ #
34
+ # @return [String]
35
+ attr_accessor :username
36
+
37
+ # Defines the password used with HTTP basic authentication.
38
+ #
39
+ # @return [String]
40
+ attr_accessor :password
41
+
42
+ # Defines the access token (Bearer) used with OAuth2.
43
+ attr_accessor :access_token
44
+
45
+ # Set this to enable/disable debugging. When enabled (set to true), HTTP request/response
46
+ # details will be logged with `logger.debug` (see the `logger` attribute).
47
+ # Default to false.
48
+ #
49
+ # @return [true, false]
50
+ attr_accessor :debugging
51
+
52
+ # Defines the logger used for debugging.
53
+ # Default to `Rails.logger` (when in Rails) or logging to STDOUT.
54
+ #
55
+ # @return [#debug]
56
+ attr_accessor :logger
57
+
58
+ # Defines the temporary folder to store downloaded files
59
+ # (for API endpoints that have file response).
60
+ # Default to use `Tempfile`.
61
+ #
62
+ # @return [String]
63
+ attr_accessor :temp_folder_path
64
+
65
+ # The time limit for HTTP request in seconds.
66
+ # Default to 0 (never times out).
67
+ attr_accessor :timeout
68
+
69
+ # Set this to false to skip client side validation in the operation.
70
+ # Default to true.
71
+ # @return [true, false]
72
+ attr_accessor :client_side_validation
73
+
74
+ ### TLS/SSL setting
75
+ # Set this to false to skip verifying SSL certificate when calling API from https server.
76
+ # Default to true.
77
+ #
78
+ # @note Do NOT set it to false in production code, otherwise you would face multiple types of cryptographic attacks.
79
+ #
80
+ # @return [true, false]
81
+ attr_accessor :verify_ssl
82
+
83
+ ### TLS/SSL setting
84
+ # Set this to false to skip verifying SSL host name
85
+ # Default to true.
86
+ #
87
+ # @note Do NOT set it to false in production code, otherwise you would face multiple types of cryptographic attacks.
88
+ #
89
+ # @return [true, false]
90
+ attr_accessor :verify_ssl_host
91
+
92
+ ### TLS/SSL setting
93
+ # Set this to customize the certificate file to verify the peer.
94
+ #
95
+ # @return [String] the path to the certificate file
96
+ #
97
+ # @see The `cainfo` option of Typhoeus, `--cert` option of libcurl. Related source code:
98
+ # https://github.com/typhoeus/typhoeus/blob/master/lib/typhoeus/easy_factory.rb#L145
99
+ attr_accessor :ssl_ca_cert
100
+
101
+ ### TLS/SSL setting
102
+ # Client certificate file (for client certificate)
103
+ attr_accessor :cert_file
104
+
105
+ ### TLS/SSL setting
106
+ # Client private key file (for client certificate)
107
+ attr_accessor :key_file
108
+
109
+ # Set this to customize parameters encoding of array parameter with multi collectionFormat.
110
+ # Default to nil.
111
+ #
112
+ # @see The params_encoding option of Ethon. Related source code:
113
+ # https://github.com/typhoeus/ethon/blob/master/lib/ethon/easy/queryable.rb#L96
114
+ attr_accessor :params_encoding
115
+
116
+ attr_accessor :inject_format
117
+
118
+ attr_accessor :force_ending_format
119
+
120
+ def initialize
121
+ @scheme = 'https'
122
+ @host = 'api.notonthehighstreet.com'
123
+ @base_path = '{{contextPath}}'
124
+ @api_key = {}
125
+ @api_key_prefix = {}
126
+ @timeout = 0
127
+ @client_side_validation = true
128
+ @verify_ssl = true
129
+ @verify_ssl_host = true
130
+ @params_encoding = nil
131
+ @cert_file = nil
132
+ @key_file = nil
133
+ @debugging = false
134
+ @inject_format = false
135
+ @force_ending_format = false
136
+ @logger = defined?(Rails) ? Rails.logger : Logger.new(STDOUT)
137
+
138
+ yield(self) if block_given?
139
+ end
140
+
141
+ # The default Configuration object.
142
+ def self.default
143
+ @@default ||= Configuration.new
144
+ end
145
+
146
+ def configure
147
+ yield(self) if block_given?
148
+ end
149
+
150
+ def scheme=(scheme)
151
+ # remove :// from scheme
152
+ @scheme = scheme.sub(/:\/\//, '')
153
+ end
154
+
155
+ def host=(host)
156
+ # remove http(s):// and anything after a slash
157
+ @host = host.sub(/https?:\/\//, '').split('/').first
158
+ end
159
+
160
+ def base_path=(base_path)
161
+ # Add leading and trailing slashes to base_path
162
+ @base_path = "/#{base_path}".gsub(/\/+/, '/')
163
+ @base_path = "" if @base_path == "/"
164
+ end
165
+
166
+ def base_url
167
+ url = "#{scheme}://#{[host, base_path].join('/').gsub(/\/+/, '/')}".sub(/\/+\z/, '')
168
+ URI.encode(url)
169
+ end
170
+
171
+ # Gets API key (with prefix if set).
172
+ # @param [String] param_name the parameter name of API key auth
173
+ def api_key_with_prefix(param_name)
174
+ if @api_key_prefix[param_name]
175
+ "#{@api_key_prefix[param_name]} #{@api_key[param_name]}"
176
+ else
177
+ @api_key[param_name]
178
+ end
179
+ end
180
+
181
+ # Gets Basic Auth token string
182
+ def basic_auth_token
183
+ 'Basic ' + ["#{username}:#{password}"].pack('m').delete("\r\n")
184
+ end
185
+
186
+ # Returns Auth Settings hash for api client.
187
+ def auth_settings
188
+ {
189
+ {{#authMethods}}
190
+ {{#isApiKey}}
191
+ '{{name}}' =>
192
+ {
193
+ type: 'api_key',
194
+ in: {{#isKeyInHeader}}'header'{{/isKeyInHeader}}{{#isKeyInQuery}}'query'{{/isKeyInQuery}},
195
+ key: '{{keyParamName}}',
196
+ value: api_key_with_prefix('{{keyParamName}}')
197
+ },
198
+ {{/isApiKey}}
199
+ {{#isBasic}}
200
+ '{{name}}' =>
201
+ {
202
+ type: 'basic',
203
+ in: 'header',
204
+ key: 'Authorization',
205
+ value: basic_auth_token
206
+ },
207
+ {{/isBasic}}
208
+ {{#isOAuth}}
209
+ '{{name}}' =>
210
+ {
211
+ type: 'oauth2',
212
+ in: 'header',
213
+ key: 'Authorization',
214
+ value: "Bearer #{access_token}"
215
+ },
216
+ {{/isOAuth}}
217
+ {{/authMethods}}
218
+ }
219
+ end
220
+ end
221
+ end