Class: Cts::Mpx::Aci::Stencil

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStencil

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

#nameString

Returns Name of the stencil.

Returns:

  • (String)

    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. =  if 
    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_urlString

Returns If applicable, the original location of the asset.

Returns:

  • (String)

    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. =  if 
    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

#queriesString

Returns Queries to perform in the collect object.

Returns:

  • (String)

    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. =  if 
    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

#schemaString

Returns Schema of this stencil.

Returns:

  • (String)

    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. =  if 
    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.

Parameters:

  • key (String) (defaults to: nil)

    name of the stencil

Returns:



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.

Parameters:

  • string (String)

    string to attempt to smart load.

Returns:



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.

Parameters:

  • file (String)

    file of the file to load.

Returns:

Raises:

  • (RuntimeError)

    If file could not be found

  • (RuntimeError)

    If file is empty.



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.

Parameters:

  • string (String)

    string to be parsed and loaded.

Returns:

Raises:

  • (ArgumentError)

    if the parsed queries is not an array



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.

Parameters:

  • url (String)

    url to be fetched and passed to load_string

Returns:

Raises:

  • (ArgumentError)

    if the url is not valid



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.

Parameters:

  • string (String)

    string is parsed and returned as a hash

Returns:

  • (Hash)

    hash of the data from the json

Raises:

  • (ArgumentError)

    if the arugment is not a type of a string

  • (ArgumentError)

    if the string could not be parsed



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.

Parameters:

  • account_id (String) (defaults to: nil)

    account_id account_id to collect from

  • user (String) (defaults to: nil)

    user to use to make the collection.

Returns:

  • (Collect)

    a collect object created from the json and parameters passed in.

Raises:

  • (ArgumentError)

    if the arugment is not a type of a string

  • (ArgumentError)

    if no queries are provided



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. =  if 
  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