Class: AnalyticsOps::CLI::Presenter
- Inherits:
-
Object
- Object
- AnalyticsOps::CLI::Presenter
- Includes:
- PresenterCSV
- Defined in:
- lib/analytics_ops/cli/presenter.rb
Overview
Formats gem-owned values for terminal and automation consumers.
Constant Summary collapse
- OVERVIEW_LABELS =
{ "overview_totals" => "Totals", "overview_trend" => "Daily trend", "overview_acquisition" => "Traffic acquisition", "overview_landing_pages" => "Landing pages", "overview_devices" => "Devices" }.freeze
Instance Method Summary collapse
- #human_plan(plan, detailed: false) ⇒ Object
-
#initialize(out:, format:) ⇒ Presenter
constructor
A new instance of Presenter.
- #json(value) ⇒ Object
- #render(value, status: CLI::SUCCESS) ⇒ Object
- #render_connections(connections) ⇒ Object
- #render_profiles(profiles) ⇒ Object
- #render_properties(accounts) ⇒ Object
- #render_selection(selection) ⇒ Object
Constructor Details
#initialize(out:, format:) ⇒ Presenter
Returns a new instance of Presenter.
19 20 21 22 |
# File 'lib/analytics_ops/cli/presenter.rb', line 19 def initialize(out:, format:) @out = out @format = format end |
Instance Method Details
#human_plan(plan, detailed: false) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/analytics_ops/cli/presenter.rb', line 93 def human_plan(plan, detailed: false) lines = [ "Plan for profile #{display(plan.profile)} / property #{display(plan.property_id)}", "Snapshot: #{display(plan.snapshot_fingerprint)}", "Changes: #{plan.changes.length}; findings: #{plan.findings.length}" ] plan.changes.each do |change| lines.concat(human_plan_change(change, detailed:)) end plan.findings.each do |finding| lines << " #{display(finding.severity).upcase.ljust(12)} #{display(finding.resource_identity)}: " \ "#{display(finding.)}" end lines.join("\n") end |
#json(value) ⇒ Object
88 89 90 91 |
# File 'lib/analytics_ops/cli/presenter.rb', line 88 def json(value) payload = serializable(value) "#{JSON.pretty_generate(Canonical.normalize(payload))}\n" end |
#render(value, status: CLI::SUCCESS) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/analytics_ops/cli/presenter.rb', line 24 def render(value, status: CLI::SUCCESS) if value.is_a?(Reports::ExportResult) @format == "json" ? @out.write(json(value)) : @out.puts(human(value)) return status end case @format when "json" @out.write(json(value)) when "csv" @out.write(csv(value)) else @out.puts(human(value)) end status end |
#render_connections(connections) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/analytics_ops/cli/presenter.rb', line 53 def render_connections(connections) return render(connections) unless @format == "human" if connections.empty? return write("No Google connections saved. Run `analytics-ops setup --service-account PATH`.") end lines = connections.map do |connection| status = connection.fetch("available") ? "ready" : "key unavailable" in_use = connection.fetch("in_use") ? " · in use" : "" "#{display(connection.fetch("name"))}: #{status}#{in_use}" end write(lines.join("\n")) end |
#render_profiles(profiles) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/analytics_ops/cli/presenter.rb', line 67 def render_profiles(profiles) return render(profiles) unless @format == "human" lines = profiles.map do |profile| marker = profile.fetch("selected") ? "*" : " " connection = profile["connection"] || "not connected" "#{marker} #{display(profile.fetch("name"))}: property #{display(profile.fetch("property_id"))} " \ "· connection #{display(connection)}" end write(lines.join("\n")) end |
#render_properties(accounts) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/analytics_ops/cli/presenter.rb', line 41 def render_properties(accounts) return render(accounts) unless @format == "human" return write("No accessible Google Analytics properties found") if accounts.empty? lines = accounts.flat_map do |account| ["Account #{display(account.id)}: #{display(account.display_name)}"] + account.properties.map do |property| " Property #{display(property.fetch("id"))}: #{display(property.fetch("display_name"))}" end end write(lines.join("\n")) end |
#render_selection(selection) ⇒ Object
79 80 81 82 83 84 85 86 |
# File 'lib/analytics_ops/cli/presenter.rb', line 79 def render_selection(selection) return render(selection) unless @format == "human" write( "Using profile #{display(selection.fetch("profile"))} with connection " \ "#{display(selection.fetch("connection"))}." ) end |