Class: Cts::Mpx::Aci::Stencil
- Inherits:
-
Object
- Object
- Cts::Mpx::Aci::Stencil
- Includes:
- Creatable
- Defined in:
- lib/cts/mpx/aci/stencil.rb
Overview
Container for a selection of queries to run on an account.
Instance Attribute Summary collapse
-
#name ⇒ String
Name of the stencil.
-
#original_url ⇒ String
If applicable, the original location of the asset.
-
#queries ⇒ String
Queries to perform in the collect object.
-
#schema ⇒ String
Schema of this stencil.
Class Method Summary collapse
-
.[](key = nil) ⇒ Stencil
find a stencil in the loaded stencils by name.
-
.load(string) ⇒ Stencil
Smart load a stencil.
-
.load_file(file) ⇒ Stencil
Attempt to open a local file for reading, loading in the JSON if it is able.
-
.load_string(string) ⇒ Stencil
Attempt to open a local file for reading, loading in the JSON if it is able.
-
.load_url(url) ⇒ Stencil
Attempt to open a local file for reading, loading in the JSON if it is able.
-
.parse_json(string) ⇒ Hash
Attempt to open a local file for reading, loading in the JSON if it is able.
Instance Method Summary collapse
-
#initialize ⇒ Stencil
constructor
A new instance of Stencil.
-
#to_collect(account_id: nil, user: nil) ⇒ Collect
Attempt to open a local file for reading, loading in the JSON if it is able.
Constructor Details
#initialize ⇒ Stencil
Returns a new instance of Stencil.
116 117 118 119 120 121 122 |
# File 'lib/cts/mpx/aci/stencil.rb', line 116 def initialize Stencil.class_variable_set(:@@available_stencils, {}) unless Stencil.class_variable_defined? :@@available_stencils @name = "" @original_url = "" @queries = [] @schema = 1 end |
Instance Attribute Details
#name ⇒ String
Returns Name of the stencil.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/cts/mpx/aci/stencil.rb', line 13 class Stencil include Creatable attribute name: 'name', kind_of: String attribute name: 'original_url', kind_of: String attribute name: 'schema', kind_of: String attribute name: 'queries', kind_of: Array # find a stencil in the loaded stencils by name. when used with no parameter will show all available stencils. # # @param key [String] name of the stencil # @return [Stencil] referenced stencil def self.[](key = nil) Stencil.class_variable_set(:@@available_stencils, {}) unless Stencil.class_variable_defined? :@@available_stencils return @@available_stencils[key] if key @@available_stencils end # Smart load a stencil. This will attemp to parse it as json, then load it as a url, and finally try to load a file. # # @param string [String] string to attempt to smart load. # @return [Stencil] loaded stencil def self.load(string) begin data = load_string Stencil.parse_json(string) rescue ArgumentError return load_url(string) if string.start_with? 'http' return load_file string end load_string(string) if data end # Attempt to open a local file for reading, loading in the JSON if it is able. # # @param file [String] file of the file to load. # @raise [RuntimeError] If file could not be found # @raise [RuntimeError] If file is empty. # @return [Stencil] loaded stencil def self.load_file(file) raise "Could not find file #{file}." unless File.exist? file content = File.read(file) raise RuntimeError if content.empty? stencil = load_string(content) stencil.original_url = file stencil end # Attempt to open a local file for reading, loading in the JSON if it is able. # # @param string [String] string to be parsed and loaded. # @raise [ArgumentError] if the parsed queries is not an array # @return [Stencil] loaded stencil def self.load_string(string) data = Stencil.parse_json(string) stencil = Stencil.new stencil.name = data['name'] raise ArgumentError, "queries is not a kind of Array" unless data['queries'].is_a? Array stencil.queries = data['queries'] @@available_stencils[stencil.name] = stencil stencil end # Attempt to open a local file for reading, loading in the JSON if it is able. # # @param url [String] url to be fetched and passed to load_string # @raise [ArgumentError] if the url is not valid # @return [Stencil] loaded stencil def self.load_url(url) begin URI.parse url rescue URI::InvalidURIError raise ArgumentError, "#{url} is not a url" end load_string(Excon.get(url).body) end # Attempt to open a local file for reading, loading in the JSON if it is able. # # @param string [String] string is parsed and returned as a hash # @raise [ArgumentError] if the arugment is not a type of a string # @raise [ArgumentError] if the string could not be parsed # @return [Hash] hash of the data from the json def self.parse_json(string) raise ArgumentError, "#{string.class} is not a kind of String" unless string&.is_a?(String) begin data = Oj.load(string) rescue Oj::ParseError raise ArgumentError, "could not be parsed" end data end def initialize Stencil.class_variable_set(:@@available_stencils, {}) unless Stencil.class_variable_defined? :@@available_stencils @name = "" @original_url = "" @queries = [] @schema = 1 end # Attempt to open a local file for reading, loading in the JSON if it is able. # # @param account_id [String] account_id account_id to collect from # @param user [String] user to use to make the collection. # @raise [ArgumentError] if the arugment is not a type of a string # @raise [ArgumentError] if no queries are provided # @return [Collect] a collect object created from the json and parameters passed in. def to_collect(account_id: nil, user: nil) raise ArgumentError, 'queries must contain entries' unless queries.any? collect = Tasks::Collect.new collect.account_id = account_id if account_id collect.user = user if user queries.each do |q| query = Query.create service: q[:service], endpoint: q[:endpoint] collect.queries.push query end collect end end |
#original_url ⇒ String
Returns If applicable, the original location of the asset.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/cts/mpx/aci/stencil.rb', line 13 class Stencil include Creatable attribute name: 'name', kind_of: String attribute name: 'original_url', kind_of: String attribute name: 'schema', kind_of: String attribute name: 'queries', kind_of: Array # find a stencil in the loaded stencils by name. when used with no parameter will show all available stencils. # # @param key [String] name of the stencil # @return [Stencil] referenced stencil def self.[](key = nil) Stencil.class_variable_set(:@@available_stencils, {}) unless Stencil.class_variable_defined? :@@available_stencils return @@available_stencils[key] if key @@available_stencils end # Smart load a stencil. This will attemp to parse it as json, then load it as a url, and finally try to load a file. # # @param string [String] string to attempt to smart load. # @return [Stencil] loaded stencil def self.load(string) begin data = load_string Stencil.parse_json(string) rescue ArgumentError return load_url(string) if string.start_with? 'http' return load_file string end load_string(string) if data end # Attempt to open a local file for reading, loading in the JSON if it is able. # # @param file [String] file of the file to load. # @raise [RuntimeError] If file could not be found # @raise [RuntimeError] If file is empty. # @return [Stencil] loaded stencil def self.load_file(file) raise "Could not find file #{file}." unless File.exist? file content = File.read(file) raise RuntimeError if content.empty? stencil = load_string(content) stencil.original_url = file stencil end # Attempt to open a local file for reading, loading in the JSON if it is able. # # @param string [String] string to be parsed and loaded. # @raise [ArgumentError] if the parsed queries is not an array # @return [Stencil] loaded stencil def self.load_string(string) data = Stencil.parse_json(string) stencil = Stencil.new stencil.name = data['name'] raise ArgumentError, "queries is not a kind of Array" unless data['queries'].is_a? Array stencil.queries = data['queries'] @@available_stencils[stencil.name] = stencil stencil end # Attempt to open a local file for reading, loading in the JSON if it is able. # # @param url [String] url to be fetched and passed to load_string # @raise [ArgumentError] if the url is not valid # @return [Stencil] loaded stencil def self.load_url(url) begin URI.parse url rescue URI::InvalidURIError raise ArgumentError, "#{url} is not a url" end load_string(Excon.get(url).body) end # Attempt to open a local file for reading, loading in the JSON if it is able. # # @param string [String] string is parsed and returned as a hash # @raise [ArgumentError] if the arugment is not a type of a string # @raise [ArgumentError] if the string could not be parsed # @return [Hash] hash of the data from the json def self.parse_json(string) raise ArgumentError, "#{string.class} is not a kind of String" unless string&.is_a?(String) begin data = Oj.load(string) rescue Oj::ParseError raise ArgumentError, "could not be parsed" end data end def initialize Stencil.class_variable_set(:@@available_stencils, {}) unless Stencil.class_variable_defined? :@@available_stencils @name = "" @original_url = "" @queries = [] @schema = 1 end # Attempt to open a local file for reading, loading in the JSON if it is able. # # @param account_id [String] account_id account_id to collect from # @param user [String] user to use to make the collection. # @raise [ArgumentError] if the arugment is not a type of a string # @raise [ArgumentError] if no queries are provided # @return [Collect] a collect object created from the json and parameters passed in. def to_collect(account_id: nil, user: nil) raise ArgumentError, 'queries must contain entries' unless queries.any? collect = Tasks::Collect.new collect.account_id = account_id if account_id collect.user = user if user queries.each do |q| query = Query.create service: q[:service], endpoint: q[:endpoint] collect.queries.push query end collect end end |
#queries ⇒ String
Returns Queries to perform in the collect object.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/cts/mpx/aci/stencil.rb', line 13 class Stencil include Creatable attribute name: 'name', kind_of: String attribute name: 'original_url', kind_of: String attribute name: 'schema', kind_of: String attribute name: 'queries', kind_of: Array # find a stencil in the loaded stencils by name. when used with no parameter will show all available stencils. # # @param key [String] name of the stencil # @return [Stencil] referenced stencil def self.[](key = nil) Stencil.class_variable_set(:@@available_stencils, {}) unless Stencil.class_variable_defined? :@@available_stencils return @@available_stencils[key] if key @@available_stencils end # Smart load a stencil. This will attemp to parse it as json, then load it as a url, and finally try to load a file. # # @param string [String] string to attempt to smart load. # @return [Stencil] loaded stencil def self.load(string) begin data = load_string Stencil.parse_json(string) rescue ArgumentError return load_url(string) if string.start_with? 'http' return load_file string end load_string(string) if data end # Attempt to open a local file for reading, loading in the JSON if it is able. # # @param file [String] file of the file to load. # @raise [RuntimeError] If file could not be found # @raise [RuntimeError] If file is empty. # @return [Stencil] loaded stencil def self.load_file(file) raise "Could not find file #{file}." unless File.exist? file content = File.read(file) raise RuntimeError if content.empty? stencil = load_string(content) stencil.original_url = file stencil end # Attempt to open a local file for reading, loading in the JSON if it is able. # # @param string [String] string to be parsed and loaded. # @raise [ArgumentError] if the parsed queries is not an array # @return [Stencil] loaded stencil def self.load_string(string) data = Stencil.parse_json(string) stencil = Stencil.new stencil.name = data['name'] raise ArgumentError, "queries is not a kind of Array" unless data['queries'].is_a? Array stencil.queries = data['queries'] @@available_stencils[stencil.name] = stencil stencil end # Attempt to open a local file for reading, loading in the JSON if it is able. # # @param url [String] url to be fetched and passed to load_string # @raise [ArgumentError] if the url is not valid # @return [Stencil] loaded stencil def self.load_url(url) begin URI.parse url rescue URI::InvalidURIError raise ArgumentError, "#{url} is not a url" end load_string(Excon.get(url).body) end # Attempt to open a local file for reading, loading in the JSON if it is able. # # @param string [String] string is parsed and returned as a hash # @raise [ArgumentError] if the arugment is not a type of a string # @raise [ArgumentError] if the string could not be parsed # @return [Hash] hash of the data from the json def self.parse_json(string) raise ArgumentError, "#{string.class} is not a kind of String" unless string&.is_a?(String) begin data = Oj.load(string) rescue Oj::ParseError raise ArgumentError, "could not be parsed" end data end def initialize Stencil.class_variable_set(:@@available_stencils, {}) unless Stencil.class_variable_defined? :@@available_stencils @name = "" @original_url = "" @queries = [] @schema = 1 end # Attempt to open a local file for reading, loading in the JSON if it is able. # # @param account_id [String] account_id account_id to collect from # @param user [String] user to use to make the collection. # @raise [ArgumentError] if the arugment is not a type of a string # @raise [ArgumentError] if no queries are provided # @return [Collect] a collect object created from the json and parameters passed in. def to_collect(account_id: nil, user: nil) raise ArgumentError, 'queries must contain entries' unless queries.any? collect = Tasks::Collect.new collect.account_id = account_id if account_id collect.user = user if user queries.each do |q| query = Query.create service: q[:service], endpoint: q[:endpoint] collect.queries.push query end collect end end |
#schema ⇒ String
Returns Schema of this stencil.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/cts/mpx/aci/stencil.rb', line 13 class Stencil include Creatable attribute name: 'name', kind_of: String attribute name: 'original_url', kind_of: String attribute name: 'schema', kind_of: String attribute name: 'queries', kind_of: Array # find a stencil in the loaded stencils by name. when used with no parameter will show all available stencils. # # @param key [String] name of the stencil # @return [Stencil] referenced stencil def self.[](key = nil) Stencil.class_variable_set(:@@available_stencils, {}) unless Stencil.class_variable_defined? :@@available_stencils return @@available_stencils[key] if key @@available_stencils end # Smart load a stencil. This will attemp to parse it as json, then load it as a url, and finally try to load a file. # # @param string [String] string to attempt to smart load. # @return [Stencil] loaded stencil def self.load(string) begin data = load_string Stencil.parse_json(string) rescue ArgumentError return load_url(string) if string.start_with? 'http' return load_file string end load_string(string) if data end # Attempt to open a local file for reading, loading in the JSON if it is able. # # @param file [String] file of the file to load. # @raise [RuntimeError] If file could not be found # @raise [RuntimeError] If file is empty. # @return [Stencil] loaded stencil def self.load_file(file) raise "Could not find file #{file}." unless File.exist? file content = File.read(file) raise RuntimeError if content.empty? stencil = load_string(content) stencil.original_url = file stencil end # Attempt to open a local file for reading, loading in the JSON if it is able. # # @param string [String] string to be parsed and loaded. # @raise [ArgumentError] if the parsed queries is not an array # @return [Stencil] loaded stencil def self.load_string(string) data = Stencil.parse_json(string) stencil = Stencil.new stencil.name = data['name'] raise ArgumentError, "queries is not a kind of Array" unless data['queries'].is_a? Array stencil.queries = data['queries'] @@available_stencils[stencil.name] = stencil stencil end # Attempt to open a local file for reading, loading in the JSON if it is able. # # @param url [String] url to be fetched and passed to load_string # @raise [ArgumentError] if the url is not valid # @return [Stencil] loaded stencil def self.load_url(url) begin URI.parse url rescue URI::InvalidURIError raise ArgumentError, "#{url} is not a url" end load_string(Excon.get(url).body) end # Attempt to open a local file for reading, loading in the JSON if it is able. # # @param string [String] string is parsed and returned as a hash # @raise [ArgumentError] if the arugment is not a type of a string # @raise [ArgumentError] if the string could not be parsed # @return [Hash] hash of the data from the json def self.parse_json(string) raise ArgumentError, "#{string.class} is not a kind of String" unless string&.is_a?(String) begin data = Oj.load(string) rescue Oj::ParseError raise ArgumentError, "could not be parsed" end data end def initialize Stencil.class_variable_set(:@@available_stencils, {}) unless Stencil.class_variable_defined? :@@available_stencils @name = "" @original_url = "" @queries = [] @schema = 1 end # Attempt to open a local file for reading, loading in the JSON if it is able. # # @param account_id [String] account_id account_id to collect from # @param user [String] user to use to make the collection. # @raise [ArgumentError] if the arugment is not a type of a string # @raise [ArgumentError] if no queries are provided # @return [Collect] a collect object created from the json and parameters passed in. def to_collect(account_id: nil, user: nil) raise ArgumentError, 'queries must contain entries' unless queries.any? collect = Tasks::Collect.new collect.account_id = account_id if account_id collect.user = user if user queries.each do |q| query = Query.create service: q[:service], endpoint: q[:endpoint] collect.queries.push query end collect end end |
Class Method Details
.[](key = nil) ⇒ Stencil
find a stencil in the loaded stencils by name. when used with no parameter will show all available stencils.
25 26 27 28 29 30 |
# File 'lib/cts/mpx/aci/stencil.rb', line 25 def self.[](key = nil) Stencil.class_variable_set(:@@available_stencils, {}) unless Stencil.class_variable_defined? :@@available_stencils return @@available_stencils[key] if key @@available_stencils end |
.load(string) ⇒ Stencil
Smart load a stencil. This will attemp to parse it as json, then load it as a url, and finally try to load a file.
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/cts/mpx/aci/stencil.rb', line 36 def self.load(string) begin data = load_string Stencil.parse_json(string) rescue ArgumentError return load_url(string) if string.start_with? 'http' return load_file string end load_string(string) if data end |
.load_file(file) ⇒ Stencil
Attempt to open a local file for reading, loading in the JSON if it is able.
54 55 56 57 58 59 60 61 62 63 |
# File 'lib/cts/mpx/aci/stencil.rb', line 54 def self.load_file(file) raise "Could not find file #{file}." unless File.exist? file content = File.read(file) raise RuntimeError if content.empty? stencil = load_string(content) stencil.original_url = file stencil end |
.load_string(string) ⇒ Stencil
Attempt to open a local file for reading, loading in the JSON if it is able.
70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/cts/mpx/aci/stencil.rb', line 70 def self.load_string(string) data = Stencil.parse_json(string) stencil = Stencil.new stencil.name = data['name'] raise ArgumentError, "queries is not a kind of Array" unless data['queries'].is_a? Array stencil.queries = data['queries'] @@available_stencils[stencil.name] = stencil stencil end |
.load_url(url) ⇒ Stencil
Attempt to open a local file for reading, loading in the JSON if it is able.
88 89 90 91 92 93 94 95 96 |
# File 'lib/cts/mpx/aci/stencil.rb', line 88 def self.load_url(url) begin URI.parse url rescue URI::InvalidURIError raise ArgumentError, "#{url} is not a url" end load_string(Excon.get(url).body) end |
.parse_json(string) ⇒ Hash
Attempt to open a local file for reading, loading in the JSON if it is able.
104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/cts/mpx/aci/stencil.rb', line 104 def self.parse_json(string) raise ArgumentError, "#{string.class} is not a kind of String" unless string&.is_a?(String) begin data = Oj.load(string) rescue Oj::ParseError raise ArgumentError, "could not be parsed" end data end |
Instance Method Details
#to_collect(account_id: nil, user: nil) ⇒ Collect
Attempt to open a local file for reading, loading in the JSON if it is able.
131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/cts/mpx/aci/stencil.rb', line 131 def to_collect(account_id: nil, user: nil) raise ArgumentError, 'queries must contain entries' unless queries.any? collect = Tasks::Collect.new collect.account_id = account_id if account_id collect.user = user if user queries.each do |q| query = Query.create service: q[:service], endpoint: q[:endpoint] collect.queries.push query end collect end |