Class: LocSearch

Inherits:
Object
  • Object
show all
Defined in:
app/models/loc_search.rb

Direct Known Subclasses

DCRecord, ModsRecord

Defined Under Namespace

Classes: DCRecord, ModsRecord

Constant Summary collapse

LOC_SRU_BASEURL =
"http://lx2.loc.gov:210/LCDB"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node) ⇒ LocSearch

Returns a new instance of LocSearch.



2
3
4
# File 'app/models/loc_search.rb', line 2

def initialize(node)
  @node = node
end

Class Method Details

.import_from_sru_response(lccn) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'app/models/loc_search.rb', line 87

def self.import_from_sru_response(lccn)
  identifier_type_lccn = IdentifierType.where(name: 'lccn').first
  identifier_type_lccn = IdentifierType.create!(name: 'lccn') unless identifier_type_lccn
  identifier = Identifier.where(body: lccn, identifier_type_id: identifier_type_lccn.id).first
  return if identifier
  url = make_sru_request_uri("bath.lccn=\"^#{ lccn }\"")
  response = Nokogiri::XML(Faraday.get(url).body)
  record = response.at('//zs:recordData', {"zs" => "http://www.loc.gov/zing/srw/"})
  return unless record.try(:content)
  doc = Nokogiri::XML::Document.new
  doc << record.at("//mods:mods", { "mods" => "http://www.loc.gov/mods/v3" })
  Manifestation.import_record_from_loc(doc)
end

.make_sru_request_uri(query, options = {}) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/models/loc_search.rb', line 61

def self.make_sru_request_uri(query, options = {})
  if options[ :page ]
    page = options[ :page ].to_i
    options[ :startRecord ] = (page - 1) * 10 + 1
    options.delete :page
  end
  options = { maximumRecords: 10, recordSchema: :mods }.merge(options)
  options = options.merge({ query: query, version: "1.1", operation: "searchRetrieve" })
  params = options.map do |k, v|
    "#{ URI.escape(k.to_s) }=#{ URI.escape(v.to_s) }"
    end.join('&')
  uri = "#{ LOC_SRU_BASEURL }?#{ params }"
end

.search(query, options = {}) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
# File 'app/models/loc_search.rb', line 75

def self.search(query, options = {})
  if query and !query.empty?
    url = make_sru_request_uri(query, options)
    doc = Nokogiri::XML(Faraday.get(url).body)
    items = doc.search('//zs:record').map{|e| ModsRecord.new e }
    @results = { items: items,
                 total_entries: doc.xpath('//zs:numberOfRecords').first.try(:content).to_i }
  else
    { items: [], total_entries: 0 }
  end
end