Module: BibSync::Utils
- Included in:
- Actions::CheckArXivVersions, Actions::DetermineArXivDOI, Actions::FetchFromArXiv, Actions::FindMyCitations, Actions::JabRefFormat, Actions::SynchronizeFiles, Actions::SynchronizeMetadata, Actions::Validate, Transformer
- Defined in:
- lib/bibsync/utils.rb
Instance Method Summary collapse
- #arxiv_download(dir, id) ⇒ Object
- #arxiv_id(arxiv, opts = {}) ⇒ Object
- #fetch(url, headers = {}) ⇒ Object
- #fetch_html(url, headers = {}) ⇒ Object
- #fetch_xml(url, headers = {}) ⇒ Object
- #name_without_ext(file) ⇒ Object
Instance Method Details
#arxiv_download(dir, id) ⇒ Object
16 17 18 19 20 21 |
# File 'lib/bibsync/utils.rb', line 16 def arxiv_download(dir, id) url = "http://arxiv.org/pdf/#{id}" file = File.join(dir, "#{arxiv_id(id, version: true, prefix: false)}.pdf") result = `curl --stderr - -S -s -L -o #{Shellwords.escape file} #{Shellwords.escape url}` raise result.chomp if $? != 0 end |
#arxiv_id(arxiv, opts = {}) ⇒ Object
33 34 35 36 37 38 39 40 41 |
# File 'lib/bibsync/utils.rb', line 33 def arxiv_id(arxiv, opts = {}) raise unless opts.include?(:prefix) && opts.include?(:version) arxiv = arxiv[:arxiv] if Entry === arxiv if arxiv arxiv = arxiv.sub(/\A.*\//, '') unless opts[:prefix] arxiv = arxiv.sub(/v\d+\Z/, '') unless opts[:version] end arxiv end |
#fetch(url, headers = {}) ⇒ Object
8 9 10 11 12 13 14 |
# File 'lib/bibsync/utils.rb', line 8 def fetch(url, headers = {}) # open(url, headers) {|f| f.read } headers = headers.map {|k,v| '-H ' + Shellwords.escape("#{k}: #{v}") }.join(' ') result = `curl --stderr - -S -s -L #{headers} #{Shellwords.escape url}` raise result.chomp if $? != 0 result end |
#fetch_html(url, headers = {}) ⇒ Object
29 30 31 |
# File 'lib/bibsync/utils.rb', line 29 def fetch_html(url, headers = {}) Nokogiri::HTML(fetch(url, headers)) end |
#fetch_xml(url, headers = {}) ⇒ Object
23 24 25 26 27 |
# File 'lib/bibsync/utils.rb', line 23 def fetch_xml(url, headers = {}) xml = Nokogiri::XML(fetch(url, headers)) xml.remove_namespaces! xml end |
#name_without_ext(file) ⇒ Object
3 4 5 6 |
# File 'lib/bibsync/utils.rb', line 3 def name_without_ext(file) file =~ /\A(.*?)\.\w+\Z/ $1 end |