Module: Hondana

Defined in:
lib/hondana.rb

Defined Under Namespace

Classes: Book, Entry, Shelf

Constant Summary collapse

VERSION =
'0.1.1'

Class Method Summary collapse

Class Method Details

.books(pattern = nil) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
# File 'lib/hondana.rb', line 128

def Hondana.books(pattern=nil)
  isbns = 
    if pattern
      JSON.parse(Hondana.http_get("/books?pattern=#{pattern}"))
    else
      JSON.parse(Hondana.http_get("/books"))
    end
  isbns.collect { |isbn|
    Book.new(isbn)
  }
end

.http_get(addr) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/hondana.rb', line 15

def Hondana.http_get(addr)
  ret = nil
  begin
    Net::HTTP.start('api.hondana.org', 80) {|http|
      req = Net::HTTP::Get.new(addr)
      response = http.request(req)
      raise "status code error (#{response.code})" if response.code != "200"
      ret = response.body
    }
  rescue
  end
  raise "http_get failed" if ret == nil
  ret.chomp
end

.shelves(pattern = nil) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
# File 'lib/hondana.rb', line 140

def Hondana.shelves(pattern=nil)
  shelves = 
    if pattern
      JSON.parse(Hondana.http_get("/shelves?pattern=#{pattern}"))
    else
      JSON.parse(Hondana.http_get("/shelves"))
    end
  shelves.collect { |name|
    Shelf.new(name)
  }
end