Module: Atlasq::Format

Defined in:
lib/atlasq/format.rb

Class Method Summary collapse

Class Method Details

.brief_template(title:, elements:) ⇒ String

*

  • Title

        • *

(attr1 | attr2 | attr3) (attr1 | attr2 | attr3) (attr1 | attr2 | attr3)

*

  • Title

        • *

  • Heading 1.0 (attr1 | attr2 | attr3) (attr1 | attr2 | attr3)

  • Heading 2.0 (attr1 | attr2 | attr3)

Examples:

for elements [Array<String>]

for elements [Hash<String, Array<String>>]

Parameters:

  • title (String)
  • elements (Array<String>, Hash<String, Array<String>>)

Returns:

  • (String)


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/atlasq/format.rb', line 41

def self.brief_template(title:, elements:)
  elements =
    case elements
    when Array
      elements.sort
    when Hash
      elements
        .sort_by(&:first)
        .each_with_object([]) do |(key, values), array|
          array << "- #{key}"
          values.sort.each { |value| array << "    #{value}" }
        end
    else
      raise Error, "Unknown elements type: #{elements.class}"
    end

  [
    Format.title(title),
    *elements,
  ].join("\n")
end

.countries(country_codes, title:) ⇒ String

Parameters:

  • country_codes (Array<String>)

    ISO3166 2 letter country codes

  • title (String)

Returns:

  • (String)


66
67
68
69
70
71
72
73
# File 'lib/atlasq/format.rb', line 66

def self.countries(country_codes, title:)
  Format.brief_template(
    title: title,
    elements: country_codes.map do |country|
      Format.one_line_country(country)
    end
  )
end

.country(country_code) ⇒ String

Parameters:

  • country_code (String)

    ISO3166 2 letter country code

Returns:

  • (String)


77
78
79
80
81
82
83
# File 'lib/atlasq/format.rb', line 77

def self.country(country_code)
  [
    Format.country_title(country_code),
    Format.one_line_country(country_code),
    Format.multiline_country(country_code),
  ].join("\n")
end

.country_title(country_code) ⇒ String

Parameters:

  • country_code (String)

    ISO3166 2 letter country code

Returns:

  • (String)


87
88
89
90
91
92
# File 'lib/atlasq/format.rb', line 87

def self.country_title(country_code)
  Cache
    .get("formatted_output/country_title.json")
    .fetch(country_code)
    .then(&Format.method(:title))
end

.currencies(currencies, partial_match: false) ⇒ String

Parameters:

  • currencies (Hash<String, Array<String>>)

    3 letter currency code to ISO3166 2 letter country codes

  • partial_match (Boolean) (defaults to: false)

    defaults to false

Returns:

  • (String)


126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/atlasq/format.rb', line 126

def self.currencies(currencies, partial_match: false)
  currencies = currencies.to_h do |currency, countries|
    [
      Format.one_line_currency(currency),
      countries.map(&Format.method(:one_line_country)),
    ]
  end

  if !partial_match && currencies.size == 1
    title, elements = currencies.first
    title = "Currency: #{title}"
    Format.brief_template(title: title, elements: elements)
  else
    title = partial_match ? "Currencies (Partial Match)" : "Currencies"
    Format.brief_template(title: title, elements: currencies)
  end
end

.languages(languages, partial_match: false) ⇒ String

Parameters:

  • languages (Hash<String, Array<String>>)

    2 letter ISO639 language code to ISO3166 2 letter country codes

  • partial_match (Boolean) (defaults to: false)

    defaults to false

Returns:

  • (String)


155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/atlasq/format.rb', line 155

def self.languages(languages, partial_match: false)
  languages = languages.to_h do |language, countries|
    [
      Format.one_line_language(language),
      countries.map(&Format.method(:one_line_country)),
    ]
  end

  if !partial_match && languages.size == 1
    title, elements = languages.first
    title = "Language: #{title}"
    Format.brief_template(title: title, elements: elements)
  else
    title = partial_match ? "Languages (Partial Match)" : "Languages"
    Format.brief_template(title: title, elements: languages)
  end
end

.multiline_country(country_code) ⇒ String

Parameters:

  • country_code (String)

    ISO3166 2 letter country code

Returns:

  • (String)


104
105
106
107
108
# File 'lib/atlasq/format.rb', line 104

def self.multiline_country(country_code)
  Cache
    .get("formatted_output/multiline_country.json")
    .fetch(country_code)
end

.one_line_country(country_code) ⇒ String

Parameters:

  • country_code (String)

    ISO3166 2 letter country code

Returns:

  • (String)


96
97
98
99
100
# File 'lib/atlasq/format.rb', line 96

def self.one_line_country(country_code)
  Cache
    .get("formatted_output/one_line_country.json")
    .fetch(country_code)
end

.one_line_currency(currency_code) ⇒ String

Parameters:

  • currency_code (String)

Returns:

  • (String)


146
147
148
149
150
# File 'lib/atlasq/format.rb', line 146

def self.one_line_currency(currency_code)
  Cache
    .get("formatted_output/one_line_currency.json")
    .fetch(currency_code)
end

.one_line_language(language) ⇒ String

Parameters:

  • language (String)

    2 letter ISO639 language code

Returns:

  • (String)


175
176
177
178
179
# File 'lib/atlasq/format.rb', line 175

def self.one_line_language(language)
  Cache
    .get("formatted_output/one_line_language.json")
    .fetch(language)
end

.subregions(subregions) ⇒ String

Parameters:

  • subregions (Hash<String, Array<String>>)

    region name to ISO3166 2 letter country codes

Returns:

  • (String)


112
113
114
115
116
117
118
119
120
121
# File 'lib/atlasq/format.rb', line 112

def self.subregions(subregions)
  subregions = subregions.to_h do |region, countries|
    [
      Util::String.titleize(region),
      countries.map(&Format.method(:one_line_country)),
    ]
  end

  Format.brief_template(title: "All Subregions", elements: subregions)
end

.title(title) ⇒ String

*

  • Title

        • *

Parameters:

  • title (String)

Returns:

  • (String)


12
13
14
15
16
17
18
# File 'lib/atlasq/format.rb', line 12

def self.title(title)
  [
    "*",
    "* #{title}",
    "*#{" *" * ((title.size / 2) + 2)}",
  ].join("\n")
end