class ElsevierApi::Connection

Attributes

error[R]
error_msg[R]
key[R]
proxy_host[RW]
proxy_pass[RW]
proxy_post[RW]
proxy_user[RW]
raw_xml[R]
use_proxy[RW]
xml_response[R]

Public Class Methods

new(key, opts={}) click to toggle source
# File lib/elsevier_api/connection.rb, line 38
def initialize(key, opts={})
  @key=key
  @use_proxy=false
  @error=false
  @error_msg=nil
  if opts[:proxy_host]
    @use_proxy=true
    @proxy_host=opts[:proxy_host]
    @proxy_port=opts[:proxy_port]
    @proxy_user=opts[:proxy_user]
    @proxy_pass=opts[:proxy_pass]
  end
end

Public Instance Methods

close() click to toggle source
# File lib/elsevier_api/connection.rb, line 54
def close
  @connection.close if @connection
end
connection() click to toggle source
# File lib/elsevier_api/connection.rb, line 51
def connection
  @connection||=get_connection
end
get_articles_from_uri(uri) click to toggle source
# File lib/elsevier_api/connection.rb, line 100
  def get_articles_from_uri(uri)
    completo=false
    acumulado=[]
    pagina=1
    until completo do
      #puts "Page:#{pagina}"
      xml_response=retrieve_response(uri)
      if @error
        break
      else
        acumulado=acumulado+xml_response.entries_to_hash
        next_page=xml_response.next_page
        if next_page
          pagina+=1
          uri=next_page.attribute("href").value
        else
#          puts "completo"
          completo=true
        end
      end  
    end
    acumulado
  end
get_journal_articles(journal,year=nil) click to toggle source
# File lib/elsevier_api/connection.rb, line 124
def get_journal_articles(journal,year=nil)
  uri=get_uri_journal_articles(journal,year)
  completo=false
  acumulado=[]
  until completo do
    xml_response=retrieve_response(uri)
    if @error
      break
    else
      acumulado=acumulado+xml_response.entries_to_ary
      next_page=xml_response.next_page
      if next_page
        uri=next_page.attribute("href").value
        #puts "siguiente pagina"
      else
        #puts "completo"
        completo=true
      end
    end  
  end
  acumulado
  end
retrieve_response(uri_string) click to toggle source

Connect to api and retrieve a response based on URI

# File lib/elsevier_api/connection.rb, line 59
def retrieve_response(uri_string)
  @xml_response=nil
  begin
    Curl::Easy.new(uri_string) do |curl|
      if @use_proxy
        curl.proxy_tunnel = true
        curl.proxy_url = "http://#{@proxy_host}:#{@proxy_port}/"
        curl.proxypwd = "#{@proxy_user}:#{proxy_pass}"
      end
      curl.follow_location = true
      curl.ssl_verify_peer = false
      curl.max_redirects = 3
      curl.headers=['Accept: application/xml', "X-ELS-APIKey: #{@key}"]
      curl.perform
      #p curl.body_str
      xml=Nokogiri::XML(curl.body_str)
      if xml.xpath("//service-error").length>0
        @error=true
        @error_msg=xml.xpath("//statusText").text
      elsif xml.xpath("//atom:error",'atom'=>'http://www.w3.org/2005/Atom').length>0
        @error=true
        @error_msg=xml.xpath("//atom:error").text
      elsif xml.children.length==0
        @error=true
        @error_msg="Empty_XML"
      else
        @error=false
        @error_msg=nil
      end
      @xml_response=ElsevierApi.process_xml(xml)
    end
  rescue Exception=>e
    #$log.info(e.message)
    @error=true
    @error_msg=e.message
  end

  @xml_response
  
end