Module: Kindai::Util::Database
- Defined in:
- lib/kindai/util/database.rb
Constant Summary collapse
- ENDPOINT =
URI.parse 'http://gigaschema.appspot.com/hitode909/kindai.json'
Class Method Summary collapse
- .item_for_book(book) ⇒ Object
-
.items ⇒ Object
XXX: deprecated, page 1 only.
- .save_item(book, info) ⇒ Object
- .validate(info) ⇒ Object
Class Method Details
.item_for_book(book) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/kindai/util/database.rb', line 27 def self.item_for_book(book) path = ENDPOINT + "?group=#{book.key}" JSON.parse(Kindai::Util.fetch_uri(path))['data'].map{|item| begin hash = JSON.parse(item['value']) self.validate(hash) hash.each_pair.inject({ }){ |obj, pair| obj[pair.first.to_sym] = pair.last obj } rescue => error Kindai::Util.logger.warn error nil end }.compact.first end |
.items ⇒ Object
XXX: deprecated, page 1 only
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/kindai/util/database.rb', line 10 def self.items @items ||= JSON.parse(Kindai::Util.fetch_uri(ENDPOINT))['data'].map{|item| begin hash = JSON.parse(item['value']) self.validate(hash) hash.each_pair.inject({ }){ |obj, pair| obj[pair.first.to_sym] = pair.last obj } rescue => error Kindai::Util.logger.warn error nil end }.compact end |
.save_item(book, info) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/kindai/util/database.rb', line 44 def self.save_item(book, info) previous_item = self.item_for_book(book) if previous_item and previous_item[:version] >= Kindai::VERSION Kindai::Util.logger.warn "Database has newer version of #{book.title}. To save, delete it first." return false end send_data = { 'uri' => book.permalink_uri, 'title' => book.title, 'author' => book., 'x' => info[:x], 'y' => info[:y], 'width' => info[:width], 'height' => info[:height], 'version' => Kindai::VERSION } self.validate(send_data) proxy_uri = URI.parse(ENV["http_proxy"] || ENV["HTTP_PROXY"] || "") proxy_user, proxy_pass = proxy.userinfo.split(/:/) if proxy_uri.userinfo res = Net::HTTP.Proxy(proxy_uri.host, proxy_uri.port, proxy_user, proxy_pass).start(ENDPOINT.host, ENDPOINT.port){|http| request = Net::HTTP::Post.new(ENDPOINT.path) request.set_form_data({:value => send_data.to_json, :group => book.key}) http.request(request) } case res.code when '200' JSON.parse(res.body) else raise res end end |
.validate(info) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/kindai/util/database.rb', line 79 def self.validate(info) %w{uri title author}.each{|key| raise "key #{key} is required" unless info.has_key? key raise "key #{key} must be kind of String" unless info[key].kind_of? String } %w{x y width height version}.each{|key| raise "key #{key} is required" unless info.has_key? key raise "key #{key} must be kind of Numeric" unless info[key].kind_of? Numeric } end |