Class: Altmetric::Client
- Inherits:
-
Object
- Object
- Altmetric::Client
- Defined in:
- lib/altmetric.rb
Constant Summary collapse
- DEFAULT_USER_AGENT =
"altmetric-ruby-client/0.0.1"
- URI_TEMPLATE =
URITemplate.new("http://{host}/{version}{/path*}")
- HOST =
"api.altmetric.com"
- VERSION =
"v1"
- KEY_PARAM =
"key"
- HOURLY_RATE_LIMIT =
"X-HourlyRateLimit-Limit"
- DAILY_RATE_LIMIT =
"X-DailyRateLimit-Limit"
- HOURLY_RATE_LIMIT_REMAINING =
"X-HourlyRateLimit-Remaining"
- DAILY_RATE_LIMIT_REMAINING =
"X-DailyRateLimit-Remaining"
- RATE_HEADERS =
[HOURLY_RATE_LIMIT, DAILY_RATE_LIMIT, HOURLY_RATE_LIMIT_REMAINING, DAILY_RATE_LIMIT_REMAINING]
- LIMITS =
{}
Class Method Summary collapse
-
.make_url(template, opts) ⇒ Object
Format a URL according to provided template.
-
.update_limits(headers) ⇒ Object
Update class variable with latest rate limit data.
Instance Method Summary collapse
-
#ads(id, &block) ⇒ Object
Fetch altmetrics for ADS bibcode.
-
#arxiv(id, &block) ⇒ Object
Fetch altmetrics for Arxiv id.
-
#citations(timeframe, params, &block) ⇒ Object
Fetch citations for a DOI Read the API documentation for explanation of parameters.
-
#doi(id, &block) ⇒ Object
Fetch altmetrics for a DOI.
- #fetch(type, id, params) ⇒ Object
- #get(uri, query = {}, headers = {}) ⇒ Object
-
#get_metrics(path, query = {}, headers = {}) ⇒ Object
Get metrics, specifying path, query parameters, and headers.
-
#id(id, &block) ⇒ Object
Fetch altmetrics using (unstable) altmetrics ids.
-
#initialize(opts = {}) ⇒ Client
constructor
Create a new client object.
-
#pmid(id, &block) ⇒ Object
Fetch altmetrics for a Pubmed identifier.
- #validate_response(response) ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ Client
Create a new client object
Supports several options in the provided hash, including:
- [apikey]
-
specify altmetric API key, only required for
fetch
method and increased rate limits - [client]
-
specify a pre-created HTTPClient object (e.g. for mocking during testing)
- [user_agent]]
-
specify a user agent. Default is
DEFAULT_USER_AGENT
Method params:
- opts
-
options for configuring client
60 61 62 63 64 65 |
# File 'lib/altmetric.rb', line 60 def initialize(opts={}) name = opts[:user_agent] || DEFAULT_USER_AGENT @client = opts[:client] || HTTPClient.new( :agent_name => name ) @apikey = opts[:apikey] || nil @opts = opts end |
Class Method Details
.make_url(template, opts) ⇒ Object
Format a URL according to provided template
Automatically injects the correct :host and :version params for the API
- template
-
a valid URI template
- opts
-
additional template params, should include :path
32 33 34 35 36 37 38 |
# File 'lib/altmetric.rb', line 32 def self.make_url(template, opts) return template.( { :host => HOST, :version => VERSION }.merge(opts) ) end |
.update_limits(headers) ⇒ Object
Update class variable with latest rate limit data
- headers
-
hash of response headers
43 44 45 46 47 |
# File 'lib/altmetric.rb', line 43 def self.update_limits(headers) RATE_HEADERS.each do |header| LIMITS[header] = headers[header].to_i if headers[header] end end |
Instance Method Details
#ads(id, &block) ⇒ Object
Fetch altmetrics for ADS bibcode
95 96 97 |
# File 'lib/altmetric.rb', line 95 def ads(id, &block) return get_metrics(["ads", id], &block) end |
#arxiv(id, &block) ⇒ Object
Fetch altmetrics for Arxiv id
90 91 92 |
# File 'lib/altmetric.rb', line 90 def arxiv(id, &block) return get_metrics(["arxiv", id], &block) end |
#citations(timeframe, params, &block) ⇒ Object
Fetch citations for a DOI
Read the API documentation for explanation of parameters
75 76 77 |
# File 'lib/altmetric.rb', line 75 def citations(timeframe, params, &block) return get_metrics(["citations", timeframe], params, &block) end |
#doi(id, &block) ⇒ Object
Fetch altmetrics for a DOI
68 69 70 |
# File 'lib/altmetric.rb', line 68 def doi(id, &block) return get_metrics(["doi", id], &block) end |
#fetch(type, id, params) ⇒ Object
99 100 101 |
# File 'lib/altmetric.rb', line 99 def fetch(type, id, params) return get_metrics(["fetch", type, id], params, &block) end |
#get(uri, query = {}, headers = {}) ⇒ Object
117 118 119 120 121 122 123 |
# File 'lib/altmetric.rb', line 117 def get(uri, query={}, headers={}) query[KEY_PARAM] = @apikey if @apikey headers["Accept"] = "application/json" response = @client.get(uri, query, headers) Client.update_limits(response.headers) return response end |
#get_metrics(path, query = {}, headers = {}) ⇒ Object
Get metrics, specifying path, query parameters, and headers
Accepts a block for direct processing of the result, otherwise response is validated to ensure its a success then parsed as JSON
107 108 109 110 111 112 113 114 115 |
# File 'lib/altmetric.rb', line 107 def get_metrics(path, query={}, headers={}) url = Client::make_url(URI_TEMPLATE, {:path=>path} ) response = get( url, query, headers ) if block_given? yield response end validate_response(response) return JSON.parse( response.content ) end |
#id(id, &block) ⇒ Object
Fetch altmetrics using (unstable) altmetrics ids
80 81 82 |
# File 'lib/altmetric.rb', line 80 def id(id, &block) return get_metrics(["id", id], &block) end |
#pmid(id, &block) ⇒ Object
Fetch altmetrics for a Pubmed identifier
85 86 87 |
# File 'lib/altmetric.rb', line 85 def pmid(id, &block) return get_metrics(["pmid", id], &block) end |
#validate_response(response) ⇒ Object
125 126 127 128 129 |
# File 'lib/altmetric.rb', line 125 def validate_response(response) if response.status != 200 raise "Unable to perform request. Status: #{response.status}. Message: #{response.content}" end end |