shotstack 0.0.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +4 -0
- data/lib/shotstack/api/render_api.rb +137 -0
- data/lib/shotstack/api_client.rb +318 -0
- data/lib/shotstack/api_error.rb +24 -0
- data/lib/shotstack/configuration.rb +170 -0
- data/lib/shotstack/models/clips.rb +147 -0
- data/lib/shotstack/models/edit.rb +157 -0
- data/lib/shotstack/models/image_clip.rb +216 -0
- data/lib/shotstack/models/image_clip_options.rb +175 -0
- data/lib/shotstack/models/output.rb +175 -0
- data/lib/shotstack/models/queued_response.rb +167 -0
- data/lib/shotstack/models/queued_response_data.rb +157 -0
- data/lib/shotstack/models/render_response.rb +167 -0
- data/lib/shotstack/models/render_response_data.rb +206 -0
- data/lib/shotstack/models/soundtrack.rb +157 -0
- data/lib/shotstack/models/timeline.rb +169 -0
- data/lib/shotstack/models/title_clip.rb +216 -0
- data/lib/shotstack/models/title_clip_options.rb +194 -0
- data/lib/shotstack/models/track.rb +149 -0
- data/lib/shotstack/models/transition.rb +175 -0
- data/lib/shotstack/models/video_clip.rb +216 -0
- data/lib/shotstack/models/video_clip_options.rb +185 -0
- data/lib/shotstack/version.rb +3 -0
- data/lib/shotstack.rb +45 -0
- data/shotstack.gemspec +32 -0
- data/tags +306 -0
- metadata +250 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 31d87d73c44a309627e675a50328dd0595745f55
|
4
|
+
data.tar.gz: 43089c6e46758d056c8c7926f160a6b9437890ed
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 734bb28a5608c27273234cae6e3cfcfcf7f95ad89fb04c7399adac6e3e2f54da3733da9785866211ebf5717a4ac49ef097a78a5b35f58a9e02759dd375784864
|
7
|
+
data.tar.gz: c13acb5444f705bbdef61583edb555d0ab0a3809f9378e89604c592f302d645c8860207708ad9badccacad21fd9c2ffd25e7c00a8175ec7b0eccbea2ea0af54c
|
data/README.md
ADDED
@@ -0,0 +1,137 @@
|
|
1
|
+
require "uri"
|
2
|
+
|
3
|
+
module Shotstack
|
4
|
+
class RenderApi
|
5
|
+
attr_accessor :api_client
|
6
|
+
|
7
|
+
def initialize(api_client = ApiClient.default)
|
8
|
+
@api_client = api_client
|
9
|
+
end
|
10
|
+
|
11
|
+
#
|
12
|
+
# Render the contents of a timeline as a video file.
|
13
|
+
# @param edit Edit
|
14
|
+
# @param [Hash] opts the optional parameters
|
15
|
+
# @return [QueuedResponse]
|
16
|
+
def post_render(edit, opts = {})
|
17
|
+
data, status_code, headers = post_render_with_http_info(edit, opts)
|
18
|
+
return data
|
19
|
+
end
|
20
|
+
|
21
|
+
#
|
22
|
+
# Render the contents of a timeline as a video file.
|
23
|
+
# @param edit Edit
|
24
|
+
# @param [Hash] opts the optional parameters
|
25
|
+
# @return [Array<(QueuedResponse, Fixnum, Hash)>] QueuedResponse data, response status code and response headers
|
26
|
+
def post_render_with_http_info(edit, opts = {})
|
27
|
+
if @api_client.config.debugging
|
28
|
+
@api_client.config.logger.debug "Calling API: RenderApi#post_render ..."
|
29
|
+
end
|
30
|
+
|
31
|
+
# verify the required parameter 'edit' is set
|
32
|
+
fail "Missing the required parameter 'edit' when calling post_render" if edit.nil?
|
33
|
+
|
34
|
+
# resource path
|
35
|
+
path = "/render".sub('{format}','json')
|
36
|
+
|
37
|
+
# query parameters
|
38
|
+
query_params = {}
|
39
|
+
|
40
|
+
# header parameters
|
41
|
+
header_params = {}
|
42
|
+
|
43
|
+
# HTTP header 'Accept' (if needed)
|
44
|
+
_header_accept = ['application/json']
|
45
|
+
_header_accept_result = @api_client.select_header_accept(_header_accept) and header_params['Accept'] = _header_accept_result
|
46
|
+
|
47
|
+
# HTTP header 'Content-Type'
|
48
|
+
_header_content_type = []
|
49
|
+
header_params['Content-Type'] = @api_client.select_header_content_type(_header_content_type)
|
50
|
+
|
51
|
+
# form parameters
|
52
|
+
form_params = {}
|
53
|
+
|
54
|
+
# http body (model)
|
55
|
+
post_body = @api_client.object_to_http_body(edit)
|
56
|
+
|
57
|
+
|
58
|
+
auth_names = ['DeveloperKey']
|
59
|
+
data, status_code, headers = @api_client.call_api(:POST, path,
|
60
|
+
:header_params => header_params,
|
61
|
+
:query_params => query_params,
|
62
|
+
:form_params => form_params,
|
63
|
+
:body => post_body,
|
64
|
+
:auth_names => auth_names,
|
65
|
+
:return_type => 'QueuedResponse')
|
66
|
+
if @api_client.config.debugging
|
67
|
+
@api_client.config.logger.debug "API called: RenderApi#post_render\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
|
68
|
+
end
|
69
|
+
return data, status_code, headers
|
70
|
+
end
|
71
|
+
|
72
|
+
#
|
73
|
+
# Get the rendering status and details of a timeline by ID.
|
74
|
+
# @param id The id of the timeline render task in UUID format
|
75
|
+
# @param [Hash] opts the optional parameters
|
76
|
+
# @return [RenderResponse]
|
77
|
+
def get_render(id, opts = {})
|
78
|
+
data, status_code, headers = get_render_with_http_info(id, opts)
|
79
|
+
return data
|
80
|
+
end
|
81
|
+
|
82
|
+
#
|
83
|
+
# Get the rendering status and details of a timeline by ID.
|
84
|
+
# @param id The id of the timeline render task in UUID format
|
85
|
+
# @param [Hash] opts the optional parameters
|
86
|
+
# @return [Array<(RenderResponse, Fixnum, Hash)>] RenderResponse data, response status code and response headers
|
87
|
+
def get_render_with_http_info(id, opts = {})
|
88
|
+
if @api_client.config.debugging
|
89
|
+
@api_client.config.logger.debug "Calling API: RenderApi#get_render ..."
|
90
|
+
end
|
91
|
+
|
92
|
+
# verify the required parameter 'id' is set
|
93
|
+
fail "Missing the required parameter 'id' when calling get_render" if id.nil?
|
94
|
+
|
95
|
+
# resource path
|
96
|
+
path = "/render/{id}".sub('{format}','json').sub('{' + 'id' + '}', id.to_s)
|
97
|
+
|
98
|
+
# query parameters
|
99
|
+
query_params = {}
|
100
|
+
|
101
|
+
# header parameters
|
102
|
+
header_params = {}
|
103
|
+
|
104
|
+
# HTTP header 'Accept' (if needed)
|
105
|
+
_header_accept = ['application/json']
|
106
|
+
_header_accept_result = @api_client.select_header_accept(_header_accept) and header_params['Accept'] = _header_accept_result
|
107
|
+
|
108
|
+
# HTTP header 'Content-Type'
|
109
|
+
_header_content_type = []
|
110
|
+
header_params['Content-Type'] = @api_client.select_header_content_type(_header_content_type)
|
111
|
+
|
112
|
+
# form parameters
|
113
|
+
form_params = {}
|
114
|
+
|
115
|
+
# http body (model)
|
116
|
+
post_body = nil
|
117
|
+
|
118
|
+
|
119
|
+
auth_names = ['DeveloperKey']
|
120
|
+
data, status_code, headers = @api_client.call_api(:GET, path,
|
121
|
+
:header_params => header_params,
|
122
|
+
:query_params => query_params,
|
123
|
+
:form_params => form_params,
|
124
|
+
:body => post_body,
|
125
|
+
:auth_names => auth_names,
|
126
|
+
:return_type => 'RenderResponse')
|
127
|
+
if @api_client.config.debugging
|
128
|
+
@api_client.config.logger.debug "API called: RenderApi#get_render\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
|
129
|
+
end
|
130
|
+
return data, status_code, headers
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
|
@@ -0,0 +1,318 @@
|
|
1
|
+
require 'date'
|
2
|
+
require 'json'
|
3
|
+
require 'logger'
|
4
|
+
require 'tempfile'
|
5
|
+
require 'typhoeus'
|
6
|
+
require 'uri'
|
7
|
+
|
8
|
+
module Shotstack
|
9
|
+
class ApiClient
|
10
|
+
# The Configuration object holding settings to be used in the API client.
|
11
|
+
attr_accessor :config
|
12
|
+
|
13
|
+
# Defines the headers to be used in HTTP requests of all API calls by default.
|
14
|
+
#
|
15
|
+
# @return [Hash]
|
16
|
+
attr_accessor :default_headers
|
17
|
+
|
18
|
+
def initialize(config = Configuration.default)
|
19
|
+
@config = config
|
20
|
+
@user_agent = "ruby-swagger-#{VERSION}"
|
21
|
+
@default_headers = {
|
22
|
+
'Content-Type' => "application/json",
|
23
|
+
'User-Agent' => @user_agent
|
24
|
+
}
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.default
|
28
|
+
@@default ||= ApiClient.new
|
29
|
+
end
|
30
|
+
|
31
|
+
# Call an API with given options.
|
32
|
+
#
|
33
|
+
# @return [Array<(Object, Fixnum, Hash)>] an array of 3 elements:
|
34
|
+
# the data deserialized from response body (could be nil), response status code and response headers.
|
35
|
+
def call_api(http_method, path, opts = {})
|
36
|
+
request = build_request(http_method, path, opts)
|
37
|
+
response = request.run
|
38
|
+
|
39
|
+
if @config.debugging
|
40
|
+
@config.logger.debug "HTTP response body ~BEGIN~\n#{response.body}\n~END~\n"
|
41
|
+
end
|
42
|
+
|
43
|
+
unless response.success?
|
44
|
+
fail ApiError.new(:code => response.code,
|
45
|
+
:response_headers => response.headers,
|
46
|
+
:response_body => response.body),
|
47
|
+
response.status_message
|
48
|
+
end
|
49
|
+
|
50
|
+
if opts[:return_type]
|
51
|
+
data = deserialize(response, opts[:return_type])
|
52
|
+
else
|
53
|
+
data = nil
|
54
|
+
end
|
55
|
+
return data, response.code, response.headers
|
56
|
+
end
|
57
|
+
|
58
|
+
def build_request(http_method, path, opts = {})
|
59
|
+
url = build_request_url(path)
|
60
|
+
http_method = http_method.to_sym.downcase
|
61
|
+
|
62
|
+
header_params = @default_headers.merge(opts[:header_params] || {})
|
63
|
+
query_params = opts[:query_params] || {}
|
64
|
+
form_params = opts[:form_params] || {}
|
65
|
+
|
66
|
+
|
67
|
+
update_params_for_auth! header_params, query_params, opts[:auth_names]
|
68
|
+
|
69
|
+
|
70
|
+
req_opts = {
|
71
|
+
:method => http_method,
|
72
|
+
:headers => header_params,
|
73
|
+
:params => query_params,
|
74
|
+
:timeout => @config.timeout,
|
75
|
+
:ssl_verifypeer => @config.verify_ssl,
|
76
|
+
:sslcert => @config.cert_file,
|
77
|
+
:sslkey => @config.key_file,
|
78
|
+
:cainfo => @config.ssl_ca_cert,
|
79
|
+
:verbose => @config.debugging
|
80
|
+
}
|
81
|
+
|
82
|
+
if [:post, :patch, :put, :delete].include?(http_method)
|
83
|
+
req_body = build_request_body(header_params, form_params, opts[:body])
|
84
|
+
req_opts.update :body => req_body
|
85
|
+
if @config.debugging
|
86
|
+
@config.logger.debug "HTTP request body param ~BEGIN~\n#{req_body}\n~END~\n"
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
Typhoeus::Request.new(url, req_opts)
|
91
|
+
end
|
92
|
+
|
93
|
+
# Check if the given MIME is a JSON MIME.
|
94
|
+
# JSON MIME examples:
|
95
|
+
# application/json
|
96
|
+
# application/json; charset=UTF8
|
97
|
+
# APPLICATION/JSON
|
98
|
+
def json_mime?(mime)
|
99
|
+
!!(mime =~ /\Aapplication\/json(;.*)?\z/i)
|
100
|
+
end
|
101
|
+
|
102
|
+
# Deserialize the response to the given return type.
|
103
|
+
#
|
104
|
+
# @param [String] return_type some examples: "User", "Array[User]", "Hash[String,Integer]"
|
105
|
+
def deserialize(response, return_type)
|
106
|
+
body = response.body
|
107
|
+
return nil if body.nil? || body.empty?
|
108
|
+
|
109
|
+
# handle file downloading - save response body into a tmp file and return the File instance
|
110
|
+
return download_file(response) if return_type == 'File'
|
111
|
+
|
112
|
+
# ensuring a default content type
|
113
|
+
content_type = response.headers['Content-Type'] || 'application/json'
|
114
|
+
|
115
|
+
fail "Content-Type is not supported: #{content_type}" unless json_mime?(content_type)
|
116
|
+
|
117
|
+
begin
|
118
|
+
data = JSON.parse("[#{body}]", :symbolize_names => true)[0]
|
119
|
+
rescue JSON::ParserError => e
|
120
|
+
if %w(String Date DateTime).include?(return_type)
|
121
|
+
data = body
|
122
|
+
else
|
123
|
+
raise e
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
convert_to_type data, return_type
|
128
|
+
end
|
129
|
+
|
130
|
+
# Convert data to the given return type.
|
131
|
+
def convert_to_type(data, return_type)
|
132
|
+
return nil if data.nil?
|
133
|
+
case return_type
|
134
|
+
when 'String'
|
135
|
+
data.to_s
|
136
|
+
when 'Integer'
|
137
|
+
data.to_i
|
138
|
+
when 'Float'
|
139
|
+
data.to_f
|
140
|
+
when 'BOOLEAN'
|
141
|
+
data == true
|
142
|
+
when 'DateTime'
|
143
|
+
# parse date time (expecting ISO 8601 format)
|
144
|
+
DateTime.parse data
|
145
|
+
when 'Date'
|
146
|
+
# parse date time (expecting ISO 8601 format)
|
147
|
+
Date.parse data
|
148
|
+
when 'Object'
|
149
|
+
# generic object, return directly
|
150
|
+
data
|
151
|
+
when /\AArray<(.+)>\z/
|
152
|
+
# e.g. Array<Pet>
|
153
|
+
sub_type = $1
|
154
|
+
data.map {|item| convert_to_type(item, sub_type) }
|
155
|
+
when /\AHash\<String, (.+)\>\z/
|
156
|
+
# e.g. Hash<String, Integer>
|
157
|
+
sub_type = $1
|
158
|
+
{}.tap do |hash|
|
159
|
+
data.each {|k, v| hash[k] = convert_to_type(v, sub_type) }
|
160
|
+
end
|
161
|
+
else
|
162
|
+
# models, e.g. Pet
|
163
|
+
Shotstack.const_get(return_type).new.tap do |model|
|
164
|
+
model.build_from_hash data
|
165
|
+
end
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
# Save response body into a file in (the defined) temporary folder, using the filename
|
170
|
+
# from the "Content-Disposition" header if provided, otherwise a random filename.
|
171
|
+
#
|
172
|
+
# @see Configuration#temp_folder_path
|
173
|
+
# @return [Tempfile] the file downloaded
|
174
|
+
def download_file(response)
|
175
|
+
content_disposition = response.headers['Content-Disposition']
|
176
|
+
if content_disposition
|
177
|
+
filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1]
|
178
|
+
prefix = sanitize_filename(filename)
|
179
|
+
else
|
180
|
+
prefix = 'download-'
|
181
|
+
end
|
182
|
+
prefix = prefix + '-' unless prefix.end_with?('-')
|
183
|
+
|
184
|
+
tempfile = nil
|
185
|
+
encoding = response.body.encoding
|
186
|
+
Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding) do |file|
|
187
|
+
file.write(response.body)
|
188
|
+
tempfile = file
|
189
|
+
end
|
190
|
+
@config.logger.info "Temp file written to #{tempfile.path}, please copy the file to a proper folder "\
|
191
|
+
"with e.g. `FileUtils.cp(tempfile.path, '/new/file/path')` otherwise the temp file "\
|
192
|
+
"will be deleted automatically with GC. It's also recommended to delete the temp file "\
|
193
|
+
"explicitly with `tempfile.delete`"
|
194
|
+
tempfile
|
195
|
+
end
|
196
|
+
|
197
|
+
# Sanitize filename by removing path.
|
198
|
+
# e.g. ../../sun.gif becomes sun.gif
|
199
|
+
#
|
200
|
+
# @param [String] filename the filename to be sanitized
|
201
|
+
# @return [String] the sanitized filename
|
202
|
+
def sanitize_filename(filename)
|
203
|
+
filename.gsub /.*[\/\\]/, ''
|
204
|
+
end
|
205
|
+
|
206
|
+
def build_request_url(path)
|
207
|
+
# Add leading and trailing slashes to path
|
208
|
+
path = "/#{path}".gsub(/\/+/, '/')
|
209
|
+
URI.encode(@config.base_url + path)
|
210
|
+
end
|
211
|
+
|
212
|
+
def build_request_body(header_params, form_params, body)
|
213
|
+
# http form
|
214
|
+
if header_params['Content-Type'] == 'application/x-www-form-urlencoded' ||
|
215
|
+
header_params['Content-Type'] == 'multipart/form-data'
|
216
|
+
data = {}
|
217
|
+
form_params.each do |key, value|
|
218
|
+
case value
|
219
|
+
when File, Array, nil
|
220
|
+
# let typhoeus handle File, Array and nil parameters
|
221
|
+
data[key] = value
|
222
|
+
else
|
223
|
+
data[key] = value.to_s
|
224
|
+
end
|
225
|
+
end
|
226
|
+
elsif body
|
227
|
+
data = body.is_a?(String) ? body : body.to_json
|
228
|
+
else
|
229
|
+
data = nil
|
230
|
+
end
|
231
|
+
data
|
232
|
+
end
|
233
|
+
|
234
|
+
# Update hearder and query params based on authentication settings.
|
235
|
+
def update_params_for_auth!(header_params, query_params, auth_names)
|
236
|
+
Array(auth_names).each do |auth_name|
|
237
|
+
auth_setting = @config.auth_settings[auth_name]
|
238
|
+
next unless auth_setting
|
239
|
+
case auth_setting[:in]
|
240
|
+
when 'header' then header_params[auth_setting[:key]] = auth_setting[:value]
|
241
|
+
when 'query' then query_params[auth_setting[:key]] = auth_setting[:value]
|
242
|
+
else fail ArgumentError, 'Authentication token must be in `query` of `header`'
|
243
|
+
end
|
244
|
+
end
|
245
|
+
end
|
246
|
+
|
247
|
+
def user_agent=(user_agent)
|
248
|
+
@user_agent = user_agent
|
249
|
+
@default_headers['User-Agent'] = @user_agent
|
250
|
+
end
|
251
|
+
|
252
|
+
# Return Accept header based on an array of accepts provided.
|
253
|
+
# @param [Array] accepts array for Accept
|
254
|
+
# @return [String] the Accept header (e.g. application/json)
|
255
|
+
def select_header_accept(accepts)
|
256
|
+
return nil if accepts.nil? || accepts.empty?
|
257
|
+
# use JSON when present, otherwise use all of the provided
|
258
|
+
json_accept = accepts.find { |s| json_mime?(s) }
|
259
|
+
return json_accept || accepts.join(',')
|
260
|
+
end
|
261
|
+
|
262
|
+
# Return Content-Type header based on an array of content types provided.
|
263
|
+
# @param [Array] content_types array for Content-Type
|
264
|
+
# @return [String] the Content-Type header (e.g. application/json)
|
265
|
+
def select_header_content_type(content_types)
|
266
|
+
# use application/json by default
|
267
|
+
return 'application/json' if content_types.nil? || content_types.empty?
|
268
|
+
# use JSON when present, otherwise use the first one
|
269
|
+
json_content_type = content_types.find { |s| json_mime?(s) }
|
270
|
+
return json_content_type || content_types.first
|
271
|
+
end
|
272
|
+
|
273
|
+
# Convert object (array, hash, object, etc) to JSON string.
|
274
|
+
# @param [Object] model object to be converted into JSON string
|
275
|
+
# @return [String] JSON string representation of the object
|
276
|
+
def object_to_http_body(model)
|
277
|
+
return if model.nil?
|
278
|
+
_body = nil
|
279
|
+
if model.is_a?(Array)
|
280
|
+
_body = model.map{|m| object_to_hash(m) }
|
281
|
+
else
|
282
|
+
_body = object_to_hash(model)
|
283
|
+
end
|
284
|
+
_body.to_json
|
285
|
+
end
|
286
|
+
|
287
|
+
# Convert object(non-array) to hash.
|
288
|
+
# @param [Object] obj object to be converted into JSON string
|
289
|
+
# @return [String] JSON string representation of the object
|
290
|
+
def object_to_hash(obj)
|
291
|
+
if obj.respond_to?(:to_hash)
|
292
|
+
obj.to_hash
|
293
|
+
else
|
294
|
+
obj
|
295
|
+
end
|
296
|
+
end
|
297
|
+
|
298
|
+
# Build parameter value according to the given collection format.
|
299
|
+
# @param [String] collection_format one of :csv, :ssv, :tsv, :pipes and :multi
|
300
|
+
def build_collection_param(param, collection_format)
|
301
|
+
case collection_format
|
302
|
+
when :csv
|
303
|
+
param.join(',')
|
304
|
+
when :ssv
|
305
|
+
param.join(' ')
|
306
|
+
when :tsv
|
307
|
+
param.join("\t")
|
308
|
+
when :pipes
|
309
|
+
param.join('|')
|
310
|
+
when :multi
|
311
|
+
# return the array directly as typhoeus will handle it as expected
|
312
|
+
param
|
313
|
+
else
|
314
|
+
fail "unknown collection format: #{collection_format.inspect}"
|
315
|
+
end
|
316
|
+
end
|
317
|
+
end
|
318
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module Shotstack
|
2
|
+
class ApiError < StandardError
|
3
|
+
attr_reader :code, :response_headers, :response_body
|
4
|
+
|
5
|
+
# Usage examples:
|
6
|
+
# ApiError.new
|
7
|
+
# ApiError.new("message")
|
8
|
+
# ApiError.new(:code => 500, :response_headers => {}, :response_body => "")
|
9
|
+
# ApiError.new(:code => 404, :message => "Not Found")
|
10
|
+
def initialize(arg = nil)
|
11
|
+
if arg.is_a? Hash
|
12
|
+
arg.each do |k, v|
|
13
|
+
if k.to_s == 'message'
|
14
|
+
super v
|
15
|
+
else
|
16
|
+
instance_variable_set "@#{k}", v
|
17
|
+
end
|
18
|
+
end
|
19
|
+
else
|
20
|
+
super arg
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,170 @@
|
|
1
|
+
require 'uri'
|
2
|
+
|
3
|
+
module Shotstack
|
4
|
+
class Configuration
|
5
|
+
# Defines url scheme
|
6
|
+
attr_accessor :scheme
|
7
|
+
|
8
|
+
# Defines url host
|
9
|
+
attr_accessor :host
|
10
|
+
|
11
|
+
# Defines url base path
|
12
|
+
attr_accessor :base_path
|
13
|
+
|
14
|
+
# Defines API keys used with API Key authentications.
|
15
|
+
#
|
16
|
+
# @return [Hash] key: parameter name, value: parameter value (API key)
|
17
|
+
#
|
18
|
+
# @example parameter name is "api_key", API key is "xxx" (e.g. "api_key=xxx" in query string)
|
19
|
+
# config.api_key['api_key'] = 'xxx'
|
20
|
+
attr_accessor :api_key
|
21
|
+
|
22
|
+
# Defines API key prefixes used with API Key authentications.
|
23
|
+
#
|
24
|
+
# @return [Hash] key: parameter name, value: API key prefix
|
25
|
+
#
|
26
|
+
# @example parameter name is "Authorization", API key prefix is "Token" (e.g. "Authorization: Token xxx" in headers)
|
27
|
+
# config.api_key_prefix['api_key'] = 'Token'
|
28
|
+
attr_accessor :api_key_prefix
|
29
|
+
|
30
|
+
# Defines the username used with HTTP basic authentication.
|
31
|
+
#
|
32
|
+
# @return [String]
|
33
|
+
attr_accessor :username
|
34
|
+
|
35
|
+
# Defines the password used with HTTP basic authentication.
|
36
|
+
#
|
37
|
+
# @return [String]
|
38
|
+
attr_accessor :password
|
39
|
+
|
40
|
+
# Defines the access token (Bearer) used with OAuth2.
|
41
|
+
attr_accessor :access_token
|
42
|
+
|
43
|
+
# Set this to enable/disable debugging. When enabled (set to true), HTTP request/response
|
44
|
+
# details will be logged with `logger.debug` (see the `logger` attribute).
|
45
|
+
# Default to false.
|
46
|
+
#
|
47
|
+
# @return [true, false]
|
48
|
+
attr_accessor :debugging
|
49
|
+
|
50
|
+
# Defines the logger used for debugging.
|
51
|
+
# Default to `Rails.logger` (when in Rails) or logging to STDOUT.
|
52
|
+
#
|
53
|
+
# @return [#debug]
|
54
|
+
attr_accessor :logger
|
55
|
+
|
56
|
+
# Defines the temporary folder to store downloaded files
|
57
|
+
# (for API endpoints that have file response).
|
58
|
+
# Default to use `Tempfile`.
|
59
|
+
#
|
60
|
+
# @return [String]
|
61
|
+
attr_accessor :temp_folder_path
|
62
|
+
|
63
|
+
# The time limit for HTTP request in seconds.
|
64
|
+
# Default to 0 (never times out).
|
65
|
+
attr_accessor :timeout
|
66
|
+
|
67
|
+
### TLS/SSL
|
68
|
+
# Set this to false to skip verifying SSL certificate when calling API from https server.
|
69
|
+
# Default to true.
|
70
|
+
#
|
71
|
+
# @note Do NOT set it to false in production code, otherwise you would face multiple types of cryptographic attacks.
|
72
|
+
#
|
73
|
+
# @return [true, false]
|
74
|
+
attr_accessor :verify_ssl
|
75
|
+
|
76
|
+
# Set this to customize the certificate file to verify the peer.
|
77
|
+
#
|
78
|
+
# @return [String] the path to the certificate file
|
79
|
+
#
|
80
|
+
# @see The `cainfo` option of Typhoeus, `--cert` option of libcurl. Related source code:
|
81
|
+
# https://github.com/typhoeus/typhoeus/blob/master/lib/typhoeus/easy_factory.rb#L145
|
82
|
+
attr_accessor :ssl_ca_cert
|
83
|
+
|
84
|
+
# Client certificate file (for client certificate)
|
85
|
+
attr_accessor :cert_file
|
86
|
+
|
87
|
+
# Client private key file (for client certificate)
|
88
|
+
attr_accessor :key_file
|
89
|
+
|
90
|
+
attr_accessor :inject_format
|
91
|
+
|
92
|
+
attr_accessor :force_ending_format
|
93
|
+
|
94
|
+
def initialize
|
95
|
+
@scheme = 'https'
|
96
|
+
@host = 'api.shotstack.io'
|
97
|
+
@base_path = '/v1'
|
98
|
+
@api_key = {}
|
99
|
+
@api_key_prefix = {}
|
100
|
+
@timeout = 0
|
101
|
+
@verify_ssl = true
|
102
|
+
@cert_file = nil
|
103
|
+
@key_file = nil
|
104
|
+
@debugging = false
|
105
|
+
@inject_format = false
|
106
|
+
@force_ending_format = false
|
107
|
+
@logger = defined?(Rails) ? Rails.logger : Logger.new(STDOUT)
|
108
|
+
|
109
|
+
yield(self) if block_given?
|
110
|
+
end
|
111
|
+
|
112
|
+
# The default Configuration object.
|
113
|
+
def self.default
|
114
|
+
@@default ||= Configuration.new
|
115
|
+
end
|
116
|
+
|
117
|
+
def configure
|
118
|
+
yield(self) if block_given?
|
119
|
+
end
|
120
|
+
|
121
|
+
def scheme=(scheme)
|
122
|
+
# remove :// from scheme
|
123
|
+
@scheme = scheme.sub(/:\/\//, '')
|
124
|
+
end
|
125
|
+
|
126
|
+
def host=(host)
|
127
|
+
# remove http(s):// and anything after a slash
|
128
|
+
@host = host.sub(/https?:\/\//, '').split('/').first
|
129
|
+
end
|
130
|
+
|
131
|
+
def base_path=(base_path)
|
132
|
+
# Add leading and trailing slashes to base_path
|
133
|
+
@base_path = "/#{base_path}".gsub(/\/+/, '/')
|
134
|
+
@base_path = "" if @base_path == "/"
|
135
|
+
end
|
136
|
+
|
137
|
+
def base_url
|
138
|
+
url = "#{scheme}://#{[host, base_path].join('/').gsub(/\/+/, '/')}".sub(/\/+\z/, '')
|
139
|
+
URI.encode(url)
|
140
|
+
end
|
141
|
+
|
142
|
+
# Gets API key (with prefix if set).
|
143
|
+
# @param [String] param_name the parameter name of API key auth
|
144
|
+
def api_key_with_prefix(param_name)
|
145
|
+
if @api_key_prefix[param_name]
|
146
|
+
"#{@api_key_prefix[param_name]} #{@api_key[param_name]}"
|
147
|
+
else
|
148
|
+
@api_key[param_name]
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
# Gets Basic Auth token string
|
153
|
+
def basic_auth_token
|
154
|
+
'Basic ' + ["#{username}:#{password}"].pack('m').delete("\r\n")
|
155
|
+
end
|
156
|
+
|
157
|
+
# Returns Auth Settings hash for api client.
|
158
|
+
def auth_settings
|
159
|
+
{
|
160
|
+
'DeveloperKey' =>
|
161
|
+
{
|
162
|
+
type: 'api_key',
|
163
|
+
in: 'header',
|
164
|
+
key: 'x-api-key',
|
165
|
+
value: api_key_with_prefix('x-api-key')
|
166
|
+
},
|
167
|
+
}
|
168
|
+
end
|
169
|
+
end
|
170
|
+
end
|