Class: ElsevierApi::Connection
- Inherits:
-
Object
- Object
- ElsevierApi::Connection
- Extended by:
- URIRequest
- Includes:
- URIRequest
- Defined in:
- lib/elsevier_api/connection.rb
Instance Attribute Summary collapse
-
#error ⇒ Object
readonly
Returns the value of attribute error.
-
#error_msg ⇒ Object
readonly
Returns the value of attribute error_msg.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#proxy_host ⇒ Object
Returns the value of attribute proxy_host.
-
#proxy_pass ⇒ Object
Returns the value of attribute proxy_pass.
-
#proxy_post ⇒ Object
Returns the value of attribute proxy_post.
-
#proxy_user ⇒ Object
Returns the value of attribute proxy_user.
-
#raw_xml ⇒ Object
readonly
Returns the value of attribute raw_xml.
-
#use_proxy ⇒ Object
Returns the value of attribute use_proxy.
-
#xml_response ⇒ Object
readonly
Returns the value of attribute xml_response.
Instance Method Summary collapse
- #close ⇒ Object
- #connection ⇒ Object
- #get_articles_from_uri(uri) ⇒ Object
- #get_journal_articles(journal, year = nil) ⇒ Object
-
#initialize(key, opts = {}) ⇒ Connection
constructor
A new instance of Connection.
-
#retrieve_response(uri_string) ⇒ Object
Connect to api and retrieve a response based on URI.
Methods included from URIRequest
get_uri_abstract, get_uri_articles_country_year_area, get_uri_author, get_uri_citation_overview, get_uri_journal_articles
Constructor Details
#initialize(key, opts = {}) ⇒ Connection
Returns a new instance of Connection.
38 39 40 41 42 43 44 45 46 47 48 49 50 |
# 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 |
Instance Attribute Details
#error ⇒ Object (readonly)
Returns the value of attribute error.
35 36 37 |
# File 'lib/elsevier_api/connection.rb', line 35 def error @error end |
#error_msg ⇒ Object (readonly)
Returns the value of attribute error_msg.
35 36 37 |
# File 'lib/elsevier_api/connection.rb', line 35 def error_msg @error_msg end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
35 36 37 |
# File 'lib/elsevier_api/connection.rb', line 35 def key @key end |
#proxy_host ⇒ Object
Returns the value of attribute proxy_host.
37 38 39 |
# File 'lib/elsevier_api/connection.rb', line 37 def proxy_host @proxy_host end |
#proxy_pass ⇒ Object
Returns the value of attribute proxy_pass.
37 38 39 |
# File 'lib/elsevier_api/connection.rb', line 37 def proxy_pass @proxy_pass end |
#proxy_post ⇒ Object
Returns the value of attribute proxy_post.
37 38 39 |
# File 'lib/elsevier_api/connection.rb', line 37 def proxy_post @proxy_post end |
#proxy_user ⇒ Object
Returns the value of attribute proxy_user.
37 38 39 |
# File 'lib/elsevier_api/connection.rb', line 37 def proxy_user @proxy_user end |
#raw_xml ⇒ Object (readonly)
Returns the value of attribute raw_xml.
35 36 37 |
# File 'lib/elsevier_api/connection.rb', line 35 def raw_xml @raw_xml end |
#use_proxy ⇒ Object
Returns the value of attribute use_proxy.
36 37 38 |
# File 'lib/elsevier_api/connection.rb', line 36 def use_proxy @use_proxy end |
#xml_response ⇒ Object (readonly)
Returns the value of attribute xml_response.
35 36 37 |
# File 'lib/elsevier_api/connection.rb', line 35 def xml_response @xml_response end |
Instance Method Details
#close ⇒ Object
54 55 56 |
# File 'lib/elsevier_api/connection.rb', line 54 def close @connection.close if @connection end |
#connection ⇒ Object
51 52 53 |
# File 'lib/elsevier_api/connection.rb', line 51 def connection @connection||=get_connection end |
#get_articles_from_uri(uri) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# 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) ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# 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) ⇒ Object
Connect to api and retrieve a response based on URI
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# 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. end @xml_response end |