md-notes-resource 1.0

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.
data/lib/md_notes.rb ADDED
@@ -0,0 +1,45 @@
1
+ # md_notes
2
+ #
3
+ # This file was automatically generated by APIMATIC v2.0
4
+ # ( https://apimatic.io ).
5
+
6
+ require 'date'
7
+ require 'json'
8
+ require 'faraday'
9
+ require 'certifi'
10
+ require 'logging'
11
+
12
+ require_relative 'md_notes/api_helper.rb'
13
+ require_relative 'md_notes/client.rb'
14
+
15
+ # Utilities
16
+ require_relative 'md_notes/utilities/file_wrapper.rb'
17
+
18
+ # Http
19
+ require_relative 'md_notes/http/http_call_back.rb'
20
+ require_relative 'md_notes/http/http_client.rb'
21
+ require_relative 'md_notes/http/faraday_client.rb'
22
+ require_relative 'md_notes/http/http_method_enum.rb'
23
+ require_relative 'md_notes/http/http_request.rb'
24
+ require_relative 'md_notes/http/http_response.rb'
25
+ require_relative 'md_notes/http/auth/o_auth2.rb'
26
+
27
+ # Models
28
+ require_relative 'md_notes/models/base_model.rb'
29
+ require_relative 'md_notes/models/note.rb'
30
+ require_relative 'md_notes/models/user.rb'
31
+ require_relative 'md_notes/models/service_status.rb'
32
+ require_relative 'md_notes/models/o_auth_token.rb'
33
+ require_relative 'md_notes/models/o_auth_provider_error_enum.rb'
34
+
35
+ # Exceptions
36
+ require_relative 'md_notes/exceptions/api_exception.rb'
37
+ require_relative 'md_notes/exceptions/o_auth_provider_exception.rb'
38
+
39
+ require_relative 'md_notes/configuration.rb'
40
+
41
+ # Controllers
42
+ require_relative 'md_notes/controllers/base_controller.rb'
43
+ require_relative 'md_notes/controllers/service_controller.rb'
44
+ require_relative 'md_notes/controllers/user_controller.rb'
45
+ require_relative 'md_notes/controllers/o_auth_authorization_controller.rb'
@@ -0,0 +1,289 @@
1
+ # md_notes
2
+ #
3
+ # This file was automatically generated by APIMATIC v2.0
4
+ # ( https://apimatic.io ).
5
+
6
+ module MdNotes
7
+ # API utility class
8
+ class APIHelper
9
+ # Serializes an array parameter (creates key value pairs).
10
+ # @param [String] The name of the parameter.
11
+ # @param [Array] The value of the parameter.
12
+ # @param [String] The format of the serialization.
13
+ def self.serialize_array(key, array, formatting: 'indexed')
14
+ tuples = []
15
+
16
+ if formatting == 'unindexed'
17
+ tuples += array.map { |element| ["#{key}[]", element] }
18
+ elsif formatting == 'indexed'
19
+ tuples += array.map.with_index do |element, index|
20
+ ["#{key}[#{index}]", element]
21
+ end
22
+ elsif formatting == 'plain'
23
+ tuples += array.map { |element| [key, element] }
24
+ else
25
+ raise ArgumentError, 'Invalid format provided.'
26
+ end
27
+ tuples
28
+ end
29
+
30
+ # Replaces template parameters in the given url.
31
+ # @param [String] The query string builder to replace the template
32
+ # parameters.
33
+ # @param [Hash] The parameters to replace in the url.
34
+ def self.append_url_with_template_parameters(query_builder, parameters)
35
+ # perform parameter validation
36
+ unless query_builder.instance_of? String
37
+ raise ArgumentError, 'Given value for parameter \"query_builder\" is
38
+ invalid.'
39
+ end
40
+
41
+ # Return if there are no parameters to replace.
42
+ return query_builder if parameters.nil?
43
+
44
+ parameters.each do |key, val|
45
+ if val.nil?
46
+ replace_value = ''
47
+ elsif val['value'].instance_of? Array
48
+ if val['encode'] == true
49
+ val['value'].map! { |element| CGI.escape(element.to_s) }
50
+ else
51
+ val['value'].map!(&:to_s)
52
+ end
53
+ replace_value = val['value'].join('/')
54
+ else
55
+ replace_value = if val['encode'] == true
56
+ CGI.escape(val['value'].to_s)
57
+ else
58
+ val['value'].to_s
59
+ end
60
+ end
61
+
62
+ # Find the template parameter and replace it with its value.
63
+ query_builder = query_builder.gsub('{' + key.to_s + '}', replace_value)
64
+ end
65
+ query_builder
66
+ end
67
+
68
+ # Appends the given set of parameters to the given query string.
69
+ # @param [String] The query string builder to add the query parameters to.
70
+ # @param [Hash] The parameters to append.
71
+ def self.append_url_with_query_parameters(query_builder, parameters)
72
+ # Perform parameter validation.
73
+ unless query_builder.instance_of? String
74
+ raise ArgumentError, 'Given value for parameter \"query_builder\"
75
+ is invalid.'
76
+ end
77
+
78
+ # Return if there are no parameters to replace.
79
+ return query_builder if parameters.nil?
80
+
81
+ array_serialization = 'indexed'
82
+
83
+ parameters.each do |key, value|
84
+ seperator = query_builder.include?('?') ? '&' : '?'
85
+ unless value.nil?
86
+ if value.instance_of? Array
87
+ value.compact!
88
+ query_builder += if array_serialization == 'csv'
89
+ "#{seperator}#{key}=#{value.map do |element|
90
+ CGI.escape(element.to_s)
91
+ end.join(',')}"
92
+ elsif array_serialization == 'psv'
93
+ "#{seperator}#{key}=#{value.map do |element|
94
+ CGI.escape(element.to_s)
95
+ end.join('|')}"
96
+ elsif array_serialization == 'tsv'
97
+ "#{seperator}#{key}=#{value.map do |element|
98
+ CGI.escape(element.to_s)
99
+ end.join("\t")}"
100
+ else
101
+ "#{seperator}#{APIHelper.serialize_array(
102
+ key, value, formatting: array_serialization
103
+ ).map { |k, v| "#{k}=#{CGI.escape(v.to_s)}" }
104
+ .join('&')}"
105
+ end
106
+ else
107
+ query_builder += "#{seperator}#{key}=#{CGI.escape(value.to_s)}"
108
+ end
109
+ end
110
+ end
111
+ query_builder
112
+ end
113
+
114
+ # Validates and processes the given Url.
115
+ # @param [String] The given Url to process.
116
+ # @return [String] Pre-processed Url as string.
117
+ def self.clean_url(url)
118
+ # Perform parameter validation.
119
+ raise ArgumentError, 'Invalid Url.' unless url.instance_of? String
120
+
121
+ # Ensure that the urls are absolute.
122
+ matches = url.match(%r{^(https?:\/\/[^\/]+)})
123
+ raise ArgumentError, 'Invalid Url format.' if matches.nil?
124
+
125
+ # Get the http protocol match.
126
+ protocol = matches[1]
127
+
128
+ # Check if parameters exist.
129
+ index = url.index('?')
130
+
131
+ # Remove redundant forward slashes.
132
+ query = url[protocol.length...(!index.nil? ? index : url.length)]
133
+ query.gsub!(%r{\/\/+}, '/')
134
+
135
+ # Get the parameters.
136
+ parameters = !index.nil? ? url[url.index('?')...url.length] : ''
137
+
138
+ # Return processed url.
139
+ protocol + query + parameters
140
+ end
141
+
142
+ # Parses JSON string.
143
+ # @param [String] A JSON string.
144
+ def self.json_deserialize(json)
145
+ return JSON.parse(json)
146
+ rescue StandardError
147
+ raise TypeError, 'Server responded with invalid JSON.'
148
+ end
149
+
150
+ # Removes elements with empty values from a hash.
151
+ # @param [Hash] The hash to clean.
152
+ def self.clean_hash(hash)
153
+ hash.delete_if { |_key, value| value.to_s.strip.empty? }
154
+ end
155
+
156
+ # Form encodes a hash of parameters.
157
+ # @param [Hash] The hash of parameters to encode.
158
+ # @return [Hash] A hash with the same parameters form encoded.
159
+ def self.form_encode_parameters(form_parameters)
160
+ array_serialization = 'indexed'
161
+ encoded = {}
162
+ form_parameters.each do |key, value|
163
+ encoded.merge!(APIHelper.form_encode(value, key, formatting:
164
+ array_serialization))
165
+ end
166
+ encoded
167
+ end
168
+
169
+ def self.custom_merge(a, b)
170
+ x = {}
171
+ a.each do |key, value_a|
172
+ b.each do |k, value_b|
173
+ next unless key == k
174
+ x[k] = []
175
+ if value_a.instance_of? Array
176
+ value_a.each do |v|
177
+ x[k].push(v)
178
+ end
179
+ else
180
+ x[k].push(value_a)
181
+ end
182
+ if value_b.instance_of? Array
183
+ value_b.each do |v|
184
+ x[k].push(v)
185
+ end
186
+ else
187
+ x[k].push(value_b)
188
+ end
189
+ a.delete(k)
190
+ b.delete(k)
191
+ end
192
+ end
193
+ x.merge!(a)
194
+ x.merge!(b)
195
+ x
196
+ end
197
+
198
+ # Form encodes an object.
199
+ # @param [Dynamic] An object to form encode.
200
+ # @param [String] The name of the object.
201
+ # @return [Hash] A form encoded representation of the object in the form
202
+ # of a hash.
203
+ def self.form_encode(obj, instance_name, formatting: 'indexed')
204
+ retval = {}
205
+
206
+ serializable_types = [String, Numeric, TrueClass,
207
+ FalseClass, Date, DateTime]
208
+
209
+ # If this is a structure, resolve it's field names.
210
+ obj = obj.to_hash if obj.is_a? BaseModel
211
+
212
+ # Create a form encoded hash for this object.
213
+ if obj.nil?
214
+ nil
215
+ elsif obj.instance_of? Array
216
+ if formatting == 'indexed'
217
+ obj.each_with_index do |value, index|
218
+ retval.merge!(APIHelper.form_encode(value, instance_name + '[' +
219
+ index.to_s + ']'))
220
+ end
221
+ elsif serializable_types.map { |x| obj[0].is_a? x }.any?
222
+ obj.each do |value|
223
+ abc = if formatting == 'unindexed'
224
+ APIHelper.form_encode(value, instance_name + '[]',
225
+ formatting: formatting)
226
+ else
227
+ APIHelper.form_encode(value, instance_name,
228
+ formatting: formatting)
229
+ end
230
+ retval = APIHelper.custom_merge(retval, abc)
231
+ end
232
+ else
233
+ obj.each_with_index do |value, index|
234
+ retval.merge!(APIHelper.form_encode(value, instance_name + '[' +
235
+ index.to_s + ']', formatting: formatting))
236
+ end
237
+ end
238
+ elsif obj.instance_of? Hash
239
+ obj.each do |key, value|
240
+ retval.merge!(APIHelper.form_encode(value, instance_name + '[' +
241
+ key.to_s + ']', formatting: formatting))
242
+ end
243
+ elsif obj.instance_of? File
244
+ retval[instance_name] = UploadIO.new(
245
+ obj, 'application/octet-stream', File.basename(obj.path)
246
+ )
247
+ else
248
+ retval[instance_name] = obj
249
+ end
250
+ retval
251
+ end
252
+
253
+ # Retrieves a field from a Hash/Array based on an Array of keys/indexes
254
+ # @param [Hash, Array] The hash to extract data from
255
+ # @param [Array<String, Integer>] The keys/indexes to use
256
+ # @return [Object] The extracted value
257
+ def self.map_response(obj, keys)
258
+ val = obj
259
+ begin
260
+ keys.each do |key|
261
+ val = if val.is_a? Array
262
+ if key.to_i.to_s == key
263
+ val[key.to_i]
264
+ else
265
+ val = nil
266
+ end
267
+ else
268
+ val.fetch(key.to_sym)
269
+ end
270
+ end
271
+ rescue NoMethodError, TypeError, IndexError
272
+ val = nil
273
+ end
274
+ val
275
+ end
276
+
277
+ # Safely converts a string into an rfc3339 DateTime object
278
+ # @param [String] The datetime string
279
+ # @return [DateTime] A DateTime object of rfc3339 format
280
+ def self.rfc3339(date_time)
281
+ # missing timezone information
282
+ if date_time.end_with?('Z') || date_time.index('+')
283
+ DateTime.rfc3339(date_time)
284
+ else
285
+ DateTime.rfc3339(date_time + 'Z')
286
+ end
287
+ end
288
+ end
289
+ end
@@ -0,0 +1,61 @@
1
+ # md_notes
2
+ #
3
+ # This file was automatically generated by APIMATIC v2.0
4
+ # ( https://apimatic.io ).
5
+
6
+ module MdNotes
7
+ # md_notes client class.
8
+ class Client
9
+ attr_reader :config
10
+
11
+ # Returns the authentication class for easy access.
12
+ def auth
13
+ OAuth2
14
+ end
15
+
16
+ # Access to service controller.
17
+ # @return [ServiceController] Returns the controller instance.
18
+ def service
19
+ @service ||= ServiceController.new config
20
+ end
21
+
22
+ # Access to user controller.
23
+ # @return [UserController] Returns the controller instance.
24
+ def user
25
+ @user ||= UserController.new config
26
+ end
27
+
28
+ # Access to o_auth_authorization controller.
29
+ # @return [OAuthAuthorizationController] Returns the controller instance.
30
+ def o_auth_authorization
31
+ @o_auth_authorization ||= OAuthAuthorizationController.new config
32
+ end
33
+
34
+ def initialize(timeout: 60, max_retries: 0, retry_interval: 1,
35
+ backoff_factor: 2,
36
+ retry_statuses: [408, 413, 429, 500, 502, 503, 504, 521, 522, 524],
37
+ retry_methods: %i[get put],
38
+ environment: Environment::PRODUCTION,
39
+ o_auth_client_id: 'TODO: Replace',
40
+ o_auth_client_secret: 'TODO: Replace',
41
+ o_auth_username: 'TODO: Replace',
42
+ o_auth_password: 'TODO: Replace', o_auth_token: nil,
43
+ config: nil)
44
+ @config = if config.nil?
45
+ Configuration.new(timeout: timeout, max_retries: max_retries,
46
+ retry_interval: retry_interval,
47
+ backoff_factor: backoff_factor,
48
+ retry_statuses: retry_statuses,
49
+ retry_methods: retry_methods,
50
+ environment: environment,
51
+ o_auth_client_id: o_auth_client_id,
52
+ o_auth_client_secret: o_auth_client_secret,
53
+ o_auth_username: o_auth_username,
54
+ o_auth_password: o_auth_password,
55
+ o_auth_token: o_auth_token)
56
+ else
57
+ config
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,157 @@
1
+ # md_notes
2
+ #
3
+ # This file was automatically generated by APIMATIC v2.0
4
+ # ( https://apimatic.io ).
5
+
6
+ module MdNotes
7
+ # An enum for SDK environments.
8
+ class Environment
9
+ ENVIRONMENT = [
10
+ PRODUCTION = 'production'.freeze
11
+ ].freeze
12
+ end
13
+
14
+ # An enum for API servers.
15
+ class Server
16
+ SERVER = [
17
+ DEFAULT = 'default'.freeze
18
+ ].freeze
19
+ end
20
+
21
+ # All configuration including auth info and base URI for the API access
22
+ # are configured in this class.
23
+ class Configuration
24
+ # The attribute readers for properties.
25
+ attr_reader :http_client
26
+ attr_reader :timeout
27
+ attr_reader :max_retries
28
+ attr_reader :retry_interval
29
+ attr_reader :backoff_factor
30
+ attr_reader :retry_statuses
31
+ attr_reader :retry_methods
32
+ attr_reader :environment
33
+ attr_reader :o_auth_client_id
34
+ attr_reader :o_auth_client_secret
35
+ attr_reader :o_auth_username
36
+ attr_reader :o_auth_password
37
+
38
+ def o_auth_token
39
+ if @o_auth_token.is_a? OAuthToken
40
+ OAuthToken.from_hash @o_auth_token.to_hash
41
+ else
42
+ @o_auth_token
43
+ end
44
+ end
45
+
46
+ class << self
47
+ attr_reader :environments
48
+ end
49
+
50
+ def initialize(timeout: 60, max_retries: 0, retry_interval: 1,
51
+ backoff_factor: 2,
52
+ retry_statuses: [408, 413, 429, 500, 502, 503, 504, 521, 522, 524],
53
+ retry_methods: %i[get put],
54
+ environment: Environment::PRODUCTION,
55
+ o_auth_client_id: 'TODO: Replace',
56
+ o_auth_client_secret: 'TODO: Replace',
57
+ o_auth_username: 'TODO: Replace',
58
+ o_auth_password: 'TODO: Replace', o_auth_token: nil)
59
+ # The value to use for connection timeout
60
+ @timeout = timeout
61
+
62
+ # The number of times to retry an endpoint call if it fails
63
+ @max_retries = max_retries
64
+
65
+ # Pause in seconds between retries
66
+ @retry_interval = retry_interval
67
+
68
+ # The amount to multiply each successive retry's interval amount
69
+ # by in order to provide backoff
70
+ @backoff_factor = backoff_factor
71
+
72
+ # A list of HTTP statuses to retry
73
+ @retry_statuses = retry_statuses
74
+
75
+ # A list of HTTP methods to retry
76
+ @retry_methods = retry_methods
77
+
78
+ # Current API environment
79
+ @environment = String(environment)
80
+
81
+ # OAuth 2 Client ID
82
+ @o_auth_client_id = o_auth_client_id
83
+
84
+ # OAuth 2 Client Secret
85
+ @o_auth_client_secret = o_auth_client_secret
86
+
87
+ # OAuth 2 Resource Owner Username
88
+ @o_auth_username = o_auth_username
89
+
90
+ # OAuth 2 Resource Owner Password
91
+ @o_auth_password = o_auth_password
92
+
93
+ # Object for storing information about the OAuth token
94
+ @o_auth_token = if o_auth_token.is_a? OAuthToken
95
+ OAuthToken.from_hash o_auth_token.to_hash
96
+ else
97
+ o_auth_token
98
+ end
99
+
100
+ # The Http Client to use for making requests.
101
+ @http_client = create_http_client
102
+ end
103
+
104
+ def clone_with(timeout: nil, max_retries: nil, retry_interval: nil,
105
+ backoff_factor: nil, retry_statuses: nil, retry_methods: nil,
106
+ environment: nil, o_auth_client_id: nil,
107
+ o_auth_client_secret: nil, o_auth_username: nil,
108
+ o_auth_password: nil, o_auth_token: nil)
109
+ timeout ||= self.timeout
110
+ max_retries ||= self.max_retries
111
+ retry_interval ||= self.retry_interval
112
+ backoff_factor ||= self.backoff_factor
113
+ retry_statuses ||= self.retry_statuses
114
+ retry_methods ||= self.retry_methods
115
+ environment ||= self.environment
116
+ o_auth_client_id ||= self.o_auth_client_id
117
+ o_auth_client_secret ||= self.o_auth_client_secret
118
+ o_auth_username ||= self.o_auth_username
119
+ o_auth_password ||= self.o_auth_password
120
+ o_auth_token ||= self.o_auth_token
121
+
122
+ Configuration.new(timeout: timeout, max_retries: max_retries,
123
+ retry_interval: retry_interval,
124
+ backoff_factor: backoff_factor,
125
+ retry_statuses: retry_statuses,
126
+ retry_methods: retry_methods, environment: environment,
127
+ o_auth_client_id: o_auth_client_id,
128
+ o_auth_client_secret: o_auth_client_secret,
129
+ o_auth_username: o_auth_username,
130
+ o_auth_password: o_auth_password,
131
+ o_auth_token: o_auth_token)
132
+ end
133
+
134
+ def create_http_client
135
+ FaradayClient.new(timeout: timeout, max_retries: max_retries,
136
+ retry_interval: retry_interval,
137
+ backoff_factor: backoff_factor,
138
+ retry_statuses: retry_statuses,
139
+ retry_methods: retry_methods)
140
+ end
141
+
142
+ # All the environments the SDK can run in.
143
+ ENVIRONMENTS = {
144
+ Environment::PRODUCTION => {
145
+ Server::DEFAULT => 'http://markdown-notes-app.herokuapp.com'
146
+ }
147
+ }.freeze
148
+
149
+ # Generates the appropriate base URI for the environment and the server.
150
+ # @param [Configuration::Server] The server enum for which the base URI is
151
+ # required.
152
+ # @return [String] The base URI.
153
+ def get_base_uri(server = Server::DEFAULT)
154
+ ENVIRONMENTS[environment][server].clone
155
+ end
156
+ end
157
+ end