Class: Occi::Cli::ResourceOutputFactory
- Inherits:
-
Object
- Object
- Occi::Cli::ResourceOutputFactory
- Defined in:
- lib/occi/cli/resource_output_factory.rb
Constant Summary collapse
- @@allowed_formats =
[:json, :plain, :json_pretty, :json_extended, :json_extended_pretty].freeze
Instance Attribute Summary collapse
-
#output_format ⇒ Object
readonly
Returns the value of attribute output_format.
Class Method Summary collapse
Instance Method Summary collapse
-
#extended_json(resource) ⇒ Hash
Renders Occi::Core::Links as a full JSON, not just a String array.
- #format(data) ⇒ Object
-
#initialize(output_format = :plain) ⇒ ResourceOutputFactory
constructor
A new instance of ResourceOutputFactory.
- #kinds_to_json(occi_kinds) ⇒ Object (also: #kinds_to_json_pretty, #kinds_to_json_extended_pretty, #kinds_to_json_extended)
- #kinds_to_plain(occi_kinds) ⇒ Object
- #links_to_plain(occi_links) ⇒ Object
- #locations_to_json(url_locations) ⇒ Object (also: #locations_to_json_pretty, #locations_to_json_extended_pretty, #locations_to_json_extended)
- #locations_to_plain(url_locations) ⇒ Object
- #mixins_to_plain(occi_mixins) ⇒ Object
- #resources_to_json(occi_resources) ⇒ Object (also: #resources_to_json_pretty, #links_to_json, #links_to_json_pretty, #mixins_to_json, #mixins_to_json_pretty, #resources_to_json_extended, #resources_to_json_extended_pretty, #links_to_json_extended, #links_to_json_extended_pretty, #mixins_to_json_extended, #mixins_to_json_extended_pretty)
- #resources_to_plain(occi_resources) ⇒ Object
Constructor Details
#initialize(output_format = :plain) ⇒ ResourceOutputFactory
Returns a new instance of ResourceOutputFactory.
12 13 14 15 16 |
# File 'lib/occi/cli/resource_output_factory.rb', line 12 def initialize(output_format = :plain) raise Occi::Cli::Errors::FormatterOutputTypeError, "Format #{output_format.inspect} is not supported!" unless @@allowed_formats.include? output_format @output_format = output_format end |
Instance Attribute Details
#output_format ⇒ Object (readonly)
Returns the value of attribute output_format.
10 11 12 |
# File 'lib/occi/cli/resource_output_factory.rb', line 10 def output_format @output_format end |
Class Method Details
.allowed_formats ⇒ Object
40 41 42 |
# File 'lib/occi/cli/resource_output_factory.rb', line 40 def self.allowed_formats @@allowed_formats end |
Instance Method Details
#extended_json(resource) ⇒ Hash
Renders Occi::Core::Links as a full JSON, not just a String array.
86 87 88 89 90 91 92 93 94 |
# File 'lib/occi/cli/resource_output_factory.rb', line 86 def extended_json(resource) return resource.as_json.to_hash unless resource.respond_to?(:links) return resource.as_json.to_hash unless resource.links.kind_of?(Occi::Core::Links) links = resource.links ext_json = resource.as_json ext_json.links = links.as_json ext_json.to_hash end |
#format(data) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/occi/cli/resource_output_factory.rb', line 18 def format(data) # construct a method name from data type and output format if data.kind_of? Occi::Core::Resources method = "resources_to_#{@output_format}".to_sym elsif data.kind_of? Occi::Core::Links method = "links_to_#{@output_format}".to_sym elsif data.kind_of? Occi::Core::Mixins method = "mixins_to_#{@output_format}".to_sym elsif data.kind_of? Occi::Core::Kinds method = "kinds_to_#{@output_format}".to_sym elsif data.kind_of? Array raise Occi::Cli::Errors::FormatterInputTypeError, "Arrays with #{data.first.class.name.inspect} are not supported!" unless data.first.nil? || data.first.kind_of?(String) method = "locations_to_#{@output_format}".to_sym else raise Occi::Cli::Errors::FormatterInputTypeError, "Data format #{data.class.name.inspect} is not supported!" end send method, data end |
#kinds_to_json(occi_kinds) ⇒ Object Also known as: kinds_to_json_pretty, kinds_to_json_extended_pretty, kinds_to_json_extended
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/occi/cli/resource_output_factory.rb', line 156 def kinds_to_json(occi_kinds) # generate JSON document from Occi::Core::Kinds occi_kinds = occi_kinds.to_a if @output_format.to_s.end_with? '_pretty' output_first = "[\n" output_ary = occi_kinds.collect do |r| JSON.pretty_generate(r.as_json.to_hash) end separator = ",\n" output_last = "\n]" else output_first = "[" output_ary = occi_kinds.collect do |r| JSON.generate(r.as_json.to_hash) end separator = "," output_last = "]" end "#{output_first}#{output_ary.join(separator)}#{output_last}" end |
#kinds_to_plain(occi_kinds) ⇒ Object
145 146 147 148 149 150 151 152 153 154 |
# File 'lib/occi/cli/resource_output_factory.rb', line 145 def kinds_to_plain(occi_kinds) # using ERB templates for known kinds file = "#{File.('..', __FILE__)}/templates/kinds.erb" template = ERB.new(File.new(file).read, nil, '-') formatted_output = "" formatted_output << template.result(binding) unless occi_kinds.blank? formatted_output end |
#links_to_plain(occi_links) ⇒ Object
119 120 121 122 123 124 125 126 127 128 |
# File 'lib/occi/cli/resource_output_factory.rb', line 119 def links_to_plain(occi_links) # using ERB templates for known link types file = "#{File.('..', __FILE__)}/templates/links.erb" template = ERB.new(File.new(file).read, nil, '-') formatted_output = "" formatted_output << template.result(binding) unless occi_links.blank? formatted_output end |
#locations_to_json(url_locations) ⇒ Object Also known as: locations_to_json_pretty, locations_to_json_extended_pretty, locations_to_json_extended
96 97 98 99 100 101 102 103 |
# File 'lib/occi/cli/resource_output_factory.rb', line 96 def locations_to_json(url_locations) # generate JSON document from an array of strings if @output_format == :json_pretty JSON.pretty_generate url_locations else JSON.generate url_locations end end |
#locations_to_plain(url_locations) ⇒ Object
141 142 143 |
# File 'lib/occi/cli/resource_output_factory.rb', line 141 def locations_to_plain(url_locations) url_locations.join("\n") end |
#mixins_to_plain(occi_mixins) ⇒ Object
130 131 132 133 134 135 136 137 138 139 |
# File 'lib/occi/cli/resource_output_factory.rb', line 130 def mixins_to_plain(occi_mixins) # using ERB templates for known mixin types file = "#{File.('..', __FILE__)}/templates/mixins.erb" template = ERB.new(File.new(file).read, nil, '-') formatted_output = "" formatted_output << template.result(binding) unless occi_mixins.blank? formatted_output end |
#resources_to_json(occi_resources) ⇒ Object Also known as: resources_to_json_pretty, links_to_json, links_to_json_pretty, mixins_to_json, mixins_to_json_pretty, resources_to_json_extended, resources_to_json_extended_pretty, links_to_json_extended, links_to_json_extended_pretty, mixins_to_json_extended, mixins_to_json_extended_pretty
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/occi/cli/resource_output_factory.rb', line 44 def resources_to_json(occi_resources) # generate JSON document from Occi::Core::Resources occi_resources = occi_resources.to_a if @output_format.to_s.end_with? '_pretty' output_first = "[\n" output_ary = occi_resources.collect do |r| local_hash = @output_format.to_s.include?('_extended') ? extended_json(r) : r.as_json.to_hash JSON.pretty_generate(local_hash) end separator = ",\n" output_last = "\n]" else output_first = "[" output_ary = occi_resources.collect do |r| local_hash = @output_format.to_s.include?('_extended') ? extended_json(r) : r.as_json.to_hash JSON.generate(local_hash) end separator = "," output_last = "]" end "#{output_first}#{output_ary.join(separator)}#{output_last}" end |
#resources_to_plain(occi_resources) ⇒ Object
108 109 110 111 112 113 114 115 116 117 |
# File 'lib/occi/cli/resource_output_factory.rb', line 108 def resources_to_plain(occi_resources) # using ERB templates for known resource types file = "#{File.('..', __FILE__)}/templates/resources.erb" template = ERB.new(File.new(file).read, nil, '-') formatted_output = "" formatted_output << template.result(binding) unless occi_resources.blank? formatted_output end |