Class: Pandata::DataFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/pandata/data_formatter.rb

Overview

Sorts and formats Pandata::Scraper data as a string for printing.

Instance Method Summary collapse

Instance Method Details

#albums(albums) ⇒ Object

Returns a string with albums grouped under owning artist.

Parameters:

  • albums (Array)

    array of hashes with :artist and :album keys



31
32
33
# File 'lib/pandata/data_formatter.rb', line 31

def albums(albums)
  artists_items(albums, :album)
end

#followx(data) ⇒ Object

Returns a string with followers/ing sorted by webname.

Parameters:

  • data (Array)

    array of hashes with :name, :webname and :href keys



37
38
39
40
41
42
43
44
45
# File 'lib/pandata/data_formatter.rb', line 37

def followx(data)
  str = ''
  data.sort_by { |item| item[:webname].downcase }.each do |hash|
    str << "  - name: #{hash[:name]}\n"
    str << "    webname: #{hash[:webname]}\n"
    str << "    href: #{hash[:href]}\n"
  end
  str
end

#list(data) ⇒ Object

Returns a string with each array item on its own line.

Parameters:

  • data (Array, String)


10
11
12
13
14
15
# File 'lib/pandata/data_formatter.rb', line 10

def list(data)
  data = [data] unless data.kind_of?(Array)
  str = ''
  data.each { |item| str << "  - #{item}\n" }
  str
end

#sort_list(data) ⇒ Object

Identical to #list but sorts alphabetically ignoring ‘the’.

Parameters:

  • data (Array)


19
20
21
# File 'lib/pandata/data_formatter.rb', line 19

def sort_list(data)
  list custom_sort(data)
end

#tracks(tracks) ⇒ Object

Returns a string with tracks grouped under owning artist.

Parameters:

  • tracks (Array)

    array of hashes with :artist and :track keys



25
26
27
# File 'lib/pandata/data_formatter.rb', line 25

def tracks(tracks)
  artists_items(tracks, :track)
end