Class: RateCenter::DataSource::LocalCallingGuide

Inherits:
Object
  • Object
show all
Defined in:
lib/rate_center/data_source/local_calling_guide.rb

Defined Under Namespace

Classes: Client, ResponseParser

Constant Summary collapse

SPECIAL_CASE_LATAS =
[
  OpenStruct.new(code: "45010", region: "GA", country: "US")
].freeze
COUNTRIES =
[ :us, :ca ].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**options) ⇒ LocalCallingGuide

Returns a new instance of LocalCallingGuide.



19
20
21
# File 'lib/rate_center/data_source/local_calling_guide.rb', line 19

def initialize(**options)
  @client = options.fetch(:client) { Client.new }
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



17
18
19
# File 'lib/rate_center/data_source/local_calling_guide.rb', line 17

def client
  @client
end

#data_directoryObject (readonly)

Returns the value of attribute data_directory.



17
18
19
# File 'lib/rate_center/data_source/local_calling_guide.rb', line 17

def data_directory
  @data_directory
end

Instance Method Details

#load_data!(**options) ⇒ Object



84
85
86
87
88
89
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
118
119
120
121
122
123
# File 'lib/rate_center/data_source/local_calling_guide.rb', line 84

def load_data!(**options)
  ::RateCenter.load(:lata, :all)

  COUNTRIES.each do |country|
    data_directory = options.fetch(:data_directory).join(country.to_s)
    FileUtils.mkdir_p(data_directory)

    regions = Array(regions_for(country))

    Array(regions).each do |region, _|
      data_file = data_directory.join("#{region.downcase}.json")

      rate_centers = fetch_rate_centers_for(region)
      SPECIAL_CASE_LATAS.select { |lata| lata.region == region }.each do |lata|
        rate_centers.concat(client.fetch_rate_center_data(region:, lata: lata.code).data)
      end
      rate_centers.uniq!(&:exch)

      next if rate_centers.empty?

      data = rate_centers.sort_by { |rc| [ (rc.rcshort || rc.rc), rc.exch ] }.map do |rate_center|
        related_rate_center = rate_centers.find { |rc| rc.exch == rate_center.see_exch } unless rate_center.see_exch.nil?

        {
          "country" => country.to_s.upcase,
          "region" => region,
          "exchange" => rate_center.exch,
          "name" => (rate_center.rcshort || rate_center.rc).strip.upcase,
          "full_name" => rate_center.rc,
          "lata" => rate_center.lata.slice(0, 3),
          "ilec_name" => rate_center.ilec_name,
          "lat" => rate_center.rc_lat || related_rate_center&.rc_lat,
          "long" => rate_center.rc_lon || related_rate_center&.rc_lon
        }
      end

      data_file.write(JSON.pretty_generate("rate_centers" => data))
    end
  end
end