Class: Kiba::Extend::Transforms::Marc::LanguageCodeLookup

Inherits:
Object
  • Object
show all
Defined in:
lib/kiba/extend/transforms/marc/language_code_lookup.rb

Overview

Look up MARC List for Languages codes and provide preferred label value

Examples:

Known code

row = {code: "eng"}
result = Marc::LanguageCodeLookup.new(source: :code).process(row)
expect(result).to eq(row.merge({language: "English"}))

Unknown code

row = {code: "foo"}
result = Marc::LanguageCodeLookup.new(source: :code).process(row)
expect(result).to eq(row.merge({language: nil}))

Since:

  • 3.3.0

Instance Method Summary collapse

Constructor Details

#initialize(source:, target: :language) ⇒ LanguageCodeLookup

Returns a new instance of LanguageCodeLookup.

Parameters:

  • source (Symbol)

    row field containing language code to look up

  • target (Symbol) (defaults to: :language)

    row field into which language label value will be written

Since:

  • 3.3.0



27
28
29
30
31
# File 'lib/kiba/extend/transforms/marc/language_code_lookup.rb', line 27

def initialize(source:, target: :language)
  @source = source
  @target = target
  @host = URI.parse("https://id.loc.gov").hostname
end

Instance Method Details

#process(row) ⇒ Hash{ Symbol => String, nil }

Parameters:

  • row (Hash{ Symbol => String, nil })

Returns:

  • (Hash{ Symbol => String, nil })

Since:

  • 3.3.0



35
36
37
38
39
40
# File 'lib/kiba/extend/transforms/marc/language_code_lookup.rb', line 35

def process(row)
  Net::HTTP.start(host, use_ssl: true) do |http|
    row[target] = api_result(http, row[source])
  end
  row
end