wsc_sdk 1.3.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (265) hide show
  1. checksums.yaml +7 -0
  2. data/.circleci/config.yml +146 -0
  3. data/.circleci/docker/build/Dockerfile +3 -0
  4. data/.circleci/helpers/add_preamble.rb +29 -0
  5. data/.circleci/helpers/build_docs.sh +5 -0
  6. data/.circleci/helpers/build_gem.sh +5 -0
  7. data/.circleci/helpers/debundle-context.sh +14 -0
  8. data/.circleci/helpers/finalize_release.sh +24 -0
  9. data/.circleci/helpers/publish_docs.sh +10 -0
  10. data/.circleci/helpers/release_gem.sh +8 -0
  11. data/.circleci/helpers/setup.sh +11 -0
  12. data/.circleci/helpers/setup_aws.sh +13 -0
  13. data/.circleci/helpers/setup_gem.sh +2 -0
  14. data/.circleci/helpers/setup_git.sh +27 -0
  15. data/.circleci/helpers/setup_rubygems.sh +15 -0
  16. data/.circleci/helpers/test_integration.sh +12 -0
  17. data/.circleci/helpers/test_unit.sh +6 -0
  18. data/.circleci/jobs/publish-production.sh +17 -0
  19. data/.circleci/jobs/publish-staging.sh +17 -0
  20. data/.circleci/jobs/release-production.sh +21 -0
  21. data/.circleci/jobs/test-integration.sh +9 -0
  22. data/.circleci/jobs/test-unit.sh +9 -0
  23. data/.circleci/tag_repo.rb +46 -0
  24. data/.gitignore +28 -0
  25. data/.rspec +2 -0
  26. data/.yard/config +2 -0
  27. data/.yard/template/default/fulldoc/html/css/custom.css +151 -0
  28. data/.yard/template/default/layout/html/footer.erb +0 -0
  29. data/.yard/template/default/layout/html/headers.erb +10 -0
  30. data/.yard/template/default/layout/html/layout.erb +45 -0
  31. data/.yard/template/default/layout/html/setup.rb +24 -0
  32. data/.yardopts +1 -0
  33. data/CODE_OF_CONDUCT.md +74 -0
  34. data/Gemfile +6 -0
  35. data/Gemfile.lock +99 -0
  36. data/LICENSE.txt +28 -0
  37. data/README.md +397 -0
  38. data/Rakefile +2 -0
  39. data/bin/console +14 -0
  40. data/bin/setup +8 -0
  41. data/doc/Hash.html +291 -0
  42. data/doc/WscSdk.html +570 -0
  43. data/doc/WscSdk/ApiResponse.html +242 -0
  44. data/doc/WscSdk/Attributes.html +128 -0
  45. data/doc/WscSdk/Attributes/Mode.html +191 -0
  46. data/doc/WscSdk/Attributes/Type.html +389 -0
  47. data/doc/WscSdk/Client.html +2677 -0
  48. data/doc/WscSdk/Configuration.html +595 -0
  49. data/doc/WscSdk/Endpoint.html +2974 -0
  50. data/doc/WscSdk/Endpoints.html +141 -0
  51. data/doc/WscSdk/Endpoints/CustomStreamTargets.html +383 -0
  52. data/doc/WscSdk/Endpoints/LiveStreams.html +197 -0
  53. data/doc/WscSdk/Endpoints/OutputStreamTargets.html +180 -0
  54. data/doc/WscSdk/Endpoints/Outputs.html +180 -0
  55. data/doc/WscSdk/Endpoints/StreamTargets.html +446 -0
  56. data/doc/WscSdk/Endpoints/Transcoders.html +197 -0
  57. data/doc/WscSdk/Endpoints/UllStreamTargets.html +383 -0
  58. data/doc/WscSdk/Endpoints/WowzaStreamTargets.html +383 -0
  59. data/doc/WscSdk/Enum.html +225 -0
  60. data/doc/WscSdk/Enums.html +321 -0
  61. data/doc/WscSdk/Enums/BillingMode.html +188 -0
  62. data/doc/WscSdk/Enums/BroadcastLocation.html +396 -0
  63. data/doc/WscSdk/Enums/BufferSize.html +301 -0
  64. data/doc/WscSdk/Enums/ClosedCaptionType.html +221 -0
  65. data/doc/WscSdk/Enums/CustomProvider.html +252 -0
  66. data/doc/WscSdk/Enums/DeliveryMethod.html +188 -0
  67. data/doc/WscSdk/Enums/DeliveryType.html +188 -0
  68. data/doc/WscSdk/Enums/Encoder.html +460 -0
  69. data/doc/WscSdk/Enums/Errors.html +555 -0
  70. data/doc/WscSdk/Enums/IdleTimeout.html +398 -0
  71. data/doc/WscSdk/Enums/ImagePosition.html +220 -0
  72. data/doc/WscSdk/Enums/ModelMode.html +175 -0
  73. data/doc/WscSdk/Enums/ModelType.html +373 -0
  74. data/doc/WscSdk/Enums/PlayerType.html +188 -0
  75. data/doc/WscSdk/Enums/Protocol.html +204 -0
  76. data/doc/WscSdk/Enums/TargetDeliveryProtocol.html +188 -0
  77. data/doc/WscSdk/Enums/TranscoderType.html +188 -0
  78. data/doc/WscSdk/Enums/UserRegion.html +383 -0
  79. data/doc/WscSdk/Enums/WowzaProvider.html +188 -0
  80. data/doc/WscSdk/Errors.html +683 -0
  81. data/doc/WscSdk/Loggable.html +346 -0
  82. data/doc/WscSdk/Model.html +3316 -0
  83. data/doc/WscSdk/ModelList.html +442 -0
  84. data/doc/WscSdk/ModelTemplate.html +267 -0
  85. data/doc/WscSdk/Models.html +141 -0
  86. data/doc/WscSdk/Models/CustomStreamTarget.html +192 -0
  87. data/doc/WscSdk/Models/Error.html +351 -0
  88. data/doc/WscSdk/Models/LiveStream.html +601 -0
  89. data/doc/WscSdk/Models/LiveStreamConnectionCode.html +192 -0
  90. data/doc/WscSdk/Models/LiveStreamState.html +200 -0
  91. data/doc/WscSdk/Models/LiveStreamStats.html +200 -0
  92. data/doc/WscSdk/Models/LiveStreamThumbnailUrl.html +200 -0
  93. data/doc/WscSdk/Models/Output.html +294 -0
  94. data/doc/WscSdk/Models/OutputStreamTarget.html +274 -0
  95. data/doc/WscSdk/Models/StreamTarget.html +273 -0
  96. data/doc/WscSdk/Models/Transcoder.html +793 -0
  97. data/doc/WscSdk/Models/TranscoderBooleanStat.html +278 -0
  98. data/doc/WscSdk/Models/TranscoderConnectionCode.html +192 -0
  99. data/doc/WscSdk/Models/TranscoderFloatStat.html +278 -0
  100. data/doc/WscSdk/Models/TranscoderIntegerStat.html +278 -0
  101. data/doc/WscSdk/Models/TranscoderState.html +196 -0
  102. data/doc/WscSdk/Models/TranscoderStats.html +196 -0
  103. data/doc/WscSdk/Models/TranscoderStreamTargetState.html +192 -0
  104. data/doc/WscSdk/Models/TranscoderStringStat.html +278 -0
  105. data/doc/WscSdk/Models/TranscoderThumbnailUrl.html +196 -0
  106. data/doc/WscSdk/Models/UllStreamTarget.html +360 -0
  107. data/doc/WscSdk/Models/WowzaStreamTarget.html +380 -0
  108. data/doc/WscSdk/Pagination.html +1277 -0
  109. data/doc/WscSdk/Schema.html +861 -0
  110. data/doc/WscSdk/SchemaAttribute.html +2400 -0
  111. data/doc/WscSdk/Templates.html +141 -0
  112. data/doc/WscSdk/Templates/CustomStreamTarget.html +1045 -0
  113. data/doc/WscSdk/Templates/LiveStream.html +1432 -0
  114. data/doc/WscSdk/Templates/Output.html +829 -0
  115. data/doc/WscSdk/Templates/Transcoder.html +699 -0
  116. data/doc/WscSdk/Templates/UllStreamTarget.html +411 -0
  117. data/doc/WscSdk/Templates/WowzaStreamTarget.html +447 -0
  118. data/doc/WscSdk/TranscoderSharedMethods.html +1113 -0
  119. data/doc/_index.html +705 -0
  120. data/doc/class_list.html +51 -0
  121. data/doc/css/bootstrap.css +5 -0
  122. data/doc/css/common.css +1 -0
  123. data/doc/css/custom.css +151 -0
  124. data/doc/css/full_list.css +58 -0
  125. data/doc/css/style.css +496 -0
  126. data/doc/file.README.html +570 -0
  127. data/doc/file_list.html +56 -0
  128. data/doc/frames.html +17 -0
  129. data/doc/images/wsc-ruby-1800x400.png +0 -0
  130. data/doc/index.html +570 -0
  131. data/doc/js/app.js +292 -0
  132. data/doc/js/full_list.js +216 -0
  133. data/doc/js/jquery.js +4 -0
  134. data/doc/method_list.html +1731 -0
  135. data/doc/top-level-namespace.html +182 -0
  136. data/examples/client.rb +40 -0
  137. data/examples/helpers.rb +100 -0
  138. data/examples/live_streams/create.rb +39 -0
  139. data/examples/live_streams/delete.rb +32 -0
  140. data/examples/live_streams/find.rb +22 -0
  141. data/examples/live_streams/flood.rb +126 -0
  142. data/examples/live_streams/list.rb +24 -0
  143. data/examples/live_streams/regenerate_connection_code.rb +29 -0
  144. data/examples/live_streams/reset.rb +32 -0
  145. data/examples/live_streams/reset_and_wait.rb +51 -0
  146. data/examples/live_streams/start.rb +33 -0
  147. data/examples/live_streams/start_and_wait.rb +51 -0
  148. data/examples/live_streams/state.rb +33 -0
  149. data/examples/live_streams/stats.rb +43 -0
  150. data/examples/live_streams/stop.rb +33 -0
  151. data/examples/live_streams/stop_and_wait.rb +51 -0
  152. data/examples/live_streams/thumbnail_url.rb +46 -0
  153. data/examples/live_streams/update.rb +43 -0
  154. data/examples/live_streams/workflow.rb +165 -0
  155. data/examples/output_stream_targets/create.rb +56 -0
  156. data/examples/output_stream_targets/delete.rb +45 -0
  157. data/examples/output_stream_targets/find.rb +37 -0
  158. data/examples/output_stream_targets/list.rb +36 -0
  159. data/examples/output_stream_targets/update.rb +56 -0
  160. data/examples/outputs/create.rb +49 -0
  161. data/examples/outputs/delete.rb +39 -0
  162. data/examples/outputs/find.rb +31 -0
  163. data/examples/outputs/list.rb +29 -0
  164. data/examples/outputs/update.rb +50 -0
  165. data/examples/stream_targets/custom/create.rb +42 -0
  166. data/examples/stream_targets/custom/delete.rb +31 -0
  167. data/examples/stream_targets/custom/find.rb +23 -0
  168. data/examples/stream_targets/custom/list.rb +24 -0
  169. data/examples/stream_targets/custom/update.rb +43 -0
  170. data/examples/stream_targets/list.rb +24 -0
  171. data/examples/stream_targets/ull/create.rb +41 -0
  172. data/examples/stream_targets/ull/delete.rb +31 -0
  173. data/examples/stream_targets/ull/find.rb +23 -0
  174. data/examples/stream_targets/ull/list.rb +24 -0
  175. data/examples/stream_targets/ull/update.rb +43 -0
  176. data/examples/stream_targets/wowza/create.rb +42 -0
  177. data/examples/stream_targets/wowza/delete.rb +31 -0
  178. data/examples/stream_targets/wowza/find.rb +23 -0
  179. data/examples/stream_targets/wowza/list.rb +23 -0
  180. data/examples/stream_targets/wowza/update.rb +43 -0
  181. data/examples/transcoders/create.rb +39 -0
  182. data/examples/transcoders/delete.rb +31 -0
  183. data/examples/transcoders/find.rb +23 -0
  184. data/examples/transcoders/list.rb +24 -0
  185. data/examples/transcoders/reset.rb +32 -0
  186. data/examples/transcoders/reset_and_wait.rb +48 -0
  187. data/examples/transcoders/start.rb +33 -0
  188. data/examples/transcoders/start_and_wait.rb +50 -0
  189. data/examples/transcoders/state.rb +33 -0
  190. data/examples/transcoders/stats.rb +43 -0
  191. data/examples/transcoders/stop.rb +33 -0
  192. data/examples/transcoders/stop_and_wait.rb +50 -0
  193. data/examples/transcoders/thumbnail_url.rb +46 -0
  194. data/examples/transcoders/update.rb +43 -0
  195. data/images/wsc-ruby-1800x400.png +0 -0
  196. data/lib/wsc_sdk.rb +81 -0
  197. data/lib/wsc_sdk/client.rb +417 -0
  198. data/lib/wsc_sdk/configuration.rb +21 -0
  199. data/lib/wsc_sdk/constants.rb +29 -0
  200. data/lib/wsc_sdk/endpoint.rb +492 -0
  201. data/lib/wsc_sdk/endpoints/custom_stream_targets.rb +44 -0
  202. data/lib/wsc_sdk/endpoints/live_streams.rb +34 -0
  203. data/lib/wsc_sdk/endpoints/output_stream_targets.rb +17 -0
  204. data/lib/wsc_sdk/endpoints/outputs.rb +17 -0
  205. data/lib/wsc_sdk/endpoints/stream_targets.rb +45 -0
  206. data/lib/wsc_sdk/endpoints/transcoders.rb +38 -0
  207. data/lib/wsc_sdk/endpoints/ull_stream_targets.rb +44 -0
  208. data/lib/wsc_sdk/endpoints/wowza_stream_targets.rb +44 -0
  209. data/lib/wsc_sdk/enums.rb +31 -0
  210. data/lib/wsc_sdk/enums/billing_mode.rb +20 -0
  211. data/lib/wsc_sdk/enums/broadcast_location.rb +58 -0
  212. data/lib/wsc_sdk/enums/buffer_size.rb +41 -0
  213. data/lib/wsc_sdk/enums/closed_caption_type.rb +26 -0
  214. data/lib/wsc_sdk/enums/custom_provider.rb +32 -0
  215. data/lib/wsc_sdk/enums/delivery_method.rb +20 -0
  216. data/lib/wsc_sdk/enums/delivery_type.rb +20 -0
  217. data/lib/wsc_sdk/enums/encoder.rb +71 -0
  218. data/lib/wsc_sdk/enums/idle_timeout.rb +50 -0
  219. data/lib/wsc_sdk/enums/image_position.rb +26 -0
  220. data/lib/wsc_sdk/enums/player_type.rb +20 -0
  221. data/lib/wsc_sdk/enums/protocol.rb +23 -0
  222. data/lib/wsc_sdk/enums/target_delivery_protocol.rb +20 -0
  223. data/lib/wsc_sdk/enums/transcoder_type.rb +20 -0
  224. data/lib/wsc_sdk/enums/wowza_provider.rb +20 -0
  225. data/lib/wsc_sdk/errors.rb +115 -0
  226. data/lib/wsc_sdk/model.rb +515 -0
  227. data/lib/wsc_sdk/model_list.rb +42 -0
  228. data/lib/wsc_sdk/model_template.rb +27 -0
  229. data/lib/wsc_sdk/models/custom_stream_target.rb +34 -0
  230. data/lib/wsc_sdk/models/error.rb +53 -0
  231. data/lib/wsc_sdk/models/live_stream.rb +130 -0
  232. data/lib/wsc_sdk/models/live_stream_connection_code.rb +31 -0
  233. data/lib/wsc_sdk/models/live_stream_state.rb +20 -0
  234. data/lib/wsc_sdk/models/live_stream_stats.rb +20 -0
  235. data/lib/wsc_sdk/models/live_stream_thumbnail_url.rb +20 -0
  236. data/lib/wsc_sdk/models/output.rb +61 -0
  237. data/lib/wsc_sdk/models/output_stream_target.rb +37 -0
  238. data/lib/wsc_sdk/models/stream_target.rb +29 -0
  239. data/lib/wsc_sdk/models/transcoder.rb +135 -0
  240. data/lib/wsc_sdk/models/transcoder_boolean_stat.rb +41 -0
  241. data/lib/wsc_sdk/models/transcoder_connection_code.rb +29 -0
  242. data/lib/wsc_sdk/models/transcoder_float_stat.rb +40 -0
  243. data/lib/wsc_sdk/models/transcoder_integer_stat.rb +40 -0
  244. data/lib/wsc_sdk/models/transcoder_state.rb +31 -0
  245. data/lib/wsc_sdk/models/transcoder_stats.rb +51 -0
  246. data/lib/wsc_sdk/models/transcoder_stream_target_state.rb +29 -0
  247. data/lib/wsc_sdk/models/transcoder_string_stat.rb +40 -0
  248. data/lib/wsc_sdk/models/transcoder_thumbnail_url.rb +29 -0
  249. data/lib/wsc_sdk/models/ull_stream_target.rb +54 -0
  250. data/lib/wsc_sdk/models/wowza_stream_target.rb +60 -0
  251. data/lib/wsc_sdk/modules/api_response.rb +24 -0
  252. data/lib/wsc_sdk/modules/loggable.rb +44 -0
  253. data/lib/wsc_sdk/modules/transcoder_shared_methods.rb +221 -0
  254. data/lib/wsc_sdk/pagination.rb +89 -0
  255. data/lib/wsc_sdk/schema.rb +140 -0
  256. data/lib/wsc_sdk/schema_attribute.rb +349 -0
  257. data/lib/wsc_sdk/templates/custom_stream_target.rb +154 -0
  258. data/lib/wsc_sdk/templates/live_stream.rb +234 -0
  259. data/lib/wsc_sdk/templates/output.rb +133 -0
  260. data/lib/wsc_sdk/templates/transcoder.rb +97 -0
  261. data/lib/wsc_sdk/templates/ull_stream_target.rb +49 -0
  262. data/lib/wsc_sdk/templates/wowza_stream_target.rb +57 -0
  263. data/lib/wsc_sdk/version.rb +7 -0
  264. data/wsc_sdk.gemspec +48 -0
  265. 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