class Biblionet::Extractors::AuthorExtractor

Attributes

author[R]

Public Class Methods

new(uri=nil) click to toggle source
Calls superclass method
# File lib/bookshark/extractors/author_extractor.rb, line 12
def initialize(uri=nil)
  super(uri)        
  extract_author unless uri.nil? or @page.nil?        
end

Public Instance Methods

extract_author(biblionet_id=@biblionet_id, author_page=@page) click to toggle source

def to_json_pretty

JSON.pretty_generate(@author) unless @author.nil?

end

# File lib/bookshark/extractors/author_extractor.rb, line 27
def extract_author(biblionet_id=@biblionet_id, author_page=@page)
  puts "Extracting author: #{biblionet_id}"
  page = AuthorDataExtractor.new(author_page)
  
  identity = split_name(page.fullname)

  author_hash = {}
  if present?(identity[:lastname]) and present?(identity[:firstname]) 
    author_hash[:name] = identity[:lastname] + ', ' + identity[:firstname] 
  elsif 
    author_hash[:name] = identity[:lastname]
  end      
  author_hash[:firstname] = identity[:firstname]
  author_hash[:lastname] = identity[:lastname]
  author_hash[:extra_info] = identity[:extra_info]
  author_hash[:image] = page.image
  author_hash[:bio] = page.bio
  author_hash[:award] = page.awards
  author_hash[:b_id] = biblionet_id

  # puts JSON.pretty_generate(author_hash)

  if author_hash[:lastname].nil? and author_hash[:firstname].nil?
    return nil
  else
    return @author = author_hash
  end

end
load_and_extract_author(uri=nil) click to toggle source
# File lib/bookshark/extractors/author_extractor.rb, line 18
def load_and_extract_author(uri=nil)
  load_page(uri)
  extract_author unless uri.nil? or @page.nil?
end
split_name(fullname) click to toggle source
# File lib/bookshark/extractors/author_extractor.rb, line 57
def split_name(fullname)
  #mathes digits-digits or digits- in text like: Tolkien, John Ronald Reuel, 1892-1973
  years_re = /\d+-\d*/

  parts = fullname.split(',').map(&:strip)  

  identity = {}
  identity[:lastname] = parts[0]
  
  if parts.length == 2
    if parts[1] =~ years_re
      identity[:extra_info] = parts[1]
    else
      identity[:firstname] = parts[1]
    end
  elsif parts.length == 3
    identity[:firstname] = parts[1]
    identity[:extra_info] = parts[2]
  end

  return identity

end