Module: OpenLibrary

Defined in:
lib/fetchworks.rb

Overview

Provides methods for accessing OpenLibrary resources

Defined Under Namespace

Classes: InvalidISBN, OLBadStatus, OLDateStrUnparseable, OLResourceNotFound, OpenLibraryError

Class Method Summary collapse

Class Method Details

.get_author(url) ⇒ Object



67
68
69
70
71
# File 'lib/fetchworks.rb', line 67

def self.get_author(url)
  url = url[%r{\A.*(?=/)}] + ".json"
  uri = URI.parse(url)
  JSON.parse(uri.open.read)
end

.get_book(isbn) ⇒ Object

Raises:



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/fetchworks.rb', line 46

def self.get_book(isbn)
  raise InvalidISBN unless isbn.is_a?(Integer) || isbn.is_a?(String)

  isbn = isbn.to_s if isbn.is_a? Integer
  isbn = isbn.strip

  # Primitive ISBN check: regex match for a 10 or 12 digit number
  # TODO: check prefix and checksum
  raise InvalidISBN unless /\A\d{10}(\d{3})?\z/.match? isbn

  query = "bibkeys=ISBN:#{isbn}#{QUERY_POSTFIX}"
  uri = URI::HTTPS.build(host: HOST, path: PATH, query: query)
  response = uri.open(**OPENURI_OPTS)

  if response&.status != ["200", "OK"]
    raise OLBadStatus, response&.status
  end

  JSON.parse(response.read).values.first
end

.partial_date_from_str(str) ⇒ Object

Returns a PartialDate from strings of one of the following four formats: “YYYY” | “Month YYYY” | “Month DD, YYYY” | “DD Month, YYYY”

“Month”s are abbreviated usually, but sometimes not.

This gets nasty, because we have a number of formats when two or three elements are present. We decide which one based off of which member is a recognized month name.



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
# File 'lib/fetchworks.rb', line 81

def self.partial_date_from_str(str)
  return nil unless str.is_a?(String) && !str.empty?

  # Replace commas with spaces and part out the date string
  split = str.tr(",", " ").split

  PartialDate.new(
    case split.length
    # e.g. "1991"
    when 1
      [split[0].to_i]

    when 2
      # e.g. "Jan 1991"
      if Date::ABBR_MONTHNAMES.include?(split[0])
        [split[1].to_i,
         Date::ABBR_MONTHNAMES.index(split[0])]

      # e.g. "January 1991"
      elsif Date::MONTHNAMES.include?(split[0])
        [split[1].to_i,
         Date::MONTHNAMES.index(split[0])]
      else
        raise OLDateStrUnparseable
      end

    when 3
      # e.g. "Jan 12, 1991"
      if Date::ABBR_MONTHNAMES.include?(split[0])
        [split[2].to_i,
         Date::ABBR_MONTHNAMES.index(split[0]),
         split[1].to_i]

      # e.g. "January 12, 1991"
      elsif Date::MONTHNAMES.include?(split[0])
        [split[2].to_i,
         Date::MONTHNAMES.index(split[0]),
         split[1].to_i]

      # e.g. "12 Jan 1991"
      elsif Date::ABBR_MONTHNAMES.include?(split[1])
        [split[2].to_i,
         Date::ABBR_MONTHNAMES.index(split[1]),
         split[0].to_i]

      # e.g. "12 January 1991"
      elsif Date::MONTHNAMES.include?(split[1])
        [split[2].to_i,
         Date::MONTHNAMES.index(split[1]),
         split[0].to_i]
      else
        raise OLDateStrUnparseable
      end
    end
  )
end