ubiquity-iconik 1.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.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.travis.yml +5 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +41 -0
- data/Rakefile +10 -0
- data/bin/console +17 -0
- data/bin/setup +8 -0
- data/exe/ubiquity-iconik +3 -0
- data/lib/ubiquity/iconik.rb +7 -0
- data/lib/ubiquity/iconik/api/client.rb +708 -0
- data/lib/ubiquity/iconik/api/client/http_client.rb +333 -0
- data/lib/ubiquity/iconik/api/client/paginator.rb +134 -0
- data/lib/ubiquity/iconik/api/client/requests.rb +2 -0
- data/lib/ubiquity/iconik/api/client/requests/base_request.rb +270 -0
- data/lib/ubiquity/iconik/api/utilities.rb +174 -0
- data/lib/ubiquity/iconik/version.rb +5 -0
- data/ubiquity-iconik.gemspec +27 -0
- metadata +105 -0
@@ -0,0 +1,270 @@
|
|
1
|
+
require 'cgi'
|
2
|
+
require 'uri'
|
3
|
+
|
4
|
+
module Ubiquity
|
5
|
+
module Iconik
|
6
|
+
module API
|
7
|
+
class Client
|
8
|
+
module Requests
|
9
|
+
|
10
|
+
class BaseRequest
|
11
|
+
|
12
|
+
HTTP_METHOD = :get
|
13
|
+
HTTP_BASE_PATH = '/API/'
|
14
|
+
HTTP_PATH = ''
|
15
|
+
HTTP_SUCCESS_CODE = '200'
|
16
|
+
|
17
|
+
DEFAULT_PARAMETER_SEND_IN_VALUE = :body
|
18
|
+
|
19
|
+
PARAMETERS = [ ]
|
20
|
+
|
21
|
+
attr_accessor :client, :arguments, :options, :initial_arguments, :initial_options, :missing_required_arguments,
|
22
|
+
:default_parameter_send_in_value, :processed_parameters, :initialized, :response
|
23
|
+
|
24
|
+
attr_writer :parameters, :path, :body, :query
|
25
|
+
|
26
|
+
def self.normalize_argument_hash_keys(hash)
|
27
|
+
return hash unless hash.is_a?(Hash)
|
28
|
+
Hash[ hash.dup.map { |k,v| [ normalize_parameter_name(k), v ] } ]
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.normalize_parameter_name(name)
|
32
|
+
(name || '').respond_to?(:to_s) ? name.to_s.gsub('_', '').gsub('-', '').downcase : name
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.process_parameters(params, args, options = { })
|
36
|
+
args = normalize_argument_hash_keys(args) || { }
|
37
|
+
args_out = options[:arguments_out] || { }
|
38
|
+
default_parameter_send_in_value = options[:default_parameter_send_in_value] || DEFAULT_PARAMETER_SEND_IN_VALUE
|
39
|
+
processed_parameters = options[:processed_parameters] || { }
|
40
|
+
missing_required_arguments = options[:missing_required_arguments] || [ ]
|
41
|
+
|
42
|
+
params.each do |param|
|
43
|
+
process_parameter(param, args, args_out, missing_required_arguments, processed_parameters, default_parameter_send_in_value)
|
44
|
+
end
|
45
|
+
{ :arguments_out => args_out, :processed_parameters => processed_parameters, :missing_required_arguments => missing_required_arguments }
|
46
|
+
end
|
47
|
+
|
48
|
+
# A method to expose parameter processing
|
49
|
+
#
|
50
|
+
# @param [Hash|Symbol] param The parameter to process
|
51
|
+
# @param [Hash] args ({ }) Arguments to possibly match to the parameter
|
52
|
+
# @param [Hash] args_out ({ }) The processed value of the parameter (if any)
|
53
|
+
# @param [Array] missing_required_arguments ([ ]) If the parameter was required and no argument found then it
|
54
|
+
# will be placed into this array
|
55
|
+
# @param [Hash] processed_parameters ({ }) The parameter will be placed into this array once processed
|
56
|
+
# @param [Symbol] default_parameter_send_in_value (DEFAULT_PARAMETER_SEND_IN_VALUE) The :send_in value that
|
57
|
+
# will be set if the :send_in key is not found
|
58
|
+
# @param [Hash] options
|
59
|
+
# @option options [True|False] :normalize_argument_hash_keys (false)
|
60
|
+
def self.process_parameter(param, args = { }, args_out = { }, missing_required_arguments = [ ], processed_parameters = { }, default_parameter_send_in_value = DEFAULT_PARAMETER_SEND_IN_VALUE, options = { })
|
61
|
+
args = normalize_argument_hash_keys(args) || { } if options.fetch(:normalize_argument_hash_keys, false)
|
62
|
+
|
63
|
+
_k = param.is_a?(Hash) ? param : { :name => param, :required => false, :send_in => default_parameter_send_in_value }
|
64
|
+
_k[:send_in] ||= default_parameter_send_in_value
|
65
|
+
|
66
|
+
proper_parameter_name = _k[:name]
|
67
|
+
param_name = normalize_parameter_name(proper_parameter_name)
|
68
|
+
arg_key = (has_key = args.has_key?(param_name)) ?
|
69
|
+
param_name :
|
70
|
+
( (_k[:aliases] || [ ]).map { |a| normalize_parameter_name(a) }.find { |a| has_key = args.has_key?(a) } || param_name )
|
71
|
+
|
72
|
+
value = has_key ? args[arg_key] : _k[:default_value]
|
73
|
+
is_set = has_key || _k.has_key?(:default_value)
|
74
|
+
|
75
|
+
processed_parameters[proper_parameter_name] = _k.merge(:value => value, :is_set => is_set)
|
76
|
+
|
77
|
+
unless is_set
|
78
|
+
missing_required_arguments << proper_parameter_name if _k[:required]
|
79
|
+
else
|
80
|
+
args_out[proper_parameter_name] = value
|
81
|
+
end
|
82
|
+
|
83
|
+
{ :arguments_out => args_out, :processed_parameters => processed_parameters, :missing_required_arguments => missing_required_arguments }
|
84
|
+
end
|
85
|
+
|
86
|
+
def initialize(args = { }, options = { })
|
87
|
+
@initial_arguments = args.dup
|
88
|
+
@initial_options = options.dup
|
89
|
+
|
90
|
+
@options = options.dup
|
91
|
+
|
92
|
+
initialize_attributes if options.fetch(:initialize_attributes, true)
|
93
|
+
after_initialize
|
94
|
+
end
|
95
|
+
|
96
|
+
def after_initialize
|
97
|
+
process_parameters if initialized
|
98
|
+
end
|
99
|
+
|
100
|
+
def initialize_attributes
|
101
|
+
@client = options[:client]
|
102
|
+
@missing_required_arguments = [ ]
|
103
|
+
@default_parameter_send_in_value = options[:default_parameter_send_in_value] || self.class::DEFAULT_PARAMETER_SEND_IN_VALUE
|
104
|
+
@processed_parameters = { }
|
105
|
+
@arguments = { }
|
106
|
+
@eval_http_path = options.fetch(:eval_http_path, true)
|
107
|
+
@base_path = options[:base_path]
|
108
|
+
|
109
|
+
@parameters = options[:parameters]
|
110
|
+
@http_method = options[:http_method]
|
111
|
+
@http_path = options[:http_path] ||= options[:path_raw]
|
112
|
+
@http_success_code = options[:http_success_code] ||= HTTP_SUCCESS_CODE
|
113
|
+
|
114
|
+
@path = options[:path]
|
115
|
+
@path_arguments = nil
|
116
|
+
|
117
|
+
@query = options[:query]
|
118
|
+
@query_arguments = nil
|
119
|
+
|
120
|
+
@body = options[:body]
|
121
|
+
@body_arguments = nil
|
122
|
+
|
123
|
+
@relative_path = nil
|
124
|
+
|
125
|
+
@response = nil
|
126
|
+
|
127
|
+
@initialized = true
|
128
|
+
end
|
129
|
+
|
130
|
+
def process_parameters(params = parameters, args = @initial_arguments, options = @options)
|
131
|
+
|
132
|
+
before_process_parameters unless options.fetch(:skip_before_process_parameters, false)
|
133
|
+
self.class.process_parameters(params, args, options.merge(:processed_parameters => processed_parameters, :missing_required_arguments => missing_required_arguments, :default_parameter_send_in_value => default_parameter_send_in_value, :arguments_out => arguments))
|
134
|
+
after_process_parameters unless options.fetch(:skip_after_process_parameters, false)
|
135
|
+
|
136
|
+
end
|
137
|
+
|
138
|
+
def before_process_parameters
|
139
|
+
# TO BE IMPLEMENTED IN CHILD CLASS
|
140
|
+
end
|
141
|
+
|
142
|
+
def after_process_parameters
|
143
|
+
# TO BE IMPLEMENTED IN CHILD CLASS
|
144
|
+
end
|
145
|
+
|
146
|
+
# @!group Attribute Readers
|
147
|
+
|
148
|
+
def http_success_code
|
149
|
+
@http_success_code
|
150
|
+
end
|
151
|
+
|
152
|
+
def arguments
|
153
|
+
@arguments ||= { }
|
154
|
+
end
|
155
|
+
|
156
|
+
def base_path
|
157
|
+
@base_path ||= self.class::HTTP_BASE_PATH
|
158
|
+
end
|
159
|
+
|
160
|
+
def body_arguments
|
161
|
+
@body_arguments ||= arguments.dup.delete_if { |k,_| processed_parameters[k][:send_in] != :body }
|
162
|
+
end
|
163
|
+
|
164
|
+
def body
|
165
|
+
# body_arguments.empty? ? @body : body_arguments
|
166
|
+
|
167
|
+
_body = @body
|
168
|
+
_body_arguments = body_arguments
|
169
|
+
return _body unless _body_arguments
|
170
|
+
if _body.is_a?(Hash) && _body_arguments.is_a?(Hash)
|
171
|
+
return _body.merge(_body_arguments)
|
172
|
+
end
|
173
|
+
_body_arguments
|
174
|
+
end
|
175
|
+
|
176
|
+
def client
|
177
|
+
@client ||= options[:client]
|
178
|
+
end
|
179
|
+
|
180
|
+
def logger
|
181
|
+
@logger ||= client.logger
|
182
|
+
end
|
183
|
+
|
184
|
+
def eval_http_path?
|
185
|
+
@eval_http_path
|
186
|
+
end
|
187
|
+
|
188
|
+
def http_path
|
189
|
+
@http_path ||= self.class::HTTP_PATH.dup
|
190
|
+
end
|
191
|
+
|
192
|
+
def http_method
|
193
|
+
@http_method ||= self.class::HTTP_METHOD
|
194
|
+
end
|
195
|
+
|
196
|
+
def parameters
|
197
|
+
@parameters ||= self.class::PARAMETERS.dup
|
198
|
+
end
|
199
|
+
|
200
|
+
def relative_path
|
201
|
+
@relative_path ||= (path.start_with?('/') ? path[1..-1] : path)
|
202
|
+
end
|
203
|
+
|
204
|
+
# The URI Path
|
205
|
+
def path
|
206
|
+
@path ||= begin
|
207
|
+
_path = File.join(base_path, (eval_http_path? ? eval(%("#{http_path}"), binding, __FILE__, __LINE__) : http_path))
|
208
|
+
_path.concat('/') if !_path.end_with?('/') && http_path.end_with?('/')
|
209
|
+
_path
|
210
|
+
end
|
211
|
+
end
|
212
|
+
|
213
|
+
def path_arguments
|
214
|
+
@path_arguments ||= Hash[
|
215
|
+
arguments.dup.delete_if { |k, _| processed_parameters[k][:send_in] != :path }.
|
216
|
+
map { |k,v| [ k, CGI.escape(v.respond_to?(:to_s) ? v.to_s : '').gsub('+', '%20') ] }
|
217
|
+
]
|
218
|
+
end
|
219
|
+
|
220
|
+
def query
|
221
|
+
@query ||= begin
|
222
|
+
query_arguments.is_a?(Hash) ? query_arguments.map { |k,v| "#{CGI.escape(k.to_s).gsub('+', '%20')}=#{CGI.escape(v.respond_to?(:to_s) ? v.to_s : v).gsub('+', '%20')}" }.join('&') : query_arguments
|
223
|
+
end
|
224
|
+
end
|
225
|
+
|
226
|
+
def query_arguments
|
227
|
+
@query_arguments ||= arguments.dup.delete_if { |k,_| processed_parameters[k][:send_in] != :query }
|
228
|
+
end
|
229
|
+
|
230
|
+
def uri_request_path
|
231
|
+
[ path ].concat( [*query].delete_if { |v| v.respond_to?(:empty?) and v.empty? } ).join('?')
|
232
|
+
end
|
233
|
+
|
234
|
+
def http_client
|
235
|
+
client.http_client
|
236
|
+
end
|
237
|
+
|
238
|
+
def http_response
|
239
|
+
@http_response ||= http_client.response.dup rescue nil
|
240
|
+
end
|
241
|
+
|
242
|
+
def execute
|
243
|
+
@response = http_client.call_method(http_method, { :path => path, :query => query, :body => body }, options) if client
|
244
|
+
end
|
245
|
+
|
246
|
+
def success?
|
247
|
+
_response = client.http_client.response
|
248
|
+
_response && ( http_success_code.is_a?(Array) ?
|
249
|
+
http_success_code.include?(_response.code) :
|
250
|
+
http_success_code == _response.code )
|
251
|
+
end
|
252
|
+
|
253
|
+
# @!endgroup
|
254
|
+
|
255
|
+
end
|
256
|
+
|
257
|
+
# Requests
|
258
|
+
end
|
259
|
+
|
260
|
+
# Client
|
261
|
+
end
|
262
|
+
|
263
|
+
# API
|
264
|
+
end
|
265
|
+
|
266
|
+
# Iconik
|
267
|
+
end
|
268
|
+
|
269
|
+
# Ubiquity
|
270
|
+
end
|
@@ -0,0 +1,174 @@
|
|
1
|
+
require 'ubiquity/iconik/api/client'
|
2
|
+
|
3
|
+
module Ubiquity
|
4
|
+
module Iconik
|
5
|
+
module API
|
6
|
+
class Utilities < Client
|
7
|
+
|
8
|
+
# Converts hash keys to symbols
|
9
|
+
#
|
10
|
+
# @param [Hash] value hash
|
11
|
+
# @param [Boolean] recursive Will recurse into any values that are hashes or arrays
|
12
|
+
def symbolize_keys (value, recursive = true)
|
13
|
+
case value
|
14
|
+
when Hash
|
15
|
+
new_val = {}
|
16
|
+
value.each { |k, v|
|
17
|
+
k = (k.to_sym rescue k)
|
18
|
+
v = symbolize_keys(v, true) if recursive and (v.is_a? Hash or v.is_a? Array)
|
19
|
+
new_val[k] = v
|
20
|
+
}
|
21
|
+
return new_val
|
22
|
+
when Array
|
23
|
+
return value.map { |v| symbolize_keys(v, true) }
|
24
|
+
else
|
25
|
+
return value
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
# @param [Object] args
|
30
|
+
# @option args [Boolean] :create_keyframes (true)
|
31
|
+
# @option args [String] :file_path
|
32
|
+
# @option args [Numeric|String] :file_size (1024)
|
33
|
+
# @option args [String] :file_status ('CLOSED')
|
34
|
+
# @option args [Hash] :metadata
|
35
|
+
# @option args [String] :storage_id
|
36
|
+
# @param [Object] options
|
37
|
+
# @return [Hash]
|
38
|
+
def asset_add_using_file_path(args = {}, options = {})
|
39
|
+
_args = symbolize_keys(args, false)
|
40
|
+
|
41
|
+
create_keyframes = _args.fetch(:create_keyframes, true)
|
42
|
+
|
43
|
+
file_path = _args[:file_path]
|
44
|
+
raise ArgumentError, ':file_path is a required argument.' unless file_path
|
45
|
+
|
46
|
+
file_dir = File.dirname(file_path)
|
47
|
+
if file_dir == '.'
|
48
|
+
file_dir = ''
|
49
|
+
elsif file_dir.start_with?('./')
|
50
|
+
file_dir = file_dir[2..-1]
|
51
|
+
elsif file_dir.start_with?('/')
|
52
|
+
file_dir = file_dir[1..-1]
|
53
|
+
end
|
54
|
+
|
55
|
+
file_name = File.basename(file_path)
|
56
|
+
|
57
|
+
file_size = _args[:file_size] || 1024
|
58
|
+
file_type = 'FILE'
|
59
|
+
metadata = _args[:metadata]
|
60
|
+
|
61
|
+
# Determine Storage
|
62
|
+
storage_id = _args[:storage_id]
|
63
|
+
raise ArgumentError, ':storage_id is a required argument.' unless storage_id
|
64
|
+
|
65
|
+
asset_id = _args[:asset_id]
|
66
|
+
unless asset_id
|
67
|
+
# Create Asset
|
68
|
+
asset_create_args = {
|
69
|
+
:title => file_name
|
70
|
+
}
|
71
|
+
asset = asset_create(asset_create_args)
|
72
|
+
asset_id = asset['id']
|
73
|
+
raise 'Error Creating Asset.' unless asset_id
|
74
|
+
end
|
75
|
+
|
76
|
+
|
77
|
+
format_id = _args[:format_id]
|
78
|
+
unless format_id
|
79
|
+
# Create Asset Format
|
80
|
+
asset_format_create_args = {
|
81
|
+
:asset_id => asset_id
|
82
|
+
}
|
83
|
+
format = asset_format_create(asset_format_create_args)
|
84
|
+
format_id = format['id']
|
85
|
+
raise 'Error Creating Format.' unless format_id
|
86
|
+
end
|
87
|
+
|
88
|
+
file_set_id = _args[:file_set_id]
|
89
|
+
unless file_set_id
|
90
|
+
# Create Asset File Set
|
91
|
+
asset_file_set_create_args = {
|
92
|
+
:asset_id => asset_id,
|
93
|
+
:storage_id => storage_id,
|
94
|
+
:format_id => format_id,
|
95
|
+
|
96
|
+
:name => file_name,
|
97
|
+
:base_dir => file_dir,
|
98
|
+
:component_ids => [],
|
99
|
+
}
|
100
|
+
file_set = asset_file_set_create(asset_file_set_create_args)
|
101
|
+
file_set_id = file_set['id']
|
102
|
+
raise 'Error Creating File Set.' unless file_set_id
|
103
|
+
end
|
104
|
+
|
105
|
+
# Create Asset File
|
106
|
+
file_status = _args[:file_status] || 'CLOSED'
|
107
|
+
file_create_and_modify_time = Time.now.to_s
|
108
|
+
asset_file_create_args = {
|
109
|
+
:asset_id => asset_id,
|
110
|
+
:storage_id => storage_id,
|
111
|
+
:format_id => format_id,
|
112
|
+
:file_set_id => file_set_id,
|
113
|
+
|
114
|
+
:name => file_name,
|
115
|
+
:original_name => file_name,
|
116
|
+
:directory_path => file_dir,
|
117
|
+
:size => file_size,
|
118
|
+
:type => file_type,
|
119
|
+
:status => file_status,
|
120
|
+
|
121
|
+
:file_date_created => file_create_and_modify_time,
|
122
|
+
:file_date_modified => file_create_and_modify_time,
|
123
|
+
|
124
|
+
:metadata => {},
|
125
|
+
}
|
126
|
+
file = asset_file_create(asset_file_create_args)
|
127
|
+
file_id = file['id'] if file.is_a?(Hash)
|
128
|
+
raise "Error Creating File. #{file}" unless file && file_id
|
129
|
+
|
130
|
+
asset_file_keyframes_create(:asset_id => asset_id, :file_id => file_id) if create_keyframes
|
131
|
+
|
132
|
+
if metadata
|
133
|
+
unless metadata.empty?
|
134
|
+
metadata[:asset_id] = asset_id
|
135
|
+
metadata_set = asset_metadata_set(metadata)
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
{ :asset => asset, :format => format, :file_set => file_set, :file => file, :metadata_set => metadata_set }
|
140
|
+
end
|
141
|
+
|
142
|
+
# @param [Object] args
|
143
|
+
# @param [Object] options
|
144
|
+
# @return [Hash]
|
145
|
+
def asset_metadata_set_extended(args = {}, options = {})
|
146
|
+
_args = symbolize_keys(args, false)
|
147
|
+
asset_id = _args[:asset_id]
|
148
|
+
default_view_id = _args[:view_id]
|
149
|
+
metadata = _args[:metadata]
|
150
|
+
|
151
|
+
responses = {}
|
152
|
+
metadata.each do |view|
|
153
|
+
values_out = {}
|
154
|
+
view_id = view['id'] || view[:id] || default_view_id
|
155
|
+
values = view['metadata_values'] || view[:metadata_values]
|
156
|
+
values.echo do |k, v|
|
157
|
+
values_out[k] = { :field_values => [v] }
|
158
|
+
end
|
159
|
+
args_out = {
|
160
|
+
:asset_id => asset_id,
|
161
|
+
:view_id => view_id,
|
162
|
+
:metadata_values => values_out
|
163
|
+
}
|
164
|
+
r = asset_metadata_set(args_out, options)
|
165
|
+
responses[view_id] = r
|
166
|
+
end
|
167
|
+
|
168
|
+
{ :responses => responses }
|
169
|
+
end
|
170
|
+
|
171
|
+
end
|
172
|
+
end
|
173
|
+
end
|
174
|
+
end
|