Module: EnjuLoc::LocSearch::ClassMethods

Defined in:
lib/enju_loc/loc_search.rb

Constant Summary collapse

NS =
{"mods"=>"http://www.loc.gov/mods/v3"}

Instance Method Summary collapse

Instance Method Details

#import_record_from_loc(doc) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/enju_loc/loc_search.rb', line 37

def import_record_from_loc(doc)
  record_identifier = doc.at('//mods:recordInfo/mods:recordIdentifier', NS).try(:content)
  identifier_type = IdentifierType.find_by(name: 'loc_identifier')
  identifier_type = IdentifierType.create!(name: 'loc_identifier') unless identifier_type
  loc_identifier = Identifier.find_by(body: record_identifier, identifier_type_id: identifier_type.id)
  return loc_identifier.manifestation if loc_identifier

  publishers = []
  doc.xpath('//mods:publisher', NS).each do |publisher|
    publishers << {
      full_name: publisher.content,
      #:agent_identifier => publisher.attributes["about"].try(:content)
    }
  end

  creators = get_mods_creators(doc)

  # title
  titles = get_mods_titles(doc)

  # date of publication
  date = get_mods_date_of_publication(doc)

  language = Language.find_by(iso_639_2: get_mods_language(doc))
  if language
    language_id = language.id
  else
    language_id = 1
  end

  isbn = Lisbn.new(doc.at('/mods:mods/mods:identifier[@type="isbn"]', NS).try(:content).to_s).try(:isbn)
  lccn = StdNum::LCCN.normalize(doc.at('/mods:mods/mods:identifier[@type="lccn"]', NS).try(:content).to_s)
  issn = StdNum::ISSN.normalize(doc.at('/mods:mods/mods:identifier[@type="issn"]', NS).try(:content).to_s)
  issn_l = StdNum::ISSN.normalize(doc.at('/mods:mods/mods:identifier[@type="issn-l"]', NS).try(:content).to_s)

  types = get_mods_carrier_and_content_types(doc)
  content_type = types[ :content_type ]
  carrier_type = types[ :carrier_type ]

  record_identifier = doc.at('//mods:recordInfo/mods:recordIdentifier', NS).try(:content)
  description = doc.xpath('//mods:abstract', NS).collect(&:content).join("\n")
  edition_string = doc.at('//mods:edition', NS).try(:content)
  extent = get_mods_extent(doc)
  note = get_mods_note(doc)
  frequency = get_mods_frequency(doc)
  issuance = doc.at('//mods:issuance', NS).try(:content)
  is_serial = true if issuance == "serial"
  statement_of_responsibility = get_mods_statement_of_responsibility(doc)
  access_address = get_mods_access_address(doc)
  publication_place = get_mods_publication_place(doc)

  manifestation = nil
  Agent.transaction do
    creator_agents = Agent.import_agents(creators)
    publisher_agents = Agent.import_agents(publishers)

    manifestation = Manifestation.new(
      manifestation_identifier: record_identifier,
      original_title: titles[:original_title],
      title_alternative: titles[:title_alternative],
      language_id: language_id,
      pub_date: date,
      description: description,
      edition_string: edition_string,
      statement_of_responsibility: statement_of_responsibility,
      start_page: extent[:start_page],
      end_page: extent[:end_page],
      extent: extent[:extent],
      height: extent[:height],
      dimensions: extent[:dimensions],
      access_address: access_address,
      note: note,
      publication_place: publication_place,
      serial: is_serial
    )
    identifier = {}
    if isbn
      identifier[:isbn] = Identifier.new(
        manifestation: manifestation,
        body: isbn,
        identifier_type: IdentifierType.find_by(name: 'isbn') || IdnetifierType.create!(name: 'isbn')
      )
    end
    if loc_identifier
      identifier[:loc_identifier] = Identifier.new(
        manifestation: manifestation,
        body: loc_identifier,
        identifier_type: IdentifierType.find_by(name: 'loc_identifier') || IdnetifierType.create!(name: 'loc_identifier')
      )
    end
    if lccn
      identifier[:lccn] = Identifier.new(
        manifestation: manifestation,
        body: lccn,
        identifier_type: IdentifierType.find_by(name: 'lccn') || IdentifierType.create!(name: 'lccn')
      )
    end
    if issn
      identifier[:issn] = Identifier.new(
        manifestation: manifestation,
        body: issn,
        identifier_type: IdentifierType.find_by(name: 'issn') || IdentifierType.create!(name: 'issn')
      )
    end
    if issn_l
      identifier[:issn_l] = Identifier.new(
        manifestation: manifestation,
        body: issn_l,
        identifier_type: IdentifierType.find_by(name: 'issn_l') || IdentifierType.create!(name: 'issn_l')
      )
    end
    manifestation.carrier_type = carrier_type if carrier_type
    manifestation.manifestation_content_type = content_type if content_type
    manifestation.frequency = frequency if frequency
    manifestation.save!
    identifier.each do |k, v|
      manifestation.identifiers << v if v.valid?
    end
    manifestation.publishers << publisher_agents
    manifestation.creators << creator_agents
    create_loc_subject_related_elements(doc, manifestation)
    create_loc_series_statement(doc, manifestation)
    if is_serial
      create_loc_series_master(doc, manifestation)
    end
  end
  manifestation
end

#import_record_from_loc_isbn(options) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/enju_loc/loc_search.rb', line 22

def import_record_from_loc_isbn(options)
  #if options[:isbn]
  lisbn = Lisbn.new(options[:isbn])
  raise EnjuLoc::InvalidIsbn unless lisbn.valid?
  #end

  manifestation = Manifestation.find_by_isbn(lisbn.isbn)
  return manifestation.first if manifestation.present?

  doc = return_xml(lisbn.isbn)
  raise EnjuLoc::RecordNotFound unless doc
  import_record_from_loc(doc)
end

#loc_search(query, options = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/enju_loc/loc_search.rb', line 8

def loc_search(query, options = {})
  options = {}.merge(options)
  doc = nil
  results = {}
  startrecord = options[:idx].to_i
  if startrecord == 0
    startrecord = 1
  end
  url = LOC_SRU_BASEURL + "?operation=searchRetrieve&version=1.1&=query=#{ URI.escape(query) }"
  cont = Faraday.get(url).body
  parser = LibXML::XML::Parser.string(cont)
  doc = parser.parse
end