-
Notifications
You must be signed in to change notification settings - Fork 0
/
zotero_client.rb
75 lines (61 loc) · 1.46 KB
/
zotero_client.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
require 'faraday'
require 'atom'
# TODO: extract to gem, probably improve the public interface
module ZoteroClient
class User
attr_reader :uid, :key
def initialize(uid, key)
@uid, @key = uid, key
end
def items
Item.for_user(self)
end
end
class Item
def self.for_user(user)
url = "https://api.zotero.org/users/#{user.uid}/items?key=#{user.key}&format=atom&content=json"
response = get_cached_http(url)
feed = Atom::Feed.load_feed(response.body)
content_hashes = feed.entries.map { |entry| JSON.parse(entry.content) }
content_hashes.map { |hash| self.new(hash) }
end
def initialize(attributes)
@attributes = attributes
end
def title
@attributes["title"]
end
def url
@attributes["url"]
end
def journal_article?
@attributes["itemType"] == "journalArticle"
end
def identifier_strings
[
identifier_for('DOI'),
identifier_for('ISSN'),
identifier_for('url'),
extra_identifier_for('PMID'),
extra_identifier_for('PMCID')
].compact
end
private
def identifier_for(key)
value = @attributes[key]
if value.present?
"#{key.upcase}:#{value}"
else
nil
end
end
# TODO: add this
def extra_identifier_for(key)
nil
end
def self.get_cached_http(url)
$http_cache ||= {}
$http_cache[url] ||= Faraday.get(url)
end
end
end