Class: CrossrefQuery

Inherits:
Sequel::Model
  • Object
show all
Defined in:
model/crossref.rb

Overview

Class that query Crossref for a given text and stores the result

Class Method Summary collapse

Class Method Details

.generate_query_from_text(t) ⇒ Object

Takes a text and returns a processed JSON

Parameters:

  • t (String)

    a text that represents a document

Returns:

  • a processed json

Raises:



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'model/crossref.rb', line 94

def self.generate_query_from_text(t)
  require 'digest'
  digest=Digest::SHA256.hexdigest t
  cq=CrossrefQuery[digest]

  if !cq
    url="https://search.crossref.org/dois?q=#{CGI.escape(t)}"
    uri = URI(url)
    res = Net::HTTP.get_response(uri)
    #$log.info(res)
    if res.code!="200"
      raise BadCrossrefResponseError, "El text #{t} no entrego una respuesta adecuada. Fue #{res.code}, #{res.body}"
    end
    json_raw = res.body
    CrossrefQuery.insert(:id=>digest.force_encoding(Encoding::UTF_8),:query=>t.force_encoding(Encoding::UTF_8),:json=>json_raw.force_encoding(Encoding::UTF_8))
  else
    json_raw=cq[:json]
  end
  JSON.parse(json_raw)
end