Class: Hondana

Inherits:
Object
  • Object
show all
Defined in:
lib/hondana.rb

Defined Under Namespace

Classes: Book, Shelf

Constant Summary collapse

VERSION =
'0.0.1'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHondana

Returns a new instance of Hondana.



55
56
# File 'lib/hondana.rb', line 55

def initialize
end

Class Method Details

.http_get(addr) ⇒ Object



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

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

Instance Method Details

#books(shelf = '', pattern = '') ⇒ Object



70
71
72
73
74
# File 'lib/hondana.rb', line 70

def books(shelf='',pattern='')
  isbns(shelf,pattern).collect { |isbn|
    Book.new(isbn)
  }
end

#isbns(shelf = '', pattern = '') ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/hondana.rb', line 58

def isbns(shelf='',pattern='')
  if shelf == '' && pattern == ''
    JSON.parse(Hondana.http_get("/books"))
  elsif pattern == ''
    JSON.parse(Hondana.http_get("/books?shelf=#{shelf}"))
  elsif shelf == ''
    JSON.parse(Hondana.http_get("/books?pattern=#{pattern}"))
  else
    JSON.parse(Hondana.http_get("/books?pattern=#{shelf},#{pattern}"))
  end
end

#shelfnames(isbn = '', pattern = '') ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/hondana.rb', line 76

def shelfnames(isbn='',pattern='')
  if isbn == '' && pattern == ''
    JSON.parse(Hondana.http_get("/shelves"))
  elsif pattern == ''
    JSON.parse(Hondana.http_get("/shelves?isbn=#{isbn}"))
  elsif isbn == ''
    JSON.parse(Hondana.http_get("/shelves?pattern=#{pattern}"))
  else
    JSON.parse(Hondana.http_get("/shelves?pattern=#{isbn},#{pattern}"))
  end
end

#shelves(isbn = '', pattern = '') ⇒ Object



88
89
90
91
92
# File 'lib/hondana.rb', line 88

def shelves(isbn='',pattern='')
  shelfnames(isbn,pattern).collect { |name|
    Shelf.new(name)
  }
end