altmetric.rb 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +61 -0
- data/Rakefile +32 -0
- data/bin/doi +9 -0
- data/lib/altmetric.rb +133 -0
- data/tests/ts_altmetric.rb +4 -0
- metadata +104 -0
data/README.md
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
Ruby Altmetric API Client
|
2
|
+
-------------------------
|
3
|
+
|
4
|
+
A simple Ruby client for the [Altmetric API][0].
|
5
|
+
|
6
|
+
Provides a basic client object for interacting with the API. Provides quick access to the JSON
|
7
|
+
results or direct access to API responses.
|
8
|
+
|
9
|
+
Installation
|
10
|
+
------------
|
11
|
+
|
12
|
+
[sudo] gem install altmetric.rb
|
13
|
+
|
14
|
+
Relies on the `json`, `uri_template` and `httpclient` gems.
|
15
|
+
|
16
|
+
Basic Usage
|
17
|
+
-----------
|
18
|
+
|
19
|
+
require 'altmetric'
|
20
|
+
|
21
|
+
client = Altmetric::Client.new()
|
22
|
+
stats = client.doi("10.1038/news.2011.490")
|
23
|
+
#do something with the stats
|
24
|
+
|
25
|
+
Creating A Client
|
26
|
+
-----------------
|
27
|
+
|
28
|
+
The client can be created with an API key which is required to raise usage limits and also to access
|
29
|
+
the commercial parts of the API (the `fetch`) calls
|
30
|
+
|
31
|
+
opts = {
|
32
|
+
:apikey => "12345",
|
33
|
+
:user_agent => "MyCoolApp/1.0"
|
34
|
+
}
|
35
|
+
|
36
|
+
client = Altmetric::Client.new(opts)
|
37
|
+
|
38
|
+
Default `User-Agent` is currently `altmetric-ruby-client/0.0.1`
|
39
|
+
|
40
|
+
Read the Altmetric API documentation for notes on the structure of the responses and additional API parameters.
|
41
|
+
|
42
|
+
Rate Limiting
|
43
|
+
-------------
|
44
|
+
|
45
|
+
The client object will automatically inspect all responses and extract the HTTP headers that Altmetric
|
46
|
+
uses for [rate limiting][1].
|
47
|
+
|
48
|
+
The latest header values are automatically added as integers to the `Altmetric::Client::LIMITS` hash,
|
49
|
+
keyed on the header name. This simplifies monitoring limits over several requests, which may use different
|
50
|
+
clients.
|
51
|
+
|
52
|
+
License
|
53
|
+
-------
|
54
|
+
|
55
|
+
This work is hereby released into the Public Domain.
|
56
|
+
|
57
|
+
To view a copy of the public domain dedication, visit http://creativecommons.org/licenses/publicdomain or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.
|
58
|
+
|
59
|
+
|
60
|
+
[0]. http://api.altmetric.com/
|
61
|
+
[1]. http://api.altmetric.com/index.html#rate_limiting
|
data/Rakefile
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rdoc/task'
|
3
|
+
require 'rake/testtask'
|
4
|
+
require 'rake/clean'
|
5
|
+
|
6
|
+
CLEAN.include ['*.gem', 'pkg']
|
7
|
+
|
8
|
+
$spec = eval(File.read('altmetric.spec'))
|
9
|
+
|
10
|
+
Rake::RDocTask.new do |rdoc|
|
11
|
+
rdoc.rdoc_dir = 'doc/rdoc'
|
12
|
+
rdoc.options += RDOC_OPTS
|
13
|
+
rdoc.rdoc_files.include("README.md", "lib/**/*.rb")
|
14
|
+
rdoc.main = "README.md"
|
15
|
+
end
|
16
|
+
|
17
|
+
Rake::TestTask.new do |test|
|
18
|
+
test.test_files = FileList['tests/tc_*.rb']
|
19
|
+
end
|
20
|
+
|
21
|
+
task :package do
|
22
|
+
sh %{gem build altmetric.spec}
|
23
|
+
end
|
24
|
+
|
25
|
+
task :install do
|
26
|
+
sh %{sudo gem install --no-ri --no-rdoc #{$spec.name}-#{$spec.version}.gem}
|
27
|
+
end
|
28
|
+
|
29
|
+
desc "Uninstall the gem"
|
30
|
+
task :uninstall => [:clean] do
|
31
|
+
sh %{sudo gem uninstall #{$spec.name}}
|
32
|
+
end
|
data/bin/doi
ADDED
data/lib/altmetric.rb
ADDED
@@ -0,0 +1,133 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'rest-client'
|
3
|
+
require 'httpclient'
|
4
|
+
require 'uri_template'
|
5
|
+
|
6
|
+
module Altmetric
|
7
|
+
|
8
|
+
class Client
|
9
|
+
|
10
|
+
DEFAULT_USER_AGENT = "altmetric-ruby-client/0.0.1"
|
11
|
+
URI_TEMPLATE = URITemplate.new("http://{host}/{version}{/path*}")
|
12
|
+
|
13
|
+
HOST = "api.altmetric.com"
|
14
|
+
VERSION = "v1"
|
15
|
+
KEY_PARAM="key"
|
16
|
+
|
17
|
+
HOURLY_RATE_LIMIT="X-HourlyRateLimit-Limit"
|
18
|
+
DAILY_RATE_LIMIT="X-DailyRateLimit-Limit"
|
19
|
+
HOURLY_RATE_LIMIT_REMAINING="X-HourlyRateLimit-Remaining"
|
20
|
+
DAILY_RATE_LIMIT_REMAINING="X-DailyRateLimit-Remaining"
|
21
|
+
|
22
|
+
RATE_HEADERS = [HOURLY_RATE_LIMIT, DAILY_RATE_LIMIT, HOURLY_RATE_LIMIT_REMAINING, DAILY_RATE_LIMIT_REMAINING]
|
23
|
+
|
24
|
+
LIMITS = {}
|
25
|
+
|
26
|
+
#Format a URL according to provided template
|
27
|
+
#
|
28
|
+
#Automatically injects the correct :host and :version params for
|
29
|
+
#the API
|
30
|
+
#
|
31
|
+
#template:: a valid URI template
|
32
|
+
#opts:: additional template params, should include :path
|
33
|
+
def self.make_url(template, opts)
|
34
|
+
return template.expand(
|
35
|
+
{ :host => HOST,
|
36
|
+
:version => VERSION
|
37
|
+
}.merge(opts)
|
38
|
+
)
|
39
|
+
end
|
40
|
+
|
41
|
+
#Update class variable with latest rate limit data
|
42
|
+
#
|
43
|
+
#headers:: hash of response headers
|
44
|
+
def self.update_limits(headers)
|
45
|
+
RATE_HEADERS.each do |header|
|
46
|
+
LIMITS[header] = headers[header].to_i if headers[header]
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
#Create a new client object
|
51
|
+
#
|
52
|
+
#Supports several options in the provided hash, including:
|
53
|
+
#
|
54
|
+
#[apikey]:: specify altmetric API key, only required for +fetch+ method and increased rate limits
|
55
|
+
#[client]:: specify a pre-created HTTPClient object (e.g. for mocking during testing)
|
56
|
+
#[user_agent]]:: specify a user agent. Default is +DEFAULT_USER_AGENT+
|
57
|
+
#
|
58
|
+
#Method params:
|
59
|
+
#
|
60
|
+
#opts:: options for configuring client
|
61
|
+
def initialize(opts={})
|
62
|
+
name = opts[:user_agent] || DEFAULT_USER_AGENT
|
63
|
+
@client = opts[:client] || HTTPClient.new( :agent_name => name )
|
64
|
+
@apikey = opts[:apikey] || nil
|
65
|
+
@opts = opts
|
66
|
+
end
|
67
|
+
|
68
|
+
#Fetch altmetrics for a DOI
|
69
|
+
def doi(id, &block)
|
70
|
+
return get_metrics(["doi", id], &block)
|
71
|
+
end
|
72
|
+
|
73
|
+
#Fetch citations for a DOI
|
74
|
+
#
|
75
|
+
#Read the {API documentation}[http://api.altmetric.com/docs/call_citations.html] for explanation of parameters
|
76
|
+
def citations(timeframe, params, &block)
|
77
|
+
return get_metrics(["citations", timeframe], params, &block)
|
78
|
+
end
|
79
|
+
|
80
|
+
#Fetch altmetrics using (unstable) altmetrics ids
|
81
|
+
def id(id, &block)
|
82
|
+
return get_metrics(["id", id], &block)
|
83
|
+
end
|
84
|
+
|
85
|
+
#Fetch altmetrics for a Pubmed identifier
|
86
|
+
def pmid(id, &block)
|
87
|
+
return get_metrics(["pmid", id], &block)
|
88
|
+
end
|
89
|
+
|
90
|
+
#Fetch altmetrics for Arxiv id
|
91
|
+
def arxiv(id, &block)
|
92
|
+
return get_metrics(["arxiv", id], &block)
|
93
|
+
end
|
94
|
+
|
95
|
+
#Fetch altmetrics for ADS bibcode
|
96
|
+
def ads(id, &block)
|
97
|
+
return get_metrics(["ads", id], &block)
|
98
|
+
end
|
99
|
+
|
100
|
+
def fetch(type, id, params)
|
101
|
+
return get_metrics(["fetch", type, id], params, &block)
|
102
|
+
end
|
103
|
+
|
104
|
+
#Get metrics, specifying path, query parameters, and headers
|
105
|
+
#
|
106
|
+
#Accepts a block for direct processing of the result, otherwise
|
107
|
+
#response is validated to ensure its a success then parsed as JSON
|
108
|
+
def get_metrics(path, query={}, headers={})
|
109
|
+
url = Client::make_url(URI_TEMPLATE, {:path=>path} )
|
110
|
+
response = get( url, query, headers )
|
111
|
+
if block_given?
|
112
|
+
yield response
|
113
|
+
end
|
114
|
+
validate_response(response)
|
115
|
+
return JSON.parse( response.content )
|
116
|
+
end
|
117
|
+
|
118
|
+
def get(uri, query={}, headers={})
|
119
|
+
query[KEY_PARAM] = @apikey if @apikey
|
120
|
+
headers["Accept"] = "application/json"
|
121
|
+
response = @client.get(uri, query, headers)
|
122
|
+
Client.update_limits(response.headers)
|
123
|
+
return response
|
124
|
+
end
|
125
|
+
|
126
|
+
def validate_response(response)
|
127
|
+
if response.status != 200
|
128
|
+
raise "Unable to perform request. Status: #{response.status}. Message: #{response.content}"
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
end
|
133
|
+
end
|
metadata
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: altmetric.rb
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Leigh Dodds
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-05-21 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: json
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: httpclient
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: uri_template
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :runtime
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
description: Altmetric API Client
|
63
|
+
email: leigh@ldodds.com
|
64
|
+
executables: []
|
65
|
+
extensions: []
|
66
|
+
extra_rdoc_files:
|
67
|
+
- README.md
|
68
|
+
files:
|
69
|
+
- README.md
|
70
|
+
- Rakefile
|
71
|
+
- bin/doi
|
72
|
+
- lib/altmetric.rb
|
73
|
+
- tests/ts_altmetric.rb
|
74
|
+
homepage: http://github.com/ldodds/altmetric
|
75
|
+
licenses: []
|
76
|
+
post_install_message:
|
77
|
+
rdoc_options:
|
78
|
+
- --quiet
|
79
|
+
- --title
|
80
|
+
- Altmetric Client
|
81
|
+
- --main
|
82
|
+
- README.md
|
83
|
+
require_paths:
|
84
|
+
- lib
|
85
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
86
|
+
none: false
|
87
|
+
requirements:
|
88
|
+
- - ! '>='
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: 1.9.3
|
91
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
92
|
+
none: false
|
93
|
+
requirements:
|
94
|
+
- - ! '>='
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
requirements: []
|
98
|
+
rubyforge_project:
|
99
|
+
rubygems_version: 1.8.23
|
100
|
+
signing_key:
|
101
|
+
specification_version: 3
|
102
|
+
summary: Altmetric API Client
|
103
|
+
test_files:
|
104
|
+
- tests/ts_altmetric.rb
|