ref2bibtex 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d1a0dce05782be34b45170a878fa53b2e424b650
4
+ data.tar.gz: 0f9e559f4eac387d0c753d2d54b373fe6075973d
5
+ SHA512:
6
+ metadata.gz: 8277b854333a500918e8078129f1fb98006f3eef9c34bbc1ac2d0966c45859fce5f8bd7e5b089dd059effdb142a1afb7521034dfbe82e74a073048bbc03d5ead
7
+ data.tar.gz: 0fea146404963493dad41379687bc65ab0ed3d78d290ea7838d9eb598ef781a5d48a71ad662b8414490087de38cfef0d28fedf5de1fd6036fdfe3974ded2b221
data/.gitignore ADDED
@@ -0,0 +1,39 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /test/tmp/
9
+ /test/version_tmp/
10
+ /tmp/
11
+
12
+ ## Specific to RubyMotion:
13
+ .dat*
14
+ .repl_history
15
+ build/
16
+
17
+ ## Documentation cache and generated files:
18
+ /.yardoc/
19
+ /_yardoc/
20
+ /doc/
21
+ /rdoc/
22
+
23
+ ## Environment normalisation:
24
+ /.bundle/
25
+ /lib/bundler/man/
26
+
27
+ # for a library or gem, you might want to ignore these files since the code is
28
+ # intended to run in multiple environments; otherwise, check them in:
29
+ # Gemfile.lock
30
+ # .ruby-version
31
+ # .ruby-gemset
32
+
33
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
34
+ .rvmrc
35
+
36
+ *~
37
+ *.swp
38
+ idea/
39
+ .DS_Store
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ref3bibtex.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,27 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ ref2bibtex (0.0.1)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ awesome_print (1.2.0)
10
+ byebug (3.4.0)
11
+ columnize (~> 0.8)
12
+ debugger-linecache (~> 1.2)
13
+ slop (~> 3.6)
14
+ columnize (0.8.9)
15
+ debugger-linecache (1.2.0)
16
+ rake (10.3.2)
17
+ slop (3.6.0)
18
+
19
+ PLATFORMS
20
+ ruby
21
+
22
+ DEPENDENCIES
23
+ awesome_print
24
+ bundler (~> 1.6)
25
+ byebug
26
+ rake
27
+ ref2bibtex!
data/LICENSE ADDED
@@ -0,0 +1,28 @@
1
+ Copyright (c) 2014, Species File Group
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without
5
+ modification, are permitted provided that the following conditions are met:
6
+
7
+ * Redistributions of source code must retain the above copyright notice, this
8
+ list of conditions and the following disclaimer.
9
+
10
+ * Redistributions in binary form must reproduce the above copyright notice,
11
+ this list of conditions and the following disclaimer in the documentation
12
+ and/or other materials provided with the distribution.
13
+
14
+ * Neither the name of ref2bibtex nor the names of its
15
+ contributors may be used to endorse or promote products derived from
16
+ this software without specific prior written permission.
17
+
18
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
+
data/README.md ADDED
@@ -0,0 +1,53 @@
1
+ ref2bibtex
2
+ ==========
3
+
4
+ A single purpose gem wrapping Crossref's API. Pass it a full reference string, get back bibtex.
5
+
6
+ usage
7
+ =====
8
+
9
+ Use citation2bibtex (aliased _get_):
10
+
11
+ ```ruby
12
+
13
+ gem install ref2bibtex
14
+
15
+ require 'ref2bibtex'
16
+
17
+ Ref2bibtex.get('Yoder, M. J., A. A. Valerio, A. Polaszek, L. Masner, and N. F. Johnson. 2009. Revision of Scelio pulchripennis - group species (Hymenoptera, Platygastroidea, Platygastridae). ZooKeys 20:53-118.') # => "@article{Yoder_2009,\n\tdoi = {10.3897/zookeys.20.205},\n\turl = {http://dx.doi.org/10.3897/zookeys.20.205},\n\tyear = 2009,\n\tmonth = {sep},\n\tpublisher = {Pensoft Publishers},\n\tvolume = {20},\n\tnumber = {0},\n\tauthor = {Matthew Yoder and Andrew Polaszek and Lubomir Masner and Norman Johnson and Alejandro Valerio},\n\ttitle = {Revision of Scelio pulchripennis - group species (Hymenoptera, Platygastroidea, Platygastridae)},\n\tjournal = {{ZOOKEYS}}\n}"
18
+
19
+ ```
20
+
21
+ If you want the doi:
22
+
23
+ ```ruby
24
+
25
+ Ref2bibtex.get_doi('Yoder, M. J., A. A. Valerio, A. Polaszek, L. Masner, and N. F. Johnson. 2009. Revision of Scelio pulchripennis - group species (Hymenoptera, Platygastroidea, Platygastridae). ZooKeys 20:53-118.') # => "http://dx.doi.org/10.3897/zookeys.20.205"
26
+
27
+ ```
28
+
29
+ If you have the doi:
30
+
31
+ ```ruby
32
+
33
+ Ref2bibtex.get_bibtex('http://dx.doi.org/10.3897/zookeys.20.205') # => "@article{Yoder_2009,\n\tdoi = {10.3897/zookeys.20.205},\n\turl = {http://dx.doi.org/10.3897/zookeys.20.205},\n\tyear = 2009,\n\tmonth = {sep},\n\tpublisher = {Pensoft Publishers},\n\tvolume = {20},\n\tnumber = {0},\n\tauthor = {Matthew Yoder and Andrew Polaszek and Lubomir Masner and Norman Johnson and Alejandro Valerio},\n\ttitle = {Revision of Scelio pulchripennis - group species (Hymenoptera, Platygastroidea, Platygastridae)},\n\tjournal = {{ZOOKEYS}}\n}"
34
+
35
+ ```
36
+
37
+ faq
38
+ ===
39
+
40
+ What if there are multiple results?
41
+ -----------------------------------
42
+ The code is dumb, it takes the first.
43
+
44
+ acknowledgements
45
+ ================
46
+
47
+ The Crossref API, and John Hill for his Python version.
48
+
49
+ license
50
+ =======
51
+
52
+ NCSA, UI flavour.
53
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,3 @@
1
+ module Ref2bibtex
2
+ VERSION = "0.0.1"
3
+ end
data/lib/ref2bibtex.rb ADDED
@@ -0,0 +1,94 @@
1
+ recent_ruby = RUBY_VERSION >= '2.1.0'
2
+ raise "IMPORTANT: gem requires ruby >= 2.1.0" unless recent_ruby
3
+
4
+ require "ref2bibtex/version"
5
+ require 'json'
6
+ require 'net/http'
7
+
8
+ module Ref2bibtex
9
+
10
+ CROSSREF_URI = URI('http://search.crossref.org/links')
11
+
12
+ # Parse the response into json
13
+ def self.parse_json(string)
14
+ begin
15
+ @json = JSON.parse(string)
16
+ rescue JSON::ParserError => e
17
+ puts e.message
18
+ ap request
19
+ end
20
+ end
21
+
22
+ # Pass a String doi get a bibtex formatted string back
23
+ def self.get_bibtex(doi)
24
+ response = Ref2bibtex.request(URI(doi), headers: {'Accept' => 'application/x-bibtex' }, protocol: 'GET', process_response_as: 'text')
25
+ end
26
+
27
+ # Pass a String citation, get a doi back
28
+ def self.get_doi(citation)
29
+ if citation.class == String
30
+ citation = [citation]
31
+ elsif citation.class != Array
32
+ raise
33
+ end
34
+
35
+ response = Ref2bibtex.request(payload: citation)
36
+ response['results'].first['doi']
37
+ end
38
+
39
+ # Pass a citation, get a String in bibtex back
40
+ def self.citation2bibtex(citation)
41
+ get_bibtex( get_doi(citation) )
42
+ end
43
+
44
+ class << self
45
+ alias_method :get, :citation2bibtex
46
+ end
47
+
48
+ def self.request(url = CROSSREF_URI, payload: nil, headers: {'content-type' => 'application/json' }, protocol: 'POST', process_response_as: 'json', redirect_limit: 10)
49
+ raise 'Infinite redirect?' if redirect_limit == 0
50
+ data = nil
51
+ if protocol == 'POST'
52
+ if payload.nil?
53
+ payload = {}
54
+ end
55
+ data = JSON.generate(payload) # Json.new(payload) # utf-8 encoding?
56
+ else
57
+ data = nil
58
+ end
59
+
60
+ if protocol == 'POST'
61
+ request = Net::HTTP::Post.new(url, initheader = headers)
62
+ elsif protocol == 'GET'
63
+ request = Net::HTTP::Get.new(url, initheader = headers)
64
+ end
65
+
66
+ response = Net::HTTP.start(request.uri.hostname, request.uri.port) do |http|
67
+ request.body = data
68
+ http.request(request)
69
+ end
70
+
71
+ case response
72
+ when Net::HTTPSuccess then
73
+ response = response
74
+ when Net::HTTPRedirection then
75
+ url = URI(response['location'])
76
+ request = Net::HTTP::Get.new(url, initheader = {'Accept' => 'application/x-bibtex'})
77
+ response = Net::HTTP.start(request.uri.hostname, request.uri.port) do |http|
78
+ http.request(request)
79
+ end
80
+ else
81
+ response = response.value
82
+ end
83
+
84
+ case process_response_as
85
+ when 'text'
86
+ response.body
87
+ when 'json'
88
+ parse_json(response.body)
89
+ else
90
+ raise
91
+ end
92
+ end
93
+
94
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ref2bibtex/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ref2bibtex"
8
+ spec.version = Ref2bibtex::VERSION
9
+ spec.authors = ["Matt Yoder"]
10
+ spec.email = ["diapriid@gmail.com"]
11
+ spec.summary = %q{Pass a full citation, get the bibtex back, that's all.}
12
+ spec.description = %q{Ok, maybe a bit more, you can have the DOI too.}
13
+ spec.homepage = "http://github.com/SpeciesFileGroup/ref2bibtex"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "byebug"
24
+ spec.add_development_dependency "awesome_print"
25
+
26
+ end
@@ -0,0 +1,45 @@
1
+ require 'spec_helper'
2
+
3
+ describe Ref2bibtex do
4
+
5
+ context '#request' do
6
+ let(:request) { Ref2bibtex.request(payload: [CITATIONS[:first]]) }
7
+
8
+ specify '#Ref2bibtex.request makes a query_ok request' do
9
+ expect(request['query_ok']).to eq(true)
10
+ end
11
+
12
+ specify '#Ref2bibtex.request returns Hash' do
13
+ expect(request.class).to eq(Hash)
14
+ end
15
+
16
+ specify '#Ref2bibtex.request results is an Array' do
17
+ expect(request['results'].class).to eq(Array)
18
+ end
19
+
20
+ specify '#Ref2bibtex.request results is an Array of Hashes' do
21
+ expect(request['results'].first.class).to eq(Hash)
22
+ end
23
+
24
+ specify '#Ref2bibtex.request results is an Array of Hashes, each one having a doi' do
25
+ expect(request['results'].first['doi']).to be_truthy
26
+ end
27
+ end
28
+
29
+ context '#get_doi' do
30
+ specify 'Ref2bibtex.get_doi() takes a full citation and returns a string' do
31
+ expect(Ref2bibtex.get_doi(CITATIONS[:first])).to eq('http://dx.doi.org/10.3897/zookeys.20.205')
32
+ end
33
+ end
34
+
35
+ context '#get_bibtex' do
36
+ let(:response) { Ref2bibtex.get_bibtex('http://dx.doi.org/10.3897/zookeys.20.205')}
37
+ specify 'Ref2bibtex.get_bibtex() takes a full citation and returns bibtex' do
38
+ expect(response).to match(/author\s=/)
39
+ expect(response).to match(/title\s=/)
40
+ expect(response).to match(/year\s=\s2009/)
41
+ end
42
+ end
43
+
44
+
45
+ end
@@ -0,0 +1,26 @@
1
+ # require 'coveralls'
2
+ # Coveralls.wear!
3
+
4
+ # These are development helpers.
5
+ require 'awesome_print'
6
+ require 'byebug'
7
+ require 'yaml'
8
+
9
+ # These are required to run tests.
10
+ require 'ref2bibtex'
11
+
12
+
13
+ CITATIONS = YAML.load_file(File.expand_path('support/citations.yml', File.dirname(__FILE__)))
14
+
15
+
16
+ RSpec.configure do |config|
17
+ # Use color in STDOUT
18
+ config.color = true
19
+
20
+ # Use color not only in STDOUT but also in pagers and files
21
+ config.tty = true
22
+
23
+ # Use the specified formatter
24
+ config.formatter = :documentation # :progress, :html, :textmate
25
+ end
26
+
@@ -0,0 +1,9 @@
1
+ ---
2
+ :first:
3
+ 'Yoder, M. J., A. A. Valerio, A. Polaszek, L. Masner, and N. F. Johnson. 2009. Revision of Scelio pulchripennis - group species (Hymenoptera, Platygastroidea, Platygastridae). ZooKeys 20:53-118.'
4
+ :second:
5
+ 'Jost & K.L. Shaw. 2006. Phylogeny of Ensifera (Hexapoda: Orthoptera) using three ribosomal loci, with implications for the evolution of acoustic communication. Molecular Phylogenetics and Evolution (Mol. Phylogenet. Evol.) 38:510-530'
6
+ :third:
7
+ 'Collins, N., van den Berghe & Carson. 2014. Two new species of Neoxabea, three new species of Oecanthus, and documentation of two other species in Nicaragua (Orthoptera: Gryllidae: Oecanthinae). Transactions of the American Entomological Society (Trans. Amer. Entomol. Soc.) 140:170'
8
+ :fourth:
9
+ "Otte, D. 1994. Crickets (Grylloidea). Orthoptera Species File, The Orthopterists' Society and The Academy of Natural Sciences of Philadelphia, Philadelphia 1:87"
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ref2bibtex
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Matt Yoder
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-09-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: byebug
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: awesome_print
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Ok, maybe a bit more, you can have the DOI too.
70
+ email:
71
+ - diapriid@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - Gemfile
78
+ - Gemfile.lock
79
+ - LICENSE
80
+ - README.md
81
+ - Rakefile
82
+ - lib/ref2bibtex.rb
83
+ - lib/ref2bibtex/version.rb
84
+ - ref2bibtex.gemspec
85
+ - spec/ref2bibtex_spec.rb
86
+ - spec/spec_helper.rb
87
+ - spec/support/citations.yml
88
+ homepage: http://github.com/SpeciesFileGroup/ref2bibtex
89
+ licenses:
90
+ - MIT
91
+ metadata: {}
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 2.2.2
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: Pass a full citation, get the bibtex back, that's all.
112
+ test_files:
113
+ - spec/ref2bibtex_spec.rb
114
+ - spec/spec_helper.rb
115
+ - spec/support/citations.yml