Class: DBLPQuery
- Inherits:
-
Object
- Object
- DBLPQuery
- Defined in:
- lib/dblp/query.rb
Constant Summary collapse
- BASE_URI =
"http://dblp.mpi-inf.mpg.de/autocomplete-php/autocomplete/ajax.php"
Instance Attribute Summary collapse
-
#params ⇒ Object
Returns the value of attribute params.
-
#result ⇒ Object
Returns the value of attribute result.
Instance Method Summary collapse
- #cite(num) ⇒ Object
- #extreact_from_body(body) ⇒ Object
-
#initialize ⇒ DBLPQuery
constructor
A new instance of DBLPQuery.
- #parse_table(body) ⇒ Object
- #present ⇒ Object
- #query(what) ⇒ Object
-
#select(num) ⇒ Object
Based on the last result that was fetched, a new cite key is returned based on the position inside the result.
Constructor Details
#initialize ⇒ DBLPQuery
Returns a new instance of DBLPQuery.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/dblp/query.rb', line 12 def initialize @result = [] @params = { :name => "dblpmirror", :path => "/dblp-mirror/", :page => "index.php", :log => "/var/opt/completesearch/log/completesearch.error_log", :qid => 6, :navigation_mode => "history", :language => "en", :mcsr => 40, :mcc => 0, :mcl => 80, :hpp => 20, :eph => 1, :er => 20, :dm => 3, :bnm => "R", :ll => 2, :mo => 100, :accc => ":", :syn => 0, :deb => 0, :hrd => "1a", :hrw => "1d", :qi => 1, :fh => 1, :fhs => 1, :mcs => 20, :rid => 0, :qt => "H" } end |
Instance Attribute Details
#params ⇒ Object
Returns the value of attribute params.
10 11 12 |
# File 'lib/dblp/query.rb', line 10 def params @params end |
#result ⇒ Object
Returns the value of attribute result.
10 11 12 |
# File 'lib/dblp/query.rb', line 10 def result @result end |
Instance Method Details
#cite(num) ⇒ Object
117 118 119 |
# File 'lib/dblp/query.rb', line 117 def cite(num) "\\cite{#{select(num)}}" end |
#extreact_from_body(body) ⇒ Object
76 77 78 79 80 |
# File 'lib/dblp/query.rb', line 76 def extreact_from_body(body) raw = body.split("\n")[27][11..-2].gsub("'", "\"") full = JSON.parse(raw) full["body"] end |
#parse_table(body) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/dblp/query.rb', line 82 def parse_table(body) result = [] body.scan(/<tr>(.*?)<\/tr>/m) do |match| cells = match[0].scan(/<td.*?>(.*?)<\/td>/m) next unless cells.size == 3 obj = OpenStruct.new # First Cell is the cite key obj.cite = "DBLP:" << cells[0][0].match(/href="http:\/\/dblp\.uni-trier\.de\/rec\/bibtex\/(.*?)">/)[1] # second cell the link to the electronic version # Third cell is author + title obj.title = cells[2][0].gsub(/<.*?>/,"") result << obj end result end |
#present ⇒ Object
111 112 113 114 115 |
# File 'lib/dblp/query.rb', line 111 def present @result.each_with_index do |item, i| puts "\t#{i+1}\t#{item.title}\n" end end |
#query(what) ⇒ Object
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 |
# File 'lib/dblp/query.rb', line 49 def query(what) # Make some simple means of caching return if @params["query"] == what @params["query"] = what url = URI.parse(BASE_URI) req = Net::HTTP::Post.new(url.path) req.set_form_data(@params) res = Net::HTTP.new(url.host, url.port).start {|http| http.request(req) } case res when Net::HTTPSuccess, Net::HTTPRedirection # OK puts "Success" body = extreact_from_body(res.body) parse_table(body) @result = parse_table(body) else res.error! end end |
#select(num) ⇒ Object
Based on the last result that was fetched, a new cite key is returned based on the position inside the result.
106 107 108 109 |
# File 'lib/dblp/query.rb', line 106 def select(num) return if @result.size == 0 || @result.size < num return @result[num.abs].cite end |