Class: BooticCli::Console

Inherits:
Object
  • Object
show all
Includes:
Connectivity
Defined in:
lib/bootic_cli/console.rb

Constant Summary collapse

SEPCOUNT =
80.freeze

Constants included from Connectivity

BooticCli::Connectivity::DEFAULT_ENV

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(session) ⇒ Console

Returns a new instance of Console.



9
10
11
# File 'lib/bootic_cli/console.rb', line 9

def initialize(session)
  @session = session
end

Instance Attribute Details

#sessionObject (readonly)

Returns the value of attribute session.



7
8
9
# File 'lib/bootic_cli/console.rb', line 7

def session
  @session
end

Instance Method Details

#explain(entity, include_links = true) ⇒ 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
# File 'lib/bootic_cli/console.rb', line 13

def explain(entity, include_links = true)
  if entity.is_a?(String) || entity.is_a?(Integer)
    puts entity
    return
  end

  if entity.is_a?(Array)
    entity.each{|e| explain(e) }
    return
  end

  title 'PROPERTIES:'
  puts table(entity.properties)

  puts ''

  if include_links
    title 'LINKS (explain_link <ENTITY>, <LINK_NAME>):'
    puts links(entity.rels)

    puts ''
  end

  title 'ENTITIES (explain <SUBENTITY>):'
  puts entity.entities.keys.join("\r\n")

  puts '-' * SEPCOUNT
  puts ''
  nil
end


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/bootic_cli/console.rb', line 44

def explain_link(entity, key)
  return "This entity does not appear to have links" unless entity.respond_to?(:rels)
  rel = entity.rels[key.to_sym]
  return "This entity does not have link '#{key}'" unless rel

  data = [
    ['name', key],
    ['type', rel.type],
    ['title', rel.title],
    ['method', rel.transport_method],
    ['docs', rel.docs],
    ['href', rel.href],
    ['parameters', rel.parameters.join(', ')]
  ]

  puts table(data)
  puts ''
  token = session.config[:access_token]
  puts %(curl -i -H "Authorization: Bearer #{token}" "#{rel.href}")

  nil
end

#list(entities) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/bootic_cli/console.rb', line 67

def list(entities)
  if entities.respond_to?(:each)
    entities.each{|e| explain(e, false)}
    puts ''
    if entities.respond_to?(:total_items)
      puts "Page #{entities.page} of #{(entities.total_items / entities.per_page) + 1}. Total items #{entities.total_items}"
    end
    if entities.respond_to?(:next)
      @last_in_list = entities
      puts "There is more. run 'more'"
    else
      @last_in_list = nil
      puts "End of list"
    end
  else
    explain entities
  end

  nil
end

#moreObject



88
89
90
91
# File 'lib/bootic_cli/console.rb', line 88

def more
  puts "End of list" unless @last_in_list
  list @last_in_list.next
end