wsc_sdk 1.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.circleci/config.yml +146 -0
- data/.circleci/docker/build/Dockerfile +3 -0
- data/.circleci/helpers/add_preamble.rb +29 -0
- data/.circleci/helpers/build_docs.sh +5 -0
- data/.circleci/helpers/build_gem.sh +5 -0
- data/.circleci/helpers/debundle-context.sh +14 -0
- data/.circleci/helpers/finalize_release.sh +24 -0
- data/.circleci/helpers/publish_docs.sh +10 -0
- data/.circleci/helpers/release_gem.sh +8 -0
- data/.circleci/helpers/setup.sh +11 -0
- data/.circleci/helpers/setup_aws.sh +13 -0
- data/.circleci/helpers/setup_gem.sh +2 -0
- data/.circleci/helpers/setup_git.sh +27 -0
- data/.circleci/helpers/setup_rubygems.sh +15 -0
- data/.circleci/helpers/test_integration.sh +12 -0
- data/.circleci/helpers/test_unit.sh +6 -0
- data/.circleci/jobs/publish-production.sh +17 -0
- data/.circleci/jobs/publish-staging.sh +17 -0
- data/.circleci/jobs/release-production.sh +21 -0
- data/.circleci/jobs/test-integration.sh +9 -0
- data/.circleci/jobs/test-unit.sh +9 -0
- data/.circleci/tag_repo.rb +46 -0
- data/.gitignore +28 -0
- data/.rspec +2 -0
- data/.yard/config +2 -0
- data/.yard/template/default/fulldoc/html/css/custom.css +151 -0
- data/.yard/template/default/layout/html/footer.erb +0 -0
- data/.yard/template/default/layout/html/headers.erb +10 -0
- data/.yard/template/default/layout/html/layout.erb +45 -0
- data/.yard/template/default/layout/html/setup.rb +24 -0
- data/.yardopts +1 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +99 -0
- data/LICENSE.txt +28 -0
- data/README.md +397 -0
- data/Rakefile +2 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/doc/Hash.html +291 -0
- data/doc/WscSdk.html +570 -0
- data/doc/WscSdk/ApiResponse.html +242 -0
- data/doc/WscSdk/Attributes.html +128 -0
- data/doc/WscSdk/Attributes/Mode.html +191 -0
- data/doc/WscSdk/Attributes/Type.html +389 -0
- data/doc/WscSdk/Client.html +2677 -0
- data/doc/WscSdk/Configuration.html +595 -0
- data/doc/WscSdk/Endpoint.html +2974 -0
- data/doc/WscSdk/Endpoints.html +141 -0
- data/doc/WscSdk/Endpoints/CustomStreamTargets.html +383 -0
- data/doc/WscSdk/Endpoints/LiveStreams.html +197 -0
- data/doc/WscSdk/Endpoints/OutputStreamTargets.html +180 -0
- data/doc/WscSdk/Endpoints/Outputs.html +180 -0
- data/doc/WscSdk/Endpoints/StreamTargets.html +446 -0
- data/doc/WscSdk/Endpoints/Transcoders.html +197 -0
- data/doc/WscSdk/Endpoints/UllStreamTargets.html +383 -0
- data/doc/WscSdk/Endpoints/WowzaStreamTargets.html +383 -0
- data/doc/WscSdk/Enum.html +225 -0
- data/doc/WscSdk/Enums.html +321 -0
- data/doc/WscSdk/Enums/BillingMode.html +188 -0
- data/doc/WscSdk/Enums/BroadcastLocation.html +396 -0
- data/doc/WscSdk/Enums/BufferSize.html +301 -0
- data/doc/WscSdk/Enums/ClosedCaptionType.html +221 -0
- data/doc/WscSdk/Enums/CustomProvider.html +252 -0
- data/doc/WscSdk/Enums/DeliveryMethod.html +188 -0
- data/doc/WscSdk/Enums/DeliveryType.html +188 -0
- data/doc/WscSdk/Enums/Encoder.html +460 -0
- data/doc/WscSdk/Enums/Errors.html +555 -0
- data/doc/WscSdk/Enums/IdleTimeout.html +398 -0
- data/doc/WscSdk/Enums/ImagePosition.html +220 -0
- data/doc/WscSdk/Enums/ModelMode.html +175 -0
- data/doc/WscSdk/Enums/ModelType.html +373 -0
- data/doc/WscSdk/Enums/PlayerType.html +188 -0
- data/doc/WscSdk/Enums/Protocol.html +204 -0
- data/doc/WscSdk/Enums/TargetDeliveryProtocol.html +188 -0
- data/doc/WscSdk/Enums/TranscoderType.html +188 -0
- data/doc/WscSdk/Enums/UserRegion.html +383 -0
- data/doc/WscSdk/Enums/WowzaProvider.html +188 -0
- data/doc/WscSdk/Errors.html +683 -0
- data/doc/WscSdk/Loggable.html +346 -0
- data/doc/WscSdk/Model.html +3316 -0
- data/doc/WscSdk/ModelList.html +442 -0
- data/doc/WscSdk/ModelTemplate.html +267 -0
- data/doc/WscSdk/Models.html +141 -0
- data/doc/WscSdk/Models/CustomStreamTarget.html +192 -0
- data/doc/WscSdk/Models/Error.html +351 -0
- data/doc/WscSdk/Models/LiveStream.html +601 -0
- data/doc/WscSdk/Models/LiveStreamConnectionCode.html +192 -0
- data/doc/WscSdk/Models/LiveStreamState.html +200 -0
- data/doc/WscSdk/Models/LiveStreamStats.html +200 -0
- data/doc/WscSdk/Models/LiveStreamThumbnailUrl.html +200 -0
- data/doc/WscSdk/Models/Output.html +294 -0
- data/doc/WscSdk/Models/OutputStreamTarget.html +274 -0
- data/doc/WscSdk/Models/StreamTarget.html +273 -0
- data/doc/WscSdk/Models/Transcoder.html +793 -0
- data/doc/WscSdk/Models/TranscoderBooleanStat.html +278 -0
- data/doc/WscSdk/Models/TranscoderConnectionCode.html +192 -0
- data/doc/WscSdk/Models/TranscoderFloatStat.html +278 -0
- data/doc/WscSdk/Models/TranscoderIntegerStat.html +278 -0
- data/doc/WscSdk/Models/TranscoderState.html +196 -0
- data/doc/WscSdk/Models/TranscoderStats.html +196 -0
- data/doc/WscSdk/Models/TranscoderStreamTargetState.html +192 -0
- data/doc/WscSdk/Models/TranscoderStringStat.html +278 -0
- data/doc/WscSdk/Models/TranscoderThumbnailUrl.html +196 -0
- data/doc/WscSdk/Models/UllStreamTarget.html +360 -0
- data/doc/WscSdk/Models/WowzaStreamTarget.html +380 -0
- data/doc/WscSdk/Pagination.html +1277 -0
- data/doc/WscSdk/Schema.html +861 -0
- data/doc/WscSdk/SchemaAttribute.html +2400 -0
- data/doc/WscSdk/Templates.html +141 -0
- data/doc/WscSdk/Templates/CustomStreamTarget.html +1045 -0
- data/doc/WscSdk/Templates/LiveStream.html +1432 -0
- data/doc/WscSdk/Templates/Output.html +829 -0
- data/doc/WscSdk/Templates/Transcoder.html +699 -0
- data/doc/WscSdk/Templates/UllStreamTarget.html +411 -0
- data/doc/WscSdk/Templates/WowzaStreamTarget.html +447 -0
- data/doc/WscSdk/TranscoderSharedMethods.html +1113 -0
- data/doc/_index.html +705 -0
- data/doc/class_list.html +51 -0
- data/doc/css/bootstrap.css +5 -0
- data/doc/css/common.css +1 -0
- data/doc/css/custom.css +151 -0
- data/doc/css/full_list.css +58 -0
- data/doc/css/style.css +496 -0
- data/doc/file.README.html +570 -0
- data/doc/file_list.html +56 -0
- data/doc/frames.html +17 -0
- data/doc/images/wsc-ruby-1800x400.png +0 -0
- data/doc/index.html +570 -0
- data/doc/js/app.js +292 -0
- data/doc/js/full_list.js +216 -0
- data/doc/js/jquery.js +4 -0
- data/doc/method_list.html +1731 -0
- data/doc/top-level-namespace.html +182 -0
- data/examples/client.rb +40 -0
- data/examples/helpers.rb +100 -0
- data/examples/live_streams/create.rb +39 -0
- data/examples/live_streams/delete.rb +32 -0
- data/examples/live_streams/find.rb +22 -0
- data/examples/live_streams/flood.rb +126 -0
- data/examples/live_streams/list.rb +24 -0
- data/examples/live_streams/regenerate_connection_code.rb +29 -0
- data/examples/live_streams/reset.rb +32 -0
- data/examples/live_streams/reset_and_wait.rb +51 -0
- data/examples/live_streams/start.rb +33 -0
- data/examples/live_streams/start_and_wait.rb +51 -0
- data/examples/live_streams/state.rb +33 -0
- data/examples/live_streams/stats.rb +43 -0
- data/examples/live_streams/stop.rb +33 -0
- data/examples/live_streams/stop_and_wait.rb +51 -0
- data/examples/live_streams/thumbnail_url.rb +46 -0
- data/examples/live_streams/update.rb +43 -0
- data/examples/live_streams/workflow.rb +165 -0
- data/examples/output_stream_targets/create.rb +56 -0
- data/examples/output_stream_targets/delete.rb +45 -0
- data/examples/output_stream_targets/find.rb +37 -0
- data/examples/output_stream_targets/list.rb +36 -0
- data/examples/output_stream_targets/update.rb +56 -0
- data/examples/outputs/create.rb +49 -0
- data/examples/outputs/delete.rb +39 -0
- data/examples/outputs/find.rb +31 -0
- data/examples/outputs/list.rb +29 -0
- data/examples/outputs/update.rb +50 -0
- data/examples/stream_targets/custom/create.rb +42 -0
- data/examples/stream_targets/custom/delete.rb +31 -0
- data/examples/stream_targets/custom/find.rb +23 -0
- data/examples/stream_targets/custom/list.rb +24 -0
- data/examples/stream_targets/custom/update.rb +43 -0
- data/examples/stream_targets/list.rb +24 -0
- data/examples/stream_targets/ull/create.rb +41 -0
- data/examples/stream_targets/ull/delete.rb +31 -0
- data/examples/stream_targets/ull/find.rb +23 -0
- data/examples/stream_targets/ull/list.rb +24 -0
- data/examples/stream_targets/ull/update.rb +43 -0
- data/examples/stream_targets/wowza/create.rb +42 -0
- data/examples/stream_targets/wowza/delete.rb +31 -0
- data/examples/stream_targets/wowza/find.rb +23 -0
- data/examples/stream_targets/wowza/list.rb +23 -0
- data/examples/stream_targets/wowza/update.rb +43 -0
- data/examples/transcoders/create.rb +39 -0
- data/examples/transcoders/delete.rb +31 -0
- data/examples/transcoders/find.rb +23 -0
- data/examples/transcoders/list.rb +24 -0
- data/examples/transcoders/reset.rb +32 -0
- data/examples/transcoders/reset_and_wait.rb +48 -0
- data/examples/transcoders/start.rb +33 -0
- data/examples/transcoders/start_and_wait.rb +50 -0
- data/examples/transcoders/state.rb +33 -0
- data/examples/transcoders/stats.rb +43 -0
- data/examples/transcoders/stop.rb +33 -0
- data/examples/transcoders/stop_and_wait.rb +50 -0
- data/examples/transcoders/thumbnail_url.rb +46 -0
- data/examples/transcoders/update.rb +43 -0
- data/images/wsc-ruby-1800x400.png +0 -0
- data/lib/wsc_sdk.rb +81 -0
- data/lib/wsc_sdk/client.rb +417 -0
- data/lib/wsc_sdk/configuration.rb +21 -0
- data/lib/wsc_sdk/constants.rb +29 -0
- data/lib/wsc_sdk/endpoint.rb +492 -0
- data/lib/wsc_sdk/endpoints/custom_stream_targets.rb +44 -0
- data/lib/wsc_sdk/endpoints/live_streams.rb +34 -0
- data/lib/wsc_sdk/endpoints/output_stream_targets.rb +17 -0
- data/lib/wsc_sdk/endpoints/outputs.rb +17 -0
- data/lib/wsc_sdk/endpoints/stream_targets.rb +45 -0
- data/lib/wsc_sdk/endpoints/transcoders.rb +38 -0
- data/lib/wsc_sdk/endpoints/ull_stream_targets.rb +44 -0
- data/lib/wsc_sdk/endpoints/wowza_stream_targets.rb +44 -0
- data/lib/wsc_sdk/enums.rb +31 -0
- data/lib/wsc_sdk/enums/billing_mode.rb +20 -0
- data/lib/wsc_sdk/enums/broadcast_location.rb +58 -0
- data/lib/wsc_sdk/enums/buffer_size.rb +41 -0
- data/lib/wsc_sdk/enums/closed_caption_type.rb +26 -0
- data/lib/wsc_sdk/enums/custom_provider.rb +32 -0
- data/lib/wsc_sdk/enums/delivery_method.rb +20 -0
- data/lib/wsc_sdk/enums/delivery_type.rb +20 -0
- data/lib/wsc_sdk/enums/encoder.rb +71 -0
- data/lib/wsc_sdk/enums/idle_timeout.rb +50 -0
- data/lib/wsc_sdk/enums/image_position.rb +26 -0
- data/lib/wsc_sdk/enums/player_type.rb +20 -0
- data/lib/wsc_sdk/enums/protocol.rb +23 -0
- data/lib/wsc_sdk/enums/target_delivery_protocol.rb +20 -0
- data/lib/wsc_sdk/enums/transcoder_type.rb +20 -0
- data/lib/wsc_sdk/enums/wowza_provider.rb +20 -0
- data/lib/wsc_sdk/errors.rb +115 -0
- data/lib/wsc_sdk/model.rb +515 -0
- data/lib/wsc_sdk/model_list.rb +42 -0
- data/lib/wsc_sdk/model_template.rb +27 -0
- data/lib/wsc_sdk/models/custom_stream_target.rb +34 -0
- data/lib/wsc_sdk/models/error.rb +53 -0
- data/lib/wsc_sdk/models/live_stream.rb +130 -0
- data/lib/wsc_sdk/models/live_stream_connection_code.rb +31 -0
- data/lib/wsc_sdk/models/live_stream_state.rb +20 -0
- data/lib/wsc_sdk/models/live_stream_stats.rb +20 -0
- data/lib/wsc_sdk/models/live_stream_thumbnail_url.rb +20 -0
- data/lib/wsc_sdk/models/output.rb +61 -0
- data/lib/wsc_sdk/models/output_stream_target.rb +37 -0
- data/lib/wsc_sdk/models/stream_target.rb +29 -0
- data/lib/wsc_sdk/models/transcoder.rb +135 -0
- data/lib/wsc_sdk/models/transcoder_boolean_stat.rb +41 -0
- data/lib/wsc_sdk/models/transcoder_connection_code.rb +29 -0
- data/lib/wsc_sdk/models/transcoder_float_stat.rb +40 -0
- data/lib/wsc_sdk/models/transcoder_integer_stat.rb +40 -0
- data/lib/wsc_sdk/models/transcoder_state.rb +31 -0
- data/lib/wsc_sdk/models/transcoder_stats.rb +51 -0
- data/lib/wsc_sdk/models/transcoder_stream_target_state.rb +29 -0
- data/lib/wsc_sdk/models/transcoder_string_stat.rb +40 -0
- data/lib/wsc_sdk/models/transcoder_thumbnail_url.rb +29 -0
- data/lib/wsc_sdk/models/ull_stream_target.rb +54 -0
- data/lib/wsc_sdk/models/wowza_stream_target.rb +60 -0
- data/lib/wsc_sdk/modules/api_response.rb +24 -0
- data/lib/wsc_sdk/modules/loggable.rb +44 -0
- data/lib/wsc_sdk/modules/transcoder_shared_methods.rb +221 -0
- data/lib/wsc_sdk/pagination.rb +89 -0
- data/lib/wsc_sdk/schema.rb +140 -0
- data/lib/wsc_sdk/schema_attribute.rb +349 -0
- data/lib/wsc_sdk/templates/custom_stream_target.rb +154 -0
- data/lib/wsc_sdk/templates/live_stream.rb +234 -0
- data/lib/wsc_sdk/templates/output.rb +133 -0
- data/lib/wsc_sdk/templates/transcoder.rb +97 -0
- data/lib/wsc_sdk/templates/ull_stream_target.rb +49 -0
- data/lib/wsc_sdk/templates/wowza_stream_target.rb +57 -0
- data/lib/wsc_sdk/version.rb +7 -0
- data/wsc_sdk.gemspec +48 -0
- metadata +460 -0
@@ -0,0 +1,89 @@
|
|
1
|
+
####> This code and all components © 2015 – 2019 Wowza Media Systems, LLC. All rights reserved.
|
2
|
+
####> This code is licensed pursuant to the BSD 3-Clause License.
|
3
|
+
|
4
|
+
module WscSdk
|
5
|
+
|
6
|
+
# A class for handling the pagination data returned by endpoint lists.
|
7
|
+
#
|
8
|
+
class Pagination
|
9
|
+
attr_reader :page, :per_page, :total_pages, :total_records, :page_first_index, :page_last_index
|
10
|
+
|
11
|
+
# Get a new instance of the pagination class.
|
12
|
+
#
|
13
|
+
# @param pagination_data [Hash] A hash of pagination data
|
14
|
+
#
|
15
|
+
def initialize(pagination_data={})
|
16
|
+
@page = pagination_data.fetch(:page, 1)
|
17
|
+
@per_page = pagination_data.fetch(:per_page, 1000)
|
18
|
+
@total_pages = pagination_data.fetch(:total_pages, 1)
|
19
|
+
@total_records = pagination_data.fetch(:total_records, -1)
|
20
|
+
@page_first_index = pagination_data.fetch(:page_first_index, -1)
|
21
|
+
@page_last_index = pagination_data.fetch(:page_last_index, -1)
|
22
|
+
end
|
23
|
+
|
24
|
+
# Determine if the current page is the first page
|
25
|
+
#
|
26
|
+
# @return [Boolean] An indication if the current page is the first page.
|
27
|
+
#
|
28
|
+
def first_page?
|
29
|
+
return true if total_pages < 1
|
30
|
+
return (page == 1)
|
31
|
+
end
|
32
|
+
|
33
|
+
# Get the number of the first page
|
34
|
+
#
|
35
|
+
# @return [Integer] The number of the first page.
|
36
|
+
#
|
37
|
+
# @return [Nil] If there is no first page.
|
38
|
+
#
|
39
|
+
def first_page
|
40
|
+
return nil if total_pages < 1
|
41
|
+
return 1
|
42
|
+
end
|
43
|
+
|
44
|
+
# Get the number of the next page.
|
45
|
+
#
|
46
|
+
# @return [Integer] The number of the next page
|
47
|
+
#
|
48
|
+
# @return [Nil] If there are no remaining pages
|
49
|
+
#
|
50
|
+
def next_page
|
51
|
+
return nil if total_pages == -1
|
52
|
+
return nil if page == total_pages
|
53
|
+
return page + 1
|
54
|
+
end
|
55
|
+
|
56
|
+
# Get the number of the previous page.
|
57
|
+
#
|
58
|
+
# @return [Integer] The number of the previous page
|
59
|
+
#
|
60
|
+
# @return [Nil] If there are no previous pages
|
61
|
+
#
|
62
|
+
def previous_page
|
63
|
+
return nil if total_pages < 1
|
64
|
+
return nil if page == 1
|
65
|
+
return page - 1
|
66
|
+
end
|
67
|
+
|
68
|
+
# Get the number of the last page
|
69
|
+
#
|
70
|
+
# @return [Integer] The number of the first page.
|
71
|
+
#
|
72
|
+
# @return [Nil] If there is no first page.
|
73
|
+
#
|
74
|
+
def last_page
|
75
|
+
return nil if total_pages < 1
|
76
|
+
return total_pages
|
77
|
+
end
|
78
|
+
|
79
|
+
# Determine if the current page is the last page
|
80
|
+
#
|
81
|
+
# @return [Boolean] An indication if the current page is the last page.
|
82
|
+
#
|
83
|
+
def last_page?
|
84
|
+
return true if total_pages < 1
|
85
|
+
return (page == total_pages)
|
86
|
+
end
|
87
|
+
|
88
|
+
end
|
89
|
+
end
|
@@ -0,0 +1,140 @@
|
|
1
|
+
####> This code and all components © 2015 – 2019 Wowza Media Systems, LLC. All rights reserved.
|
2
|
+
####> This code is licensed pursuant to the BSD 3-Clause License.
|
3
|
+
|
4
|
+
module WscSdk
|
5
|
+
|
6
|
+
# A class that manages the data schema for an SDK model.
|
7
|
+
#
|
8
|
+
class Schema < Hash
|
9
|
+
|
10
|
+
|
11
|
+
# Adds an attribute to the schema
|
12
|
+
#
|
13
|
+
# @param name [Symbol]
|
14
|
+
# The name of the attribute.
|
15
|
+
#
|
16
|
+
# @param type [Symbol]
|
17
|
+
# The type of the attribute. The type must be in the list of
|
18
|
+
# WscSdk::SchemaAttribute::TYPES
|
19
|
+
#
|
20
|
+
# @param options [Hash]
|
21
|
+
# A hash of options
|
22
|
+
#
|
23
|
+
# @option options [Any,Proc,Symbol] :default
|
24
|
+
# The default value of the attribute. This value must match the assigned
|
25
|
+
# type of the attribute. The default value can be determined by a direct
|
26
|
+
# value, a Proc or a Symbol that represents a method in the model.
|
27
|
+
#
|
28
|
+
# If a *Proc* is provided it will be passed an instance of the model, which
|
29
|
+
# can be used to determine the default value for the attribute. The Proc
|
30
|
+
# should return a value that is the same type as the attribute.
|
31
|
+
#
|
32
|
+
# If a *Symbol* is provided, the Symbol will be checked against the model
|
33
|
+
# to determine if there is a method available that has the same name. If
|
34
|
+
# one exists, that method will be passed an instance of the model, which
|
35
|
+
# can be used to determine the default value for the attribute. The
|
36
|
+
# method should return a value that is the same type as the attribute. If
|
37
|
+
# no method is found that matches the symbol, the symbol itself is used
|
38
|
+
# as the default.
|
39
|
+
#
|
40
|
+
# @option options [Array,Proc,Symbol] :required
|
41
|
+
# Determines if the attribute is required.
|
42
|
+
#
|
43
|
+
# If a *Proc* is provided it will be passed an instance of the model, which
|
44
|
+
# can be used to determine if the value is required. The Proc should
|
45
|
+
# return a boolean value.
|
46
|
+
#
|
47
|
+
# If a *Symbol* is provided, the Symbol will be checked against the model
|
48
|
+
# to determine if there is a method available that has the same name. If
|
49
|
+
# one exists, that method will be called. The method should return a
|
50
|
+
# boolean value.
|
51
|
+
#
|
52
|
+
# @option options [Array,Proc,Symbol] :validate
|
53
|
+
# Determines if the attribute is valid.
|
54
|
+
#
|
55
|
+
# If a *Proc* is provided it will be passed an instance of the model, which
|
56
|
+
# can be used to determine if the value is valid. The Proc should return
|
57
|
+
# a string describing why the attribute is not valid, otherwise it should
|
58
|
+
# return nil.
|
59
|
+
#
|
60
|
+
# If a *Symbol* is provided, the Symbol will be checked against the model to
|
61
|
+
# determine if there is a method available that has the same name. If one
|
62
|
+
# exists, that method will be passed an instance of the model, which
|
63
|
+
# can be used to determine if the value is valid. The Proc should return
|
64
|
+
# a string describing why the attribute is not valid, otherwise it should
|
65
|
+
# return nil.
|
66
|
+
#
|
67
|
+
# @raise [ArgumentError]
|
68
|
+
# If the type is not in the WscSdk::SchemaAttribute::TYPES list.
|
69
|
+
#
|
70
|
+
# @raise [IndexError]
|
71
|
+
# If the attribute name is already defined in the schema.
|
72
|
+
#
|
73
|
+
def add_attribute(name, type, options={})
|
74
|
+
name = name.to_sym
|
75
|
+
raise IndexError.new("The attribute `#{name}` is already defined in the schema") if has_key?(name)
|
76
|
+
self[name] = SchemaAttribute.new(name, type, options)
|
77
|
+
end
|
78
|
+
|
79
|
+
# Determines if the value is a valid type for the attribute.
|
80
|
+
#
|
81
|
+
# @param [Symbol] attribute
|
82
|
+
# The attribute to validate the value for.
|
83
|
+
#
|
84
|
+
# @param [Any] value
|
85
|
+
# The value to validate
|
86
|
+
#
|
87
|
+
# @return [Boolean]
|
88
|
+
# An indication if the value is valid.
|
89
|
+
#
|
90
|
+
def valid_type?(attribute, value)
|
91
|
+
return false unless has_attribute?(attribute)
|
92
|
+
self[attribute].valid_type?(value)
|
93
|
+
end
|
94
|
+
|
95
|
+
# Transforms a value into the appropriate type.
|
96
|
+
#
|
97
|
+
# @param [Symbol] attribute
|
98
|
+
# The attribute to validate the value for.
|
99
|
+
#
|
100
|
+
# @param [WscSdk::Model] model
|
101
|
+
# The model to transform the value for.
|
102
|
+
#
|
103
|
+
# @param [Any] value
|
104
|
+
# The value to validate
|
105
|
+
#
|
106
|
+
# @return [Any]
|
107
|
+
# The transformed value.
|
108
|
+
#
|
109
|
+
def transform_value(attribute, model, value)
|
110
|
+
return nil unless has_attribute?(attribute)
|
111
|
+
self[attribute].transform_value(model, value)
|
112
|
+
end
|
113
|
+
|
114
|
+
# Determine if the schema has an attribute
|
115
|
+
#
|
116
|
+
# @param attribute [Symbol]
|
117
|
+
# The name of the attribute
|
118
|
+
#
|
119
|
+
def has_attribute?(attribute)
|
120
|
+
has_key?(attribute)
|
121
|
+
end
|
122
|
+
|
123
|
+
# Generates a hash of default values for the given model.
|
124
|
+
#
|
125
|
+
# This is a non-destructive process, so if the attributes are already
|
126
|
+
# established and have values, then nothing will happen to them.
|
127
|
+
#
|
128
|
+
# @param model [WscSdk::Model]
|
129
|
+
# The model to use to determine the default values in the event that they
|
130
|
+
# are defined as a Proc or a Symbol.
|
131
|
+
#
|
132
|
+
def defaults(model)
|
133
|
+
hsh = {}
|
134
|
+
each do |name, attribute|
|
135
|
+
hsh[name] = attribute.default_value(model)
|
136
|
+
end
|
137
|
+
hsh
|
138
|
+
end
|
139
|
+
end
|
140
|
+
end
|
@@ -0,0 +1,349 @@
|
|
1
|
+
####> This code and all components © 2015 – 2019 Wowza Media Systems, LLC. All rights reserved.
|
2
|
+
####> This code is licensed pursuant to the BSD 3-Clause License.
|
3
|
+
|
4
|
+
module WscSdk
|
5
|
+
|
6
|
+
# A class to represent an attribute inside of a model's data schema.
|
7
|
+
#
|
8
|
+
class SchemaAttribute
|
9
|
+
|
10
|
+
# A list of valid types for a schema attribute
|
11
|
+
TYPES = [
|
12
|
+
:string,
|
13
|
+
:integer,
|
14
|
+
:float,
|
15
|
+
:boolean,
|
16
|
+
:array,
|
17
|
+
:hash,
|
18
|
+
:datetime
|
19
|
+
]
|
20
|
+
|
21
|
+
attr_reader :name, :type, :access, :default, :validate, :required, :as
|
22
|
+
|
23
|
+
# Create a new schema attribute.
|
24
|
+
#
|
25
|
+
# @param name [Symbol]
|
26
|
+
# The name of the attribute.
|
27
|
+
#
|
28
|
+
# @param type [Symbol]
|
29
|
+
# The type of the attribute. The type must be in the list of
|
30
|
+
# WscSdk::SchemaAttribute::TYPES
|
31
|
+
#
|
32
|
+
# @param options [Hash]
|
33
|
+
# A hash of options
|
34
|
+
#
|
35
|
+
# @option options [Any,Proc,Symbol] :default
|
36
|
+
# The default value of the attribute. This value must match the assigned
|
37
|
+
# type of the attribute. The default value can be determined by a direct
|
38
|
+
# value, a Proc or a Symbol that represents a method in the model.
|
39
|
+
#
|
40
|
+
# If a *Proc* is provided it will be passed an instance of the model, which
|
41
|
+
# can be used to determine the default value for the attribute. The Proc
|
42
|
+
# should return a value that is the same type as the attribute.
|
43
|
+
#
|
44
|
+
# If a *Symbol* is provided, the Symbol will be checked against the model
|
45
|
+
# to determine if there is a method available that has the same name. If
|
46
|
+
# one exists, that method will be passed an instance of the model, which
|
47
|
+
# can be used to determine the default value for the attribute. The
|
48
|
+
# method should return a value that is the same type as the attribute. If
|
49
|
+
# no method is found that matches the symbol, the symbol itself is used
|
50
|
+
# as the default.
|
51
|
+
#
|
52
|
+
# @option options [Array,Proc,Symbol] :required
|
53
|
+
# Determines if the attribute is required.
|
54
|
+
#
|
55
|
+
# If a *Proc* is provided it will be passed an instance of the model, which
|
56
|
+
# can be used to determine if the value is required. The Proc should
|
57
|
+
# return a boolean value.
|
58
|
+
#
|
59
|
+
# If a *Symbol* is provided, the Symbol will be checked against the model
|
60
|
+
# to determine if there is a method available that has the same name. If
|
61
|
+
# one exists, that method will be called. The method should return a
|
62
|
+
# boolean value.
|
63
|
+
#
|
64
|
+
# @option options [Array,Proc,Symbol] :validate
|
65
|
+
# Determines if the attribute is valid.
|
66
|
+
#
|
67
|
+
# If a *Proc* is provided it will be passed an instance of the model, which
|
68
|
+
# can be used to determine if the value is valid. The Proc should return
|
69
|
+
# a string describing why the attribute is not valid, otherwise it should
|
70
|
+
# return nil.
|
71
|
+
#
|
72
|
+
# If a *Symbol* is provided, the Symbol will be checked against the model to
|
73
|
+
# determine if there is a method available that has the same name. If one
|
74
|
+
# exists, that method will be passed an instance of the model, which
|
75
|
+
# can be used to determine if the value is valid. The Proc should return
|
76
|
+
# a string describing why the attribute is not valid, otherwise it should
|
77
|
+
# return nil.
|
78
|
+
#
|
79
|
+
# @raise [ArgumentError]
|
80
|
+
# If the type is not in the WscSdk::SchemaAttribute::TYPES list.
|
81
|
+
#
|
82
|
+
def initialize(name, type, options={})
|
83
|
+
raise ArgumentError.new("Invalid schema attribute type specified: #{type}") unless valid_type?(type)
|
84
|
+
@name = name.to_sym
|
85
|
+
@type = type
|
86
|
+
@access = options.fetch(:access, :read_write)
|
87
|
+
@default = options.fetch(:default, nil)
|
88
|
+
@required = options.fetch(:required, false)
|
89
|
+
@validate = options.fetch(:validate, nil)
|
90
|
+
@as = options.fetch(:as, nil)
|
91
|
+
end
|
92
|
+
|
93
|
+
# Returns the expected attribute name for the attribute. If the `as` value
|
94
|
+
# is returned, this is returned, otherwise the attributes `name` value is
|
95
|
+
# returned.
|
96
|
+
#
|
97
|
+
# @return [Symbol]
|
98
|
+
# The `as` or `name` value for the attribute
|
99
|
+
#
|
100
|
+
def attribute_name
|
101
|
+
(self.as || self.name).to_sym
|
102
|
+
end
|
103
|
+
|
104
|
+
# Determine if the attribute is readable according to the attribute
|
105
|
+
# configuration.
|
106
|
+
#
|
107
|
+
# @param model [WscSdk::Model]
|
108
|
+
# The model that will determine the access level of the attribute.
|
109
|
+
#
|
110
|
+
# @return [Boolean]
|
111
|
+
# An indication if this attribute is readable by the model.
|
112
|
+
#
|
113
|
+
# @raise [ArgumentError]
|
114
|
+
# If the provided model does not have this attribute configured.
|
115
|
+
#
|
116
|
+
def read_access?(model)
|
117
|
+
return true if access.nil?
|
118
|
+
raise ::ArgumentError.new("The provided #{model.class.name} model does not have the #{self.name} attribute configured") unless model.has_attribute?(self.name)
|
119
|
+
|
120
|
+
current_access = call_proc_or_symbol(access, model)
|
121
|
+
[:read, :read_write].include?(current_access)
|
122
|
+
end
|
123
|
+
|
124
|
+
# Determine if the attribute is writable according to the attribute
|
125
|
+
# configuration.
|
126
|
+
#
|
127
|
+
# @param model [WscSdk::Model]
|
128
|
+
# The model that will determine the access level of the attribute.
|
129
|
+
#
|
130
|
+
# @return [Boolean]
|
131
|
+
# An indication if this attribute is writable by the model.
|
132
|
+
#
|
133
|
+
# @raise [ArgumentError]
|
134
|
+
# If the provided model does not have this attribute configured.
|
135
|
+
#
|
136
|
+
def write_access?(model)
|
137
|
+
return true if access.nil?
|
138
|
+
raise ArugumentError.new("The provided #{model.class.name} model does not have the #{self.name} attribute configured") unless model.has_attribute?(self.name)
|
139
|
+
|
140
|
+
current_access = call_proc_or_symbol(access, model)
|
141
|
+
[:write, :read_write].include?(current_access)
|
142
|
+
end
|
143
|
+
|
144
|
+
# Determine if a value is valid according to the attribute configuration.
|
145
|
+
#
|
146
|
+
# @param model [WscSdk::Model]
|
147
|
+
# The model the value comes from.
|
148
|
+
#
|
149
|
+
# @return [String]
|
150
|
+
# A message of what's wrong if the value isn't valid, or nil if the value
|
151
|
+
# is valid.
|
152
|
+
#
|
153
|
+
# @raise [ArgumentError]
|
154
|
+
# If the provided model does not have this attribute configured.
|
155
|
+
#
|
156
|
+
def valid?(model)
|
157
|
+
return nil if validate.nil?
|
158
|
+
|
159
|
+
raise ArugumentError.new("The provided #{model.class.name} model does not have the #{self.name} attribute configured") unless model.has_attribute?(self.name)
|
160
|
+
value = model.attributes[self.name]
|
161
|
+
|
162
|
+
message = nil
|
163
|
+
|
164
|
+
if validate.is_a?(Array)
|
165
|
+
valid = validate.include?(value)
|
166
|
+
model.add_error(name, "value must be one of the following: #{validate.join(", ")}") unless valid
|
167
|
+
return valid
|
168
|
+
else
|
169
|
+
return call_proc_or_symbol(validate, model)
|
170
|
+
end
|
171
|
+
|
172
|
+
# Calls should never get here.
|
173
|
+
return true
|
174
|
+
end
|
175
|
+
|
176
|
+
# Determines if the specified type of the attribute is valid.
|
177
|
+
#
|
178
|
+
# @param [Symbol] attribute_type
|
179
|
+
# The type to check if it's valid.
|
180
|
+
#
|
181
|
+
# @return [Boolean]
|
182
|
+
# An indication that the type is valid.
|
183
|
+
#
|
184
|
+
def valid_type?(attribute_type)
|
185
|
+
return true if TYPES.include?(attribute_type)
|
186
|
+
return valid_model_type?(attribute_type)
|
187
|
+
end
|
188
|
+
|
189
|
+
# Determines if the specified type of the attribute matches a model.
|
190
|
+
#
|
191
|
+
# @param [Symbol] attribute_type
|
192
|
+
# The type to check if it's valid.
|
193
|
+
#
|
194
|
+
# @return [Boolean]
|
195
|
+
# An indication that the type represents a model.
|
196
|
+
#
|
197
|
+
def valid_model_type?(attribute_type)
|
198
|
+
type_class = type_to_class(attribute_type)
|
199
|
+
!type_class.nil?
|
200
|
+
end
|
201
|
+
|
202
|
+
# Converts the specified type to a class
|
203
|
+
#
|
204
|
+
# @param [Symbol] attribute_type
|
205
|
+
# The type to check if it's valid.
|
206
|
+
#
|
207
|
+
# @return [Any]
|
208
|
+
# The class represented by the type, or nil if it's not a valid type.
|
209
|
+
#
|
210
|
+
def type_to_class(attribute_type)
|
211
|
+
# Booleans are a special case because they are not represented by a single
|
212
|
+
# class.
|
213
|
+
return String if attribute_type == :boolean
|
214
|
+
return Time if attribute_type == :datetime
|
215
|
+
|
216
|
+
class_name = attribute_type.to_s.camelize
|
217
|
+
unless TYPES.include?(attribute_type)
|
218
|
+
class_name = "WscSdk::Models::#{class_name}"
|
219
|
+
end
|
220
|
+
return (class_name.constantize)
|
221
|
+
end
|
222
|
+
|
223
|
+
# Converts an inbound value to the appropriate type.
|
224
|
+
#
|
225
|
+
# @param [WscSdk::Model] model
|
226
|
+
# The model the value is for.
|
227
|
+
#
|
228
|
+
# @param [Any] value
|
229
|
+
# The value to be transformed
|
230
|
+
#
|
231
|
+
# @return [Any]
|
232
|
+
# The transformed value
|
233
|
+
#
|
234
|
+
def transform_value(model, value)
|
235
|
+
|
236
|
+
# Booleans are a special case because they are not represented by a single
|
237
|
+
# class.
|
238
|
+
return value.to_s.downcase.start_with?("y","t","1") if self.type == :boolean
|
239
|
+
type_class = type_to_class(self.type)
|
240
|
+
transformed_value = value
|
241
|
+
|
242
|
+
if type_class == Time
|
243
|
+
current_timezone = Time.zone
|
244
|
+
Time.zone = "UTC"
|
245
|
+
transformed_value = Time.zone.parse(value) # => Tue, 23 Nov 2010 23:29:57 UTC +00:00
|
246
|
+
Time.zone = current_timezone
|
247
|
+
elsif type_class < WscSdk::Model and value.is_a?(Hash)
|
248
|
+
transformed_value = type_class.new(model.endpoint, value)
|
249
|
+
end
|
250
|
+
transformed_value
|
251
|
+
end
|
252
|
+
|
253
|
+
# Returns the default value for the attribute
|
254
|
+
#
|
255
|
+
# @param model [WscSdk::Model]
|
256
|
+
# The model the value comes from.
|
257
|
+
#
|
258
|
+
def default_value(model)
|
259
|
+
call_proc_or_symbol(default, model)
|
260
|
+
end
|
261
|
+
|
262
|
+
# Return either the value from the model that is represented by the
|
263
|
+
# attribute, or the default value if the value is not assigned.
|
264
|
+
#
|
265
|
+
# @param model [WscSdk::Model]
|
266
|
+
# The model the value comes from.
|
267
|
+
#
|
268
|
+
# @raise [ArgumentError]
|
269
|
+
# If the provided model does not have this attribute configured.
|
270
|
+
#
|
271
|
+
def value_or_default(model)
|
272
|
+
raise ArugumentError.new("The provided #{model.class.name} model does not have the #{self.name} attribute configured") unless model.has_attribute?(self.name)
|
273
|
+
model.attributes[self.name] || default_value(model)
|
274
|
+
end
|
275
|
+
|
276
|
+
# Determine if the attribute is required.
|
277
|
+
#
|
278
|
+
# @param model [WscSdk::Model]
|
279
|
+
# The model the value comes from.
|
280
|
+
#
|
281
|
+
# @raise [ArgumentError]
|
282
|
+
# If the provided model does not have this attribute configured.
|
283
|
+
#
|
284
|
+
def required?(model)
|
285
|
+
raise ArugumentError.new("The provided #{model.class.name} model does not have the #{self.name} attribute configured") unless model.has_attribute?(self.name)
|
286
|
+
return required if required.is_a?(TrueClass) or required.is_a?(FalseClass)
|
287
|
+
_required = call_proc_or_symbol(required, model)
|
288
|
+
return false if _required.nil?
|
289
|
+
_required
|
290
|
+
end
|
291
|
+
|
292
|
+
# Call a Proc or Symbol against a model.
|
293
|
+
#
|
294
|
+
# @param proc_or_symbol [Proc,Symbol]
|
295
|
+
# The Proc or Symbol to attempt to make a call against. If the value
|
296
|
+
# provided is Nil, or not a Proc or Symbol, then it is returned directly.
|
297
|
+
#
|
298
|
+
# @param model [WscSdk::Model]
|
299
|
+
# The model to pass into the Proc or Symbol method if appropriate.
|
300
|
+
#
|
301
|
+
# @return [Any]
|
302
|
+
# The results of calling the Proc or Symbol or the value of proc_or_symbol
|
303
|
+
# if it isn't a Proc or Symbol.
|
304
|
+
#
|
305
|
+
# @raise [ArgumentError]
|
306
|
+
# If the provided model does not have this attribute configured.
|
307
|
+
#
|
308
|
+
def call_proc_or_symbol(proc_or_symbol, model)
|
309
|
+
return proc_or_symbol if proc_or_symbol.nil?
|
310
|
+
return proc_or_symbol.call(model) if proc_or_symbol.is_a?(Proc)
|
311
|
+
return model.send(proc_or_symbol) if proc_or_symbol.is_a?(Symbol) and model.respond_to?(proc_or_symbol)
|
312
|
+
return proc_or_symbol
|
313
|
+
end
|
314
|
+
|
315
|
+
# Determine if the provided value is of a valid type for the attribute.
|
316
|
+
#
|
317
|
+
# @param value [Any]
|
318
|
+
# The value whose type will be compared to the attribute to determine if
|
319
|
+
# it is valid.
|
320
|
+
#
|
321
|
+
# @return [Boolean]
|
322
|
+
# An indication of whether or not the value's type matches the attribute
|
323
|
+
# type.
|
324
|
+
#
|
325
|
+
def valid_type?(value)
|
326
|
+
return true if value.nil?
|
327
|
+
|
328
|
+
valid = case (type)
|
329
|
+
when :string
|
330
|
+
value.is_a?(String)
|
331
|
+
when :integer
|
332
|
+
value.is_a?(Integer) or (value.to_i.to_s == value.to_s)
|
333
|
+
when :float
|
334
|
+
value.is_a?(Float) or (value.to_f.to_s == value.to_s)
|
335
|
+
when :boolean
|
336
|
+
value.is_a?(TrueClass) or value.is_a?(FalseClass) or ['0','1','y','n','t','f','true','false'].include?(value.to_s.downcase)
|
337
|
+
when :array
|
338
|
+
value.is_a?(Array)
|
339
|
+
when :hash
|
340
|
+
value.is_a?(Hash)
|
341
|
+
when :datetime
|
342
|
+
value.is_a?(Date) or value.is_a?(Time) or (!(Date.parse(value) rescue nil).nil?)
|
343
|
+
else
|
344
|
+
true
|
345
|
+
end
|
346
|
+
end
|
347
|
+
|
348
|
+
end
|
349
|
+
end
|