Class: YellowPages::Category

Inherits:
Object
  • Object
show all
Defined in:
lib/yellow_pages/category.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code: nil, key: nil) ⇒ Category

Returns a new instance of Category.

Raises:

  • (ArgumentError)


33
34
35
36
37
# File 'lib/yellow_pages/category.rb', line 33

def initialize(code: nil, key: nil)
  raise ArgumentError, "Either code or key must be provided" if code.nil? && key.nil?

  @code, @key = code, key
end

Class Method Details

.categories_by_codeHash

Returns all categories keyed by their MCC code.

Returns:

  • (Hash)

    all categories keyed by their MCC code



9
10
11
12
13
14
15
16
17
# File 'lib/yellow_pages/category.rb', line 9

def self.categories_by_code
  @categories_by_code ||=
    begin
      path = Pathname.new(__dir__).join("categories.yaml")
      YAML.load_file(path).map do |code, obj|
        [code.to_s, obj.merge(code:).transform_keys(&:to_sym)]
      end.to_h
    end
end

.categories_by_keyHash

Returns all categories keyed by their stripe key.

Returns:

  • (Hash)

    all categories keyed by their stripe key



20
21
22
23
24
25
26
27
# File 'lib/yellow_pages/category.rb', line 20

def self.categories_by_key
  @categories_by_key ||=
    begin
      categories_by_code.map do |code, obj|
        [obj.fetch(:key), obj]
      end.to_h
    end
end

.lookupObject



39
40
41
# File 'lib/yellow_pages/category.rb', line 39

def self.lookup(...)
  new(...)
end

Instance Method Details

#codeString?

Returns category’s code

Returns:

  • (String, nil)

    the code of the category, if found



46
47
48
# File 'lib/yellow_pages/category.rb', line 46

def code
  category&.fetch(:code)
end

#in_dataset?Boolean

Returns true if the category is in the dataset

Returns:

  • (Boolean)

    whether the category is in our dataset



66
67
68
# File 'lib/yellow_pages/category.rb', line 66

def in_dataset?
  !category.nil?
end

#keyString?

Returns category’s key

Returns:

  • (String, nil)

    the key of the category, if found



60
61
62
# File 'lib/yellow_pages/category.rb', line 60

def key
  category&.fetch(:key)
end

#nameString?

Returns category’s name

Returns:

  • (String, nil)

    the name of the category, if found



53
54
55
# File 'lib/yellow_pages/category.rb', line 53

def name
  category&.fetch(:name)
end