Skip to content

Commit 4f8537e

Browse files
committedFeb 2, 2013
Initial commit
0 parents  commit 4f8537e

File tree

8 files changed

+181
-0
lines changed

8 files changed

+181
-0
lines changed
 

‎.gitignore

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
*.gem
2+
*.rbc
3+
.bundle
4+
.config
5+
.yardoc
6+
Gemfile.lock
7+
InstalledFiles
8+
_yardoc
9+
coverage
10+
doc/
11+
lib/bundler/man
12+
pkg
13+
rdoc
14+
spec/reports
15+
test/tmp
16+
test/version_tmp
17+
tmp
18+
*.sublime-project
19+
*.sublime-workspace
20+

‎Gemfile

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
source 'https://rubygems.org'
2+
3+
# Specify your gem's dependencies in paper_metadata.gemspec
4+
gemspec

‎LICENSE.txt

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Copyright (c) 2013 Jure Triglav
2+
3+
MIT License
4+
5+
Permission is hereby granted, free of charge, to any person obtaining
6+
a copy of this software and associated documentation files (the
7+
"Software"), to deal in the Software without restriction, including
8+
without limitation the rights to use, copy, modify, merge, publish,
9+
distribute, sublicense, and/or sell copies of the Software, and to
10+
permit persons to whom the Software is furnished to do so, subject to
11+
the following conditions:
12+
13+
The above copyright notice and this permission notice shall be
14+
included in all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

‎README.md

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# PaperMetadata
2+
3+
TODO: Write a gem description
4+
5+
## Installation
6+
7+
Add this line to your application's Gemfile:
8+
9+
gem 'paper_metadata'
10+
11+
And then execute:
12+
13+
$ bundle
14+
15+
Or install it yourself as:
16+
17+
$ gem install paper_metadata
18+
19+
## Usage
20+
21+
TODO: Write usage instructions here
22+
23+
## Contributing
24+
25+
1. Fork it
26+
2. Create your feature branch (`git checkout -b my-new-feature`)
27+
3. Commit your changes (`git commit -am 'Add some feature'`)
28+
4. Push to the branch (`git push origin my-new-feature`)
29+
5. Create new Pull Request

‎Rakefile

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
require "bundler/gem_tasks"
2+
3+
require 'rake/testtask'
4+
5+
Rake::TestTask.new do |t|
6+
t.libs << 'test'
7+
end
8+
9+
desc "Run tests"
10+
task :default => :test

‎lib/paper_metadata.rb

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
require 'paper_metadata/version'
2+
require 'nokogiri'
3+
require 'net/http'
4+
require 'open-uri'
5+
require 'pry'
6+
7+
module PaperMetadata
8+
class << self
9+
attr_accessor :doi_username
10+
11+
def doi_username
12+
raise "PaperMetadata needs the DOI username to work with CrossRef API" unless @doi_username
13+
@doi_username
14+
end
15+
16+
def metadata_for(identifier)
17+
if identifier =~ /^arxiv\:/i
18+
metadata_for_arxiv(identifier)
19+
elsif identifier =~ /^doi\:/i
20+
metadata_for_doi(identifier)
21+
end
22+
end
23+
24+
def metadata_for_doi(doi)
25+
doc = Nokogiri::XML(open("http://www.crossref.org/openurl/?id=doi:#{doi}&noredirect=true&pid=#{PaperMetadata.doi_username}&format=unixref"))
26+
paper = Hash.new
27+
28+
if doc.xpath("//titles/title").first
29+
paper[:volume] = doc.xpath("//journal_issue/journal_volume/volume").inner_html.to_s
30+
paper[:isssue] = doc.xpath("//journal_issue/issue").inner_html.to_s
31+
paper[:first_page] = doc.xpath("//pages/first_page").inner_html.to_s
32+
paper[:last_page] = doc.xpath("//pages/last_page").inner_html.to_s
33+
paper[:title] = doc.xpath("//titles/title").inner_html.to_s
34+
paper[:authors] = doc.xpath("//contributors/person_name").
35+
map{ |author| author.content.strip}
36+
37+
paper[:authors] = paper[:authors].map do |author|
38+
author.gsub(/\s+/, ' ')
39+
end.join(', ')
40+
41+
paper[:journal] = doc.xpath("//abbrev_title").inner_html + " " +
42+
doc.xpath("//journal_issue/publication_date/year").first.inner_html
43+
paper[:resource] = doc.xpath("//journal_article/doi_data/resource").inner_html
44+
else
45+
paper[:status] = :NODOI
46+
end
47+
paper
48+
end
49+
50+
def metadata_for_arxiv(identifier)
51+
identifier.gsub!(/^arXiv\:/i, '')
52+
url = URI.parse("http://export.arxiv.org/api/query?search_query=#{identifier}&start=0&max_results=1")
53+
res = Net::HTTP.get_response(url)
54+
doc = Nokogiri::XML(res.body)
55+
doc.remove_namespaces!
56+
paper = Hash.new
57+
if entry = doc.xpath("//entry").first
58+
paper[:title] = entry.xpath('title').text
59+
paper[:author] = entry.xpath('author').text.strip
60+
paper[:id] = entry.xpath('id').text
61+
paper[:updated] = entry.xpath('updated').text
62+
paper[:summary] = entry.xpath('summary').text
63+
paper[:published] = entry.xpath('published').text
64+
end
65+
paper
66+
end
67+
end
68+
end

‎lib/paper_metadata/version.rb

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module PaperMetadata
2+
VERSION = "0.0.1"
3+
end

‎paper_metadata.gemspec

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# -*- encoding: utf-8 -*-
2+
lib = File.expand_path('../lib', __FILE__)
3+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4+
require 'paper_metadata/version'
5+
6+
Gem::Specification.new do |gem|
7+
gem.name = "paper_metadata"
8+
gem.version = PaperMetadata::VERSION
9+
gem.authors = ["Jure Triglav"]
10+
gem.email = ["juretriglav@gmail.com"]
11+
gem.summary = %q{Paper_metadata gem gets the metadata for papers with a DOI or an arXiv identifier}
12+
gem.description = %q{Metadata getter for scientific papers}
13+
gem.homepage = "http://github.com/jure/paper_metadata"
14+
15+
gem.files = `git ls-files`.split($/)
16+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18+
gem.require_paths = ["lib"]
19+
20+
gem.add_development_dependency "pry"
21+
gem.add_development_dependency "rake"
22+
gem.add_development_dependency "webmock"
23+
gem.add_runtime_dependency "nokogiri", ["= 1.5.6"]
24+
25+
end

0 commit comments

Comments
 (0)
Please sign in to comment.