shotstack 0.0.9 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/README.md +17 -0
- data/lib/shotstack.rb +31 -15
- data/lib/shotstack/api/endpoints_api.rb +149 -0
- data/lib/shotstack/api_client.rb +142 -74
- data/lib/shotstack/api_error.rb +38 -5
- data/lib/shotstack/configuration.rb +93 -5
- data/lib/shotstack/models/asset.rb +17 -0
- data/lib/shotstack/models/audio_asset.rb +251 -0
- data/lib/shotstack/models/clip.rb +395 -0
- data/lib/shotstack/models/edit.rb +121 -42
- data/lib/shotstack/models/font.rb +213 -0
- data/lib/shotstack/models/html_asset.rb +319 -0
- data/lib/shotstack/models/image_asset.rb +231 -0
- data/lib/shotstack/models/luma_asset.rb +241 -0
- data/lib/shotstack/models/offset.rb +266 -0
- data/lib/shotstack/models/output.rb +207 -46
- data/lib/shotstack/models/poster.rb +213 -0
- data/lib/shotstack/models/queued_response.rb +121 -49
- data/lib/shotstack/models/queued_response_data.rb +116 -46
- data/lib/shotstack/models/render_response.rb +121 -49
- data/lib/shotstack/models/render_response_data.rb +193 -69
- data/lib/shotstack/models/soundtrack.rb +152 -42
- data/lib/shotstack/models/thumbnail.rb +228 -0
- data/lib/shotstack/models/timeline.rb +124 -45
- data/lib/shotstack/models/title_asset.rb +354 -0
- data/lib/shotstack/models/track.rb +100 -34
- data/lib/shotstack/models/transition.rb +132 -43
- data/lib/shotstack/models/video_asset.rb +251 -0
- data/lib/shotstack/version.rb +13 -1
- data/shotstack.gemspec +23 -16
- metadata +37 -150
- data/lib/shotstack/api/render_api.rb +0 -137
- data/lib/shotstack/models/clips.rb +0 -147
- data/lib/shotstack/models/image_clip.rb +0 -216
- data/lib/shotstack/models/image_clip_options.rb +0 -175
- data/lib/shotstack/models/title_clip.rb +0 -216
- data/lib/shotstack/models/title_clip_options.rb +0 -194
- data/lib/shotstack/models/video_clip.rb +0 -216
- data/lib/shotstack/models/video_clip_options.rb +0 -185
- data/tags +0 -306
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: d1177fae43086a9c88b2f244d5cc366de2d36ea4c1072c06e98b4b37b8ca11b5
|
4
|
+
data.tar.gz: 9d34f601b12b4d2452271e16f5dfe60d1c847792b8b4cda368222a30c35974e1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9e14b9e05c8c7291e15069e43abd65ea098ed0f842c587aa295f5bb05889dc25881aedda157576467ffb20fffc1ffdcdf469869408ac7e3a1728067f36c58fa5
|
7
|
+
data.tar.gz: 76fff0a5ed03bbbfe2c7f3cdbf0e4fe2341ea826c5878b13acee0c4d5db336d1cedde40eea0d846746c296abf514a1def2345e453539804eea88173cc986884a
|
data/README.md
CHANGED
@@ -2,3 +2,20 @@
|
|
2
2
|
|
3
3
|
Ruby SDK for Shotstack, the cloud video editing API.
|
4
4
|
|
5
|
+
## Build Instructions
|
6
|
+
|
7
|
+
First make sure the version in `shotstack/version.rb` is correct.
|
8
|
+
|
9
|
+
Then, to build as a gem:
|
10
|
+
|
11
|
+
```bash
|
12
|
+
rm shotstack-*.gem
|
13
|
+
gem build shotstack
|
14
|
+
gem push shotstack-*.*.*.gem # use the correct version number
|
15
|
+
```
|
16
|
+
|
17
|
+
For local development install the gem:
|
18
|
+
|
19
|
+
```bash
|
20
|
+
sudo gem install shotstack
|
21
|
+
```
|
data/lib/shotstack.rb
CHANGED
@@ -1,3 +1,15 @@
|
|
1
|
+
=begin
|
2
|
+
#Shotstack
|
3
|
+
|
4
|
+
#The Shotstack API is a video editing service that allows for the automated creation of videos using JSON. You can configure an edit and POST it to the Shotstack API which will render your video and provide a file location when complete. For more details check https://shotstack.io
|
5
|
+
|
6
|
+
The version of the OpenAPI document: v1
|
7
|
+
|
8
|
+
Generated by: https://openapi-generator.tech
|
9
|
+
OpenAPI Generator version: 4.2.1
|
10
|
+
|
11
|
+
=end
|
12
|
+
|
1
13
|
# Common files
|
2
14
|
require 'shotstack/api_client'
|
3
15
|
require 'shotstack/api_error'
|
@@ -5,26 +17,30 @@ require 'shotstack/version'
|
|
5
17
|
require 'shotstack/configuration'
|
6
18
|
|
7
19
|
# Models
|
8
|
-
require 'shotstack/models/
|
9
|
-
require 'shotstack/models/
|
10
|
-
require 'shotstack/models/
|
11
|
-
require 'shotstack/models/
|
20
|
+
require 'shotstack/models/audio_asset'
|
21
|
+
require 'shotstack/models/clip'
|
22
|
+
require 'shotstack/models/edit'
|
23
|
+
require 'shotstack/models/font'
|
24
|
+
require 'shotstack/models/html_asset'
|
25
|
+
require 'shotstack/models/image_asset'
|
26
|
+
require 'shotstack/models/luma_asset'
|
27
|
+
require 'shotstack/models/offset'
|
12
28
|
require 'shotstack/models/output'
|
13
|
-
require 'shotstack/models/
|
14
|
-
require 'shotstack/models/
|
15
|
-
require 'shotstack/models/
|
16
|
-
require 'shotstack/models/
|
17
|
-
require 'shotstack/models/
|
18
|
-
require 'shotstack/models/title_clip_options'
|
19
|
-
require 'shotstack/models/title_clip'
|
20
|
-
require 'shotstack/models/track'
|
21
|
-
require 'shotstack/models/clips'
|
29
|
+
require 'shotstack/models/poster'
|
30
|
+
require 'shotstack/models/queued_response'
|
31
|
+
require 'shotstack/models/queued_response_data'
|
32
|
+
require 'shotstack/models/render_response'
|
33
|
+
require 'shotstack/models/render_response_data'
|
22
34
|
require 'shotstack/models/soundtrack'
|
35
|
+
require 'shotstack/models/thumbnail'
|
23
36
|
require 'shotstack/models/timeline'
|
24
|
-
require 'shotstack/models/
|
37
|
+
require 'shotstack/models/title_asset'
|
38
|
+
require 'shotstack/models/track'
|
39
|
+
require 'shotstack/models/transition'
|
40
|
+
require 'shotstack/models/video_asset'
|
25
41
|
|
26
42
|
# APIs
|
27
|
-
require 'shotstack/api/
|
43
|
+
require 'shotstack/api/endpoints_api'
|
28
44
|
|
29
45
|
module Shotstack
|
30
46
|
class << self
|
@@ -0,0 +1,149 @@
|
|
1
|
+
=begin
|
2
|
+
#Shotstack
|
3
|
+
|
4
|
+
#The Shotstack API is a video editing service that allows for the automated creation of videos using JSON. You can configure an edit and POST it to the Shotstack API which will render your video and provide a file location when complete. For more details check https://shotstack.io
|
5
|
+
|
6
|
+
The version of the OpenAPI document: v1
|
7
|
+
|
8
|
+
Generated by: https://openapi-generator.tech
|
9
|
+
OpenAPI Generator version: 4.2.1
|
10
|
+
|
11
|
+
=end
|
12
|
+
|
13
|
+
require 'cgi'
|
14
|
+
|
15
|
+
module Shotstack
|
16
|
+
class EndpointsApi
|
17
|
+
attr_accessor :api_client
|
18
|
+
|
19
|
+
def initialize(api_client = ApiClient.default)
|
20
|
+
@api_client = api_client
|
21
|
+
end
|
22
|
+
# Get the rendering status, video url and details of a timeline by ID.
|
23
|
+
# @param id [String] The id of the timeline render task in UUID format
|
24
|
+
# @param [Hash] opts the optional parameters
|
25
|
+
# @return [RenderResponse]
|
26
|
+
def get_render(id, opts = {})
|
27
|
+
data, _status_code, _headers = get_render_with_http_info(id, opts)
|
28
|
+
data
|
29
|
+
end
|
30
|
+
|
31
|
+
# Get the rendering status, video url and details of a timeline by ID.
|
32
|
+
# @param id [String] The id of the timeline render task in UUID format
|
33
|
+
# @param [Hash] opts the optional parameters
|
34
|
+
# @return [Array<(RenderResponse, Integer, Hash)>] RenderResponse data, response status code and response headers
|
35
|
+
def get_render_with_http_info(id, opts = {})
|
36
|
+
if @api_client.config.debugging
|
37
|
+
@api_client.config.logger.debug 'Calling API: EndpointsApi.get_render ...'
|
38
|
+
end
|
39
|
+
# verify the required parameter 'id' is set
|
40
|
+
if @api_client.config.client_side_validation && id.nil?
|
41
|
+
fail ArgumentError, "Missing the required parameter 'id' when calling EndpointsApi.get_render"
|
42
|
+
end
|
43
|
+
pattern = Regexp.new(/^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/)
|
44
|
+
if @api_client.config.client_side_validation && id !~ pattern
|
45
|
+
fail ArgumentError, "invalid value for 'id' when calling EndpointsApi.get_render, must conform to the pattern #{pattern}."
|
46
|
+
end
|
47
|
+
|
48
|
+
# resource path
|
49
|
+
local_var_path = '/render/{id}'.sub('{' + 'id' + '}', CGI.escape(id.to_s))
|
50
|
+
|
51
|
+
# query parameters
|
52
|
+
query_params = opts[:query_params] || {}
|
53
|
+
|
54
|
+
# header parameters
|
55
|
+
header_params = opts[:header_params] || {}
|
56
|
+
# HTTP header 'Accept' (if needed)
|
57
|
+
header_params['Accept'] = @api_client.select_header_accept(['application/json'])
|
58
|
+
|
59
|
+
# form parameters
|
60
|
+
form_params = opts[:form_params] || {}
|
61
|
+
|
62
|
+
# http body (model)
|
63
|
+
post_body = opts[:body]
|
64
|
+
|
65
|
+
# return_type
|
66
|
+
return_type = opts[:return_type] || 'RenderResponse'
|
67
|
+
|
68
|
+
# auth_names
|
69
|
+
auth_names = opts[:auth_names] || ['DeveloperKey']
|
70
|
+
|
71
|
+
new_options = opts.merge(
|
72
|
+
:header_params => header_params,
|
73
|
+
:query_params => query_params,
|
74
|
+
:form_params => form_params,
|
75
|
+
:body => post_body,
|
76
|
+
:auth_names => auth_names,
|
77
|
+
:return_type => return_type
|
78
|
+
)
|
79
|
+
|
80
|
+
data, status_code, headers = @api_client.call_api(:GET, local_var_path, new_options)
|
81
|
+
if @api_client.config.debugging
|
82
|
+
@api_client.config.logger.debug "API called: EndpointsApi#get_render\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
|
83
|
+
end
|
84
|
+
return data, status_code, headers
|
85
|
+
end
|
86
|
+
|
87
|
+
# Render the contents of a timeline as a video file.
|
88
|
+
# @param edit [Edit]
|
89
|
+
# @param [Hash] opts the optional parameters
|
90
|
+
# @return [QueuedResponse]
|
91
|
+
def post_render(edit, opts = {})
|
92
|
+
data, _status_code, _headers = post_render_with_http_info(edit, opts)
|
93
|
+
data
|
94
|
+
end
|
95
|
+
|
96
|
+
# Render the contents of a timeline as a video file.
|
97
|
+
# @param edit [Edit]
|
98
|
+
# @param [Hash] opts the optional parameters
|
99
|
+
# @return [Array<(QueuedResponse, Integer, Hash)>] QueuedResponse data, response status code and response headers
|
100
|
+
def post_render_with_http_info(edit, opts = {})
|
101
|
+
if @api_client.config.debugging
|
102
|
+
@api_client.config.logger.debug 'Calling API: EndpointsApi.post_render ...'
|
103
|
+
end
|
104
|
+
# verify the required parameter 'edit' is set
|
105
|
+
if @api_client.config.client_side_validation && edit.nil?
|
106
|
+
fail ArgumentError, "Missing the required parameter 'edit' when calling EndpointsApi.post_render"
|
107
|
+
end
|
108
|
+
# resource path
|
109
|
+
local_var_path = '/render'
|
110
|
+
|
111
|
+
# query parameters
|
112
|
+
query_params = opts[:query_params] || {}
|
113
|
+
|
114
|
+
# header parameters
|
115
|
+
header_params = opts[:header_params] || {}
|
116
|
+
# HTTP header 'Accept' (if needed)
|
117
|
+
header_params['Accept'] = @api_client.select_header_accept(['application/json'])
|
118
|
+
# HTTP header 'Content-Type'
|
119
|
+
header_params['Content-Type'] = @api_client.select_header_content_type(['application/json'])
|
120
|
+
|
121
|
+
# form parameters
|
122
|
+
form_params = opts[:form_params] || {}
|
123
|
+
|
124
|
+
# http body (model)
|
125
|
+
post_body = opts[:body] || @api_client.object_to_http_body(edit)
|
126
|
+
|
127
|
+
# return_type
|
128
|
+
return_type = opts[:return_type] || 'QueuedResponse'
|
129
|
+
|
130
|
+
# auth_names
|
131
|
+
auth_names = opts[:auth_names] || ['DeveloperKey']
|
132
|
+
|
133
|
+
new_options = opts.merge(
|
134
|
+
:header_params => header_params,
|
135
|
+
:query_params => query_params,
|
136
|
+
:form_params => form_params,
|
137
|
+
:body => post_body,
|
138
|
+
:auth_names => auth_names,
|
139
|
+
:return_type => return_type
|
140
|
+
)
|
141
|
+
|
142
|
+
data, status_code, headers = @api_client.call_api(:POST, local_var_path, new_options)
|
143
|
+
if @api_client.config.debugging
|
144
|
+
@api_client.config.logger.debug "API called: EndpointsApi#post_render\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
|
145
|
+
end
|
146
|
+
return data, status_code, headers
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|
data/lib/shotstack/api_client.rb
CHANGED
@@ -1,9 +1,20 @@
|
|
1
|
+
=begin
|
2
|
+
#Shotstack
|
3
|
+
|
4
|
+
#The Shotstack API is a video editing service that allows for the automated creation of videos using JSON. You can configure an edit and POST it to the Shotstack API which will render your video and provide a file location when complete. For more details check https://shotstack.io
|
5
|
+
|
6
|
+
The version of the OpenAPI document: v1
|
7
|
+
|
8
|
+
Generated by: https://openapi-generator.tech
|
9
|
+
OpenAPI Generator version: 4.2.1
|
10
|
+
|
11
|
+
=end
|
12
|
+
|
1
13
|
require 'date'
|
2
14
|
require 'json'
|
3
15
|
require 'logger'
|
4
16
|
require 'tempfile'
|
5
17
|
require 'typhoeus'
|
6
|
-
require 'uri'
|
7
18
|
|
8
19
|
module Shotstack
|
9
20
|
class ApiClient
|
@@ -15,11 +26,13 @@ module Shotstack
|
|
15
26
|
# @return [Hash]
|
16
27
|
attr_accessor :default_headers
|
17
28
|
|
29
|
+
# Initializes the ApiClient
|
30
|
+
# @option config [Configuration] Configuration for initializing the object, default to Configuration.default
|
18
31
|
def initialize(config = Configuration.default)
|
19
32
|
@config = config
|
20
|
-
@user_agent = "
|
33
|
+
@user_agent = "OpenAPI-Generator/#{VERSION}/ruby"
|
21
34
|
@default_headers = {
|
22
|
-
'Content-Type' =>
|
35
|
+
'Content-Type' => 'application/json',
|
23
36
|
'User-Agent' => @user_agent
|
24
37
|
}
|
25
38
|
end
|
@@ -30,7 +43,7 @@ module Shotstack
|
|
30
43
|
|
31
44
|
# Call an API with given options.
|
32
45
|
#
|
33
|
-
# @return [Array<(Object,
|
46
|
+
# @return [Array<(Object, Integer, Hash)>] an array of 3 elements:
|
34
47
|
# the data deserialized from response body (could be nil), response status code and response headers.
|
35
48
|
def call_api(http_method, path, opts = {})
|
36
49
|
request = build_request(http_method, path, opts)
|
@@ -41,10 +54,18 @@ module Shotstack
|
|
41
54
|
end
|
42
55
|
|
43
56
|
unless response.success?
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
57
|
+
if response.timed_out?
|
58
|
+
fail ApiError.new('Connection timed out')
|
59
|
+
elsif response.code == 0
|
60
|
+
# Errors from libcurl will be made visible here
|
61
|
+
fail ApiError.new(:code => 0,
|
62
|
+
:message => response.return_message)
|
63
|
+
else
|
64
|
+
fail ApiError.new(:code => response.code,
|
65
|
+
:response_headers => response.headers,
|
66
|
+
:response_body => response.body),
|
67
|
+
response.status_message
|
68
|
+
end
|
48
69
|
end
|
49
70
|
|
50
71
|
if opts[:return_type]
|
@@ -55,6 +76,15 @@ module Shotstack
|
|
55
76
|
return data, response.code, response.headers
|
56
77
|
end
|
57
78
|
|
79
|
+
# Builds the HTTP request
|
80
|
+
#
|
81
|
+
# @param [String] http_method HTTP method/verb (e.g. POST)
|
82
|
+
# @param [String] path URL path (e.g. /account/new)
|
83
|
+
# @option opts [Hash] :header_params Header parameters
|
84
|
+
# @option opts [Hash] :query_params Query parameters
|
85
|
+
# @option opts [Hash] :form_params Query parameters
|
86
|
+
# @option opts [Object] :body HTTP body (JSON/XML)
|
87
|
+
# @return [Typhoeus::Request] A Typhoeus Request
|
58
88
|
def build_request(http_method, path, opts = {})
|
59
89
|
url = build_request_url(path)
|
60
90
|
http_method = http_method.to_sym.downcase
|
@@ -63,22 +93,27 @@ module Shotstack
|
|
63
93
|
query_params = opts[:query_params] || {}
|
64
94
|
form_params = opts[:form_params] || {}
|
65
95
|
|
66
|
-
|
67
96
|
update_params_for_auth! header_params, query_params, opts[:auth_names]
|
68
|
-
|
97
|
+
|
98
|
+
# set ssl_verifyhosts option based on @config.verify_ssl_host (true/false)
|
99
|
+
_verify_ssl_host = @config.verify_ssl_host ? 2 : 0
|
69
100
|
|
70
101
|
req_opts = {
|
71
102
|
:method => http_method,
|
72
103
|
:headers => header_params,
|
73
104
|
:params => query_params,
|
105
|
+
:params_encoding => @config.params_encoding,
|
74
106
|
:timeout => @config.timeout,
|
75
107
|
:ssl_verifypeer => @config.verify_ssl,
|
108
|
+
:ssl_verifyhost => _verify_ssl_host,
|
76
109
|
:sslcert => @config.cert_file,
|
77
110
|
:sslkey => @config.key_file,
|
78
|
-
:cainfo => @config.ssl_ca_cert,
|
79
111
|
:verbose => @config.debugging
|
80
112
|
}
|
81
113
|
|
114
|
+
# set custom cert, if provided
|
115
|
+
req_opts[:cainfo] = @config.ssl_ca_cert if @config.ssl_ca_cert
|
116
|
+
|
82
117
|
if [:post, :patch, :put, :delete].include?(http_method)
|
83
118
|
req_body = build_request_body(header_params, form_params, opts[:body])
|
84
119
|
req_opts.update :body => req_body
|
@@ -87,7 +122,37 @@ module Shotstack
|
|
87
122
|
end
|
88
123
|
end
|
89
124
|
|
90
|
-
Typhoeus::Request.new(url, req_opts)
|
125
|
+
request = Typhoeus::Request.new(url, req_opts)
|
126
|
+
download_file(request) if opts[:return_type] == 'File'
|
127
|
+
request
|
128
|
+
end
|
129
|
+
|
130
|
+
# Builds the HTTP request body
|
131
|
+
#
|
132
|
+
# @param [Hash] header_params Header parameters
|
133
|
+
# @param [Hash] form_params Query parameters
|
134
|
+
# @param [Object] body HTTP body (JSON/XML)
|
135
|
+
# @return [String] HTTP body data in the form of string
|
136
|
+
def build_request_body(header_params, form_params, body)
|
137
|
+
# http form
|
138
|
+
if header_params['Content-Type'] == 'application/x-www-form-urlencoded' ||
|
139
|
+
header_params['Content-Type'] == 'multipart/form-data'
|
140
|
+
data = {}
|
141
|
+
form_params.each do |key, value|
|
142
|
+
case value
|
143
|
+
when ::File, ::Array, nil
|
144
|
+
# let typhoeus handle File, Array and nil parameters
|
145
|
+
data[key] = value
|
146
|
+
else
|
147
|
+
data[key] = value.to_s
|
148
|
+
end
|
149
|
+
end
|
150
|
+
elsif body
|
151
|
+
data = body.is_a?(String) ? body : body.to_json
|
152
|
+
else
|
153
|
+
data = nil
|
154
|
+
end
|
155
|
+
data
|
91
156
|
end
|
92
157
|
|
93
158
|
# Check if the given MIME is a JSON MIME.
|
@@ -95,19 +160,28 @@ module Shotstack
|
|
95
160
|
# application/json
|
96
161
|
# application/json; charset=UTF8
|
97
162
|
# APPLICATION/JSON
|
163
|
+
# */*
|
164
|
+
# @param [String] mime MIME
|
165
|
+
# @return [Boolean] True if the MIME is application/json
|
98
166
|
def json_mime?(mime)
|
99
|
-
|
167
|
+
(mime == '*/*') || !(mime =~ /Application\/.*json(?!p)(;.*)?/i).nil?
|
100
168
|
end
|
101
169
|
|
102
170
|
# Deserialize the response to the given return type.
|
103
171
|
#
|
104
|
-
# @param [
|
172
|
+
# @param [Response] response HTTP response
|
173
|
+
# @param [String] return_type some examples: "User", "Array<User>", "Hash<String, Integer>"
|
105
174
|
def deserialize(response, return_type)
|
106
175
|
body = response.body
|
176
|
+
|
177
|
+
# handle file downloading - return the File instance processed in request callbacks
|
178
|
+
# note that response body is empty when the file is written in chunks in request on_body callback
|
179
|
+
return @tempfile if return_type == 'File'
|
180
|
+
|
107
181
|
return nil if body.nil? || body.empty?
|
108
182
|
|
109
|
-
#
|
110
|
-
return
|
183
|
+
# return response body directly for String return type
|
184
|
+
return body if return_type == 'String'
|
111
185
|
|
112
186
|
# ensuring a default content type
|
113
187
|
content_type = response.headers['Content-Type'] || 'application/json'
|
@@ -128,6 +202,9 @@ module Shotstack
|
|
128
202
|
end
|
129
203
|
|
130
204
|
# Convert data to the given return type.
|
205
|
+
# @param [Object] data Data to be converted
|
206
|
+
# @param [String] return_type Return type
|
207
|
+
# @return [Mixed] Data in a particular type
|
131
208
|
def convert_to_type(data, return_type)
|
132
209
|
return nil if data.nil?
|
133
210
|
case return_type
|
@@ -137,7 +214,7 @@ module Shotstack
|
|
137
214
|
data.to_i
|
138
215
|
when 'Float'
|
139
216
|
data.to_f
|
140
|
-
when '
|
217
|
+
when 'Boolean'
|
141
218
|
data == true
|
142
219
|
when 'DateTime'
|
143
220
|
# parse date time (expecting ISO 8601 format)
|
@@ -146,52 +223,58 @@ module Shotstack
|
|
146
223
|
# parse date time (expecting ISO 8601 format)
|
147
224
|
Date.parse data
|
148
225
|
when 'Object'
|
149
|
-
# generic object, return directly
|
226
|
+
# generic object (usually a Hash), return directly
|
150
227
|
data
|
151
228
|
when /\AArray<(.+)>\z/
|
152
229
|
# e.g. Array<Pet>
|
153
230
|
sub_type = $1
|
154
|
-
data.map {|item| convert_to_type(item, sub_type) }
|
231
|
+
data.map { |item| convert_to_type(item, sub_type) }
|
155
232
|
when /\AHash\<String, (.+)\>\z/
|
156
233
|
# e.g. Hash<String, Integer>
|
157
234
|
sub_type = $1
|
158
235
|
{}.tap do |hash|
|
159
|
-
data.each {|k, v| hash[k] = convert_to_type(v, sub_type) }
|
236
|
+
data.each { |k, v| hash[k] = convert_to_type(v, sub_type) }
|
160
237
|
end
|
161
238
|
else
|
162
239
|
# models, e.g. Pet
|
163
|
-
Shotstack.const_get(return_type).
|
164
|
-
model.build_from_hash data
|
165
|
-
end
|
240
|
+
Shotstack.const_get(return_type).build_from_hash(data)
|
166
241
|
end
|
167
242
|
end
|
168
243
|
|
169
244
|
# Save response body into a file in (the defined) temporary folder, using the filename
|
170
245
|
# from the "Content-Disposition" header if provided, otherwise a random filename.
|
246
|
+
# The response body is written to the file in chunks in order to handle files which
|
247
|
+
# size is larger than maximum Ruby String or even larger than the maximum memory a Ruby
|
248
|
+
# process can use.
|
171
249
|
#
|
172
250
|
# @see Configuration#temp_folder_path
|
173
|
-
|
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
|
-
|
251
|
+
def download_file(request)
|
184
252
|
tempfile = nil
|
185
|
-
encoding =
|
186
|
-
|
187
|
-
|
188
|
-
|
253
|
+
encoding = nil
|
254
|
+
request.on_headers do |response|
|
255
|
+
content_disposition = response.headers['Content-Disposition']
|
256
|
+
if content_disposition && content_disposition =~ /filename=/i
|
257
|
+
filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1]
|
258
|
+
prefix = sanitize_filename(filename)
|
259
|
+
else
|
260
|
+
prefix = 'download-'
|
261
|
+
end
|
262
|
+
prefix = prefix + '-' unless prefix.end_with?('-')
|
263
|
+
encoding = response.body.encoding
|
264
|
+
tempfile = Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding)
|
265
|
+
@tempfile = tempfile
|
266
|
+
end
|
267
|
+
request.on_body do |chunk|
|
268
|
+
chunk.force_encoding(encoding)
|
269
|
+
tempfile.write(chunk)
|
270
|
+
end
|
271
|
+
request.on_complete do |response|
|
272
|
+
tempfile.close if tempfile
|
273
|
+
@config.logger.info "Temp file written to #{tempfile.path}, please copy the file to a proper folder "\
|
274
|
+
"with e.g. `FileUtils.cp(tempfile.path, '/new/file/path')` otherwise the temp file "\
|
275
|
+
"will be deleted automatically with GC. It's also recommended to delete the temp file "\
|
276
|
+
"explicitly with `tempfile.delete`"
|
189
277
|
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
278
|
end
|
196
279
|
|
197
280
|
# Sanitize filename by removing path.
|
@@ -200,38 +283,20 @@ module Shotstack
|
|
200
283
|
# @param [String] filename the filename to be sanitized
|
201
284
|
# @return [String] the sanitized filename
|
202
285
|
def sanitize_filename(filename)
|
203
|
-
filename.gsub
|
286
|
+
filename.gsub(/.*[\/\\]/, '')
|
204
287
|
end
|
205
288
|
|
206
289
|
def build_request_url(path)
|
207
290
|
# Add leading and trailing slashes to path
|
208
291
|
path = "/#{path}".gsub(/\/+/, '/')
|
209
|
-
|
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
|
292
|
+
@config.base_url + path
|
232
293
|
end
|
233
294
|
|
234
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
|
235
300
|
def update_params_for_auth!(header_params, query_params, auth_names)
|
236
301
|
Array(auth_names).each do |auth_name|
|
237
302
|
auth_setting = @config.auth_settings[auth_name]
|
@@ -244,6 +309,9 @@ module Shotstack
|
|
244
309
|
end
|
245
310
|
end
|
246
311
|
|
312
|
+
# Sets user agent in HTTP header
|
313
|
+
#
|
314
|
+
# @param [String] user_agent User agent (e.g. openapi-generator/ruby/1.0.0)
|
247
315
|
def user_agent=(user_agent)
|
248
316
|
@user_agent = user_agent
|
249
317
|
@default_headers['User-Agent'] = @user_agent
|
@@ -256,7 +324,7 @@ module Shotstack
|
|
256
324
|
return nil if accepts.nil? || accepts.empty?
|
257
325
|
# use JSON when present, otherwise use all of the provided
|
258
326
|
json_accept = accepts.find { |s| json_mime?(s) }
|
259
|
-
|
327
|
+
json_accept || accepts.join(',')
|
260
328
|
end
|
261
329
|
|
262
330
|
# Return Content-Type header based on an array of content types provided.
|
@@ -267,21 +335,21 @@ module Shotstack
|
|
267
335
|
return 'application/json' if content_types.nil? || content_types.empty?
|
268
336
|
# use JSON when present, otherwise use the first one
|
269
337
|
json_content_type = content_types.find { |s| json_mime?(s) }
|
270
|
-
|
338
|
+
json_content_type || content_types.first
|
271
339
|
end
|
272
340
|
|
273
341
|
# Convert object (array, hash, object, etc) to JSON string.
|
274
342
|
# @param [Object] model object to be converted into JSON string
|
275
343
|
# @return [String] JSON string representation of the object
|
276
344
|
def object_to_http_body(model)
|
277
|
-
return if model.nil?
|
278
|
-
|
345
|
+
return model if model.nil? || model.is_a?(String)
|
346
|
+
local_body = nil
|
279
347
|
if model.is_a?(Array)
|
280
|
-
|
348
|
+
local_body = model.map { |m| object_to_hash(m) }
|
281
349
|
else
|
282
|
-
|
350
|
+
local_body = object_to_hash(model)
|
283
351
|
end
|
284
|
-
|
352
|
+
local_body.to_json
|
285
353
|
end
|
286
354
|
|
287
355
|
# Convert object(non-array) to hash.
|