Class: Retscli::DisplayAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/retscli/display_adapter.rb

Constant Summary collapse

NO_RESULTS =
'No Results'.freeze
EMPTY_VALUE =
'<empty>'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ DisplayAdapter

Returns a new instance of DisplayAdapter.



8
9
10
11
# File 'lib/retscli/display_adapter.rb', line 8

def initialize(client)
  @client = client
  @colorer = ::Thor::Shell::Color.new
end

Instance Method Details

#capabilitiesObject



18
19
20
# File 'lib/retscli/display_adapter.rb', line 18

def capabilities
  Terminal::Table.new(:rows => @client.capabilities.to_a)
end

#classes(resource) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/retscli/display_adapter.rb', line 28

def classes(resource)
  resource_tree = .tree[resource.downcase]

  if resource_tree
    resource_tree.rets_classes.map do |klass|
      render_class(klass)
    end.join("\n")
  else
    set_color("#{resource} resource does not exist", :red)
  end
end

#loginObject



13
14
15
16
# File 'lib/retscli/display_adapter.rb', line 13

def 
  @client.
  set_color("\u2713 Logged in", :green)
end

#metadataObject



80
81
82
# File 'lib/retscli/display_adapter.rb', line 80

def 
  build_tree
end

#objects(resource) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/retscli/display_adapter.rb', line 40

def objects(resource)
  resource_tree = .tree[resource.downcase]

  if resource_tree
    resource_tree.rets_objects.map do |object|
      render_object(object)
    end.join("\n")
  else
    set_color("#{resource} resource does not exist", :red)
  end
end

#open_in_editor(text, editor = 'editor') ⇒ Object



151
152
153
154
# File 'lib/retscli/display_adapter.rb', line 151

def open_in_editor(text, editor='editor')
  editor = editor == 'editor' ? (ENV['EDITOR'] || 'nano') : editor
  open_tempfile_with_content(editor, text)
end

#page(output) ⇒ Object



143
144
145
146
147
148
149
# File 'lib/retscli/display_adapter.rb', line 143

def page(output)
  begin
    pager = ENV['PAGER'] || 'less'
    IO.popen(pager, 'w') { |f| f.puts(output) }
  rescue Errno::EPIPE
  end
end

#resourcesObject



22
23
24
25
26
# File 'lib/retscli/display_adapter.rb', line 22

def resources
  .tree.map do |key, resource|
    render_resource(resource)
  end.join("\n")
end

#search(resource, klass, query, options = {}) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/retscli/display_adapter.rb', line 119

def search(resource, klass, query, options={})
  select = options[:select] ? options[:select].join(',') : ''
  count = options[:count] ? 2 : 0

  results = @client.find(
    :all,
    search_type: resource,
    class: klass,
    query: query,
    select: select,
    limit: options[:limit],
    offset: options[:offset],
    count: count,
    format: options[:format],
    no_records_not_an_error: true
  )

  if results.is_a?(Integer)
    resource_table([{ 'count' => results }])
  else
    resource_table(results)
  end
end

#search_metadata(search, options = {}) ⇒ Object



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
# File 'lib/retscli/display_adapter.rb', line 84

def (search, options={})
  options = { :resources => [], :classes => [] , :color => true }.merge(options)
  search_results = ''
  resources = options[:resources].map!{ |res| res.downcase }
  classes = options[:classes].map!{ |klass| klass.downcase }


  .tree.each do |key, res|
    next if !resources.empty? && !resources.include?(res.id.downcase)
    match_found_for_resouce = false

    res.rets_classes.each do |klass|
      next if !classes.empty? && !classes.include?(klass.name.downcase)
      match_found_for_class = false

      klass.tables.each do |table|
        if match = search_table(table, search)
          search_results << render_resource(res) << "\n" unless match_found_for_resouce
          search_results << tab_over(render_class(klass), 1) << "\n" unless match_found_for_class

          rendered_table = tab_over(render_table(table), 2)
          search_results << rendered_table.gsub!(match.regexp) do |sub|
            options[:color] ? set_color(sub, :red, :on_white) : sub
          end << "\n"

          match_found_for_resouce = true
          match_found_for_class = true
        end
      end
    end
  end

  search_results
end

#tables(resource, klass) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/retscli/display_adapter.rb', line 52

def tables(resource, klass)
  resource_tree = .tree[resource.downcase]
  return set_color("#{resource} resource does not exist", :red) unless resource_tree

  resource_class = resource_tree.rets_classes.detect { |rc| rc.name.downcase == klass.downcase }
  return set_color("#{klass} class does not exist", :red) unless resource_class

  resource_class.tables.map do |table|
    render_table(table)
  end.join("\n")
end

#timezone_offsetObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/retscli/display_adapter.rb', line 64

def timezone_offset
  offset = 
    .[:system]
    .first
    .fragment
    .xpath('SYSTEM')
    .attribute('TimeZoneOffset')
    .to_s

  if offset.empty?
    set_color("No offset specified", :red)
  else
    offset
  end
end