Class: Chimps::Typewriter

Inherits:
Array show all
Defined in:
lib/chimps/typewriter.rb

Overview

Responses from Infochimps (once parsed from the original JSON or YAML) consist of nested hashes:

{ 'dataset' => {
                 'title'       => 'My dataset',
                 'description' => 'An amazing dataset which...',
                 ...
                 'sources' => {
                               'source' => {
                                             'title' => 'Trustworthy Source'
                                             ...
                                           },
                               'source' => {..},
                               ...
                               }
                },
  ...
}

This class utilizes a typewriter and a team of trained chimpanizes to create pretty, line-oriented output from these hashes.

Constant Summary collapse

RESOURCE_FIELDS =

Fields to print for each resource. Given as humanized names, will be automatically converted to key names.

["ID", "Cached Slug", "Updated At", "Title"]
FIELD_SEPARATOR =

String to insert between fields in output.

"    "

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response, options = {}) ⇒ Chimps::Typewriter

Return a Typewriter to print data.

Parameters:



44
45
46
47
48
49
50
# File 'lib/chimps/typewriter.rb', line 44

def initialize response, options={}
  super()
  @response          = response
  @column_widths     = []
  @skip_column_names = options[:skip_column_names]
  accumulate(response)
end

Instance Attribute Details

#column_widthsObject

Widths of columns as determined by the maximum number of characters in any row.



31
32
33
# File 'lib/chimps/typewriter.rb', line 31

def column_widths
  @column_widths
end

#responseObject

The response that this Typewriter will print.



27
28
29
# File 'lib/chimps/typewriter.rb', line 27

def response
  @response
end

Instance Method Details

#accumulate(obj) ⇒ Object

Accumulate lines to print from obj.

If obj is a string then it will be accumulated as a single line to print.

If obj is an Array then each element will be passed to Chimps::Typewriter#accumulate.

If obj is a Hash then each key will be mapped to a method accumulate_KEY and the corresponding value passed in. This method is responsible for accumulating lines to print.

Parameters:



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/chimps/typewriter.rb', line 90

def accumulate obj
  case obj
  when Hash
    obj.each_pair do |resource_name, resource_data|
      case 
      when %w[datasets sources licenses].include?(resource_name.to_s)
        accumulate_listing(resource_data)
      when %w[dataset source license].include?(resource_name.to_s)
        accumulate_resource(resource_name, resource_data)
      when %w[errors batch search api_account message].include?(resource_name.to_s)
        send("accumulate_#{resource_name}", resource_data)
      when :array  == resource_name         # constructed by Chimps::Response
        accumulate_listing(resource_data)
      when :string == resource_name         # constructed by Chimps::Response 
        self << obj[:string]
      else
        $stderr.puts resource_data.inspect if Chimps.verbose?
        raise PrintingError.new("Unrecognized resource type `#{resource_name}'.")
      end
    end
  when Array
    obj.each { |element| accumulate(element) }
  when String
    self << obj
  else 
    raise PrintingError.new("Cannot print a #{obj.class}")
  end
end

Print the accumulated lines in this Typewriter to the given output (defaults to $stdout).

Will first calculate appropriate column widths for any Array-like lines.

Parameters:

  • output (#puts) (defaults to: $stdout)


66
67
68
69
70
71
72
73
74
75
# File 'lib/chimps/typewriter.rb', line 66

def print output=$stdout
  calculate_column_widths!
  each do |line|
    if line.is_a?(Array)
      output.puts pad_and_join(line)
    else
      output.puts line
    end
  end
end

#skip_column_names?true?

Print column names as well as values?

Returns:

  • (true, nil)


55
56
57
# File 'lib/chimps/typewriter.rb', line 55

def skip_column_names?
  @skip_column_names
end