Class: YellowPages::Merchant

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(network_id:) ⇒ Merchant

Returns a new instance of Merchant.



32
33
34
35
36
# File 'lib/yellow_pages/merchant.rb', line 32

def initialize(network_id:)
  @network_id = network_id

  YellowPages.missing_merchant_reporter&.call(@network_id) unless in_dataset?
end

Class Method Details

.lookupObject



38
39
40
# File 'lib/yellow_pages/merchant.rb', line 38

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

.merchantsHash

Returns all the merchants and their network IDs

Returns:

  • (Hash)

    the raw merchant hashes



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/yellow_pages/merchant.rb', line 11

def self.merchants
  @merchants ||=
    begin
      path = Pathname.new(__dir__).join("merchants.yaml")
      yaml = YAML.load_file(path)
      yaml.flat_map do |merchant|
        next if merchant["name"].nil?

        merchant["network_ids"].map do |nid|
          filename = merchant["name"].gsub(/[ '-]/, "").downcase
          filepath = Pathname.new(__dir__).join("../assets/icons/#{filename}.svg")

          [nid, {name: merchant["name"], icon_filepath: filepath}]
        end
      end.compact.to_h
    end
end

Instance Method Details

#iconString?

Returns an SVG logo icon of the merchant

Returns:

  • (String, nil)

    the SVG code of the merchant, if found



52
53
54
55
56
57
58
59
60
# File 'lib/yellow_pages/merchant.rb', line 52

def icon
  @icon ||=
    begin
      path = merchant&.fetch(:icon_filepath)
      File.read(path) if path
    rescue Errno::ENOENT
      nil
    end
end

#in_dataset?Boolean

Returns true if the merchant is in the dataset

Returns:

  • (Boolean)

    whether the merchant is in our dataset



64
65
66
# File 'lib/yellow_pages/merchant.rb', line 64

def in_dataset?
  !merchant.nil?
end

#nameString?

Returns merchant name

Returns:

  • (String, nil)

    the name of the merchant, if found



45
46
47
# File 'lib/yellow_pages/merchant.rb', line 45

def name
  merchant&.fetch(:name)
end