nypl_repo 0.0.2 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/nypl_repo.rb +95 -6
- metadata +9 -7
- checksums.yaml +0 -7
data/lib/nypl_repo.rb
CHANGED
@@ -5,13 +5,68 @@ module NyplRepo
|
|
5
5
|
require 'net/http'
|
6
6
|
require 'json'
|
7
7
|
|
8
|
-
|
8
|
+
# NyplRepo::Client.new(ENV["API_TOKEN"], {:debug=>true, :server_url => "http://api.nypl/api/v1/"})
|
9
|
+
#
|
10
|
+
def initialize(token, options={})
|
9
11
|
@token = token
|
12
|
+
@debug = options[:debug] || false
|
13
|
+
@server_url = options[:server_url] || "http://api.repo.nypl.org/api/v1"
|
10
14
|
end
|
11
|
-
|
15
|
+
|
16
|
+
#date format: YYYY-MM-DD
|
17
|
+
#physical_location i.e "Map%20Division"&field=physicalLocation
|
18
|
+
def get_items_since(query, since_date, until_date)
|
19
|
+
url = @server_url+'/items/search.json?q='+query+'&since='+since_date+'&until='+until_date+'&per_page=500'
|
20
|
+
json = self.get_json(url)
|
21
|
+
results = []
|
22
|
+
result = json["nyplAPI"]["response"]["result"]
|
23
|
+
results << result
|
24
|
+
totalPages = json["nyplAPI"]["request"]["totalPages"].to_i
|
25
|
+
|
26
|
+
if totalPages >= 2
|
27
|
+
puts "total pages " + totalPages.to_s if @debug
|
28
|
+
(2..totalPages).each do | page |
|
29
|
+
puts "page: "+page.to_s if @debug
|
30
|
+
newurl = url + "&page=#{page}"
|
31
|
+
json = self.get_json(newurl)
|
32
|
+
newresult = json["nyplAPI"]["response"]["result"]
|
33
|
+
results << newresult
|
34
|
+
end
|
35
|
+
end
|
36
|
+
results.flatten!
|
37
|
+
|
38
|
+
results
|
39
|
+
end
|
40
|
+
|
41
|
+
|
42
|
+
# Given a container uuid, or biblographic uuid, returns a
|
43
|
+
# list of mods uuids.
|
44
|
+
def get_capture_items(c_uuid)
|
45
|
+
url = "#{@server_url}/items/#{c_uuid}.json?per_page=500"
|
46
|
+
json = self.get_json(url)
|
47
|
+
captures = []
|
48
|
+
capture = json["nyplAPI"]["response"]["capture"]
|
49
|
+
captures << capture
|
50
|
+
|
51
|
+
totalPages = json["nyplAPI"]["request"]["totalPages"].to_i
|
52
|
+
if totalPages >= 2
|
53
|
+
puts "total pages " + totalPages.to_s if @debug
|
54
|
+
(2..totalPages).each do | page |
|
55
|
+
puts "page: "+page.to_s if @debug
|
56
|
+
newurl = url + "&page=#{page}"
|
57
|
+
json = self.get_json(newurl)
|
58
|
+
newcapture = json["nyplAPI"]["response"]["capture"]
|
59
|
+
captures << newcapture
|
60
|
+
end
|
61
|
+
end
|
62
|
+
captures.flatten!
|
63
|
+
|
64
|
+
captures
|
65
|
+
end
|
66
|
+
|
12
67
|
#get the item detail from a uuid
|
13
68
|
def get_mods_item(mods_uuid)
|
14
|
-
url = "
|
69
|
+
url = "#{@server_url}/items/mods/#{mods_uuid}.json"
|
15
70
|
json = self.get_json(url)
|
16
71
|
|
17
72
|
item = nil
|
@@ -25,7 +80,7 @@ module NyplRepo
|
|
25
80
|
#gets the mods uuid of the item, passing in the bibliographic uuid and image_id
|
26
81
|
#since there could be many maps for the same item
|
27
82
|
def get_mods_uuid(bibl_uuid, image_id)
|
28
|
-
url = "
|
83
|
+
url = "#{@server_url}/items/#{bibl_uuid}.json?per_page=500"
|
29
84
|
json = self.get_json(url)
|
30
85
|
mods_uuid = nil
|
31
86
|
|
@@ -40,10 +95,27 @@ module NyplRepo
|
|
40
95
|
return mods_uuid
|
41
96
|
end
|
42
97
|
|
98
|
+
# gets the image id for an item based on the the bibliographic uuid (container uuid) and the mods uuid (the actual item)
|
99
|
+
#
|
100
|
+
def get_image_id(bibl_uuid, mods_uuid)
|
101
|
+
url = "#{@server_url}/items/#{bibl_uuid}.json?per_page=500"
|
102
|
+
json = self.get_json(url)
|
103
|
+
image_id = nil
|
104
|
+
|
105
|
+
json["nyplAPI"]["response"]["capture"].each do | capture|
|
106
|
+
if capture["uuid"] == mods_uuid
|
107
|
+
image_id = capture["imageID"]
|
108
|
+
break
|
109
|
+
end #if
|
110
|
+
end if json["nyplAPI"]["response"]["numResults"].to_i > 0
|
111
|
+
|
112
|
+
return image_id
|
113
|
+
end
|
114
|
+
|
43
115
|
|
44
116
|
# get bibliographic container uuid from an image_id
|
45
117
|
def get_bibl_uuid(image_id)
|
46
|
-
url = "
|
118
|
+
url = "#{@server_url}/items/local_image_id/#{image_id}.json"
|
47
119
|
json = self.get_json(url)
|
48
120
|
bibl_uuid = nil
|
49
121
|
if json["nyplAPI"]["response"]["numResults"].to_i > 0
|
@@ -57,7 +129,7 @@ module NyplRepo
|
|
57
129
|
#get highreslink from an item, matching up the image idi
|
58
130
|
#since some bibliographic items may have many maps under them
|
59
131
|
def get_highreslink(bibl_uuid, image_id)
|
60
|
-
url = "
|
132
|
+
url = "#{@server_url}/items/#{bibl_uuid}.json?per_page=500"
|
61
133
|
json = self.get_json(url)
|
62
134
|
|
63
135
|
highreslink = nil
|
@@ -72,8 +144,20 @@ module NyplRepo
|
|
72
144
|
return highreslink
|
73
145
|
end
|
74
146
|
|
147
|
+
|
148
|
+
#checks the response for error
|
149
|
+
def check_error(json)
|
150
|
+
if json["nyplAPI"]["response"]["headers"]["status"] == "error"
|
151
|
+
msg = "NYPL Repo API Error: " + json["nyplAPI"]["response"]["headers"]["code"] + " "+ json["nyplAPI"]["response"]["headers"]["message"]
|
152
|
+
raise msg
|
153
|
+
end
|
154
|
+
|
155
|
+
end
|
156
|
+
|
75
157
|
|
76
158
|
def get_json(url)
|
159
|
+
puts "Calling URL: " + url if @debug
|
160
|
+
|
77
161
|
uri = URI.parse(url)
|
78
162
|
http = Net::HTTP.new(uri.host, uri.port)
|
79
163
|
|
@@ -83,8 +167,13 @@ module NyplRepo
|
|
83
167
|
body = response.body
|
84
168
|
json = JSON.parse(body)
|
85
169
|
|
170
|
+
check_error(json)
|
171
|
+
|
86
172
|
return json
|
87
173
|
end
|
88
174
|
|
89
175
|
end
|
90
176
|
end
|
177
|
+
|
178
|
+
|
179
|
+
|
metadata
CHANGED
@@ -1,14 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nypl_repo
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
5
6
|
platform: ruby
|
6
7
|
authors:
|
7
8
|
- Tim Waters
|
8
9
|
autorequire:
|
9
10
|
bindir: bin
|
10
11
|
cert_chain: []
|
11
|
-
date:
|
12
|
+
date: 2014-01-23 00:00:00.000000000 Z
|
12
13
|
dependencies: []
|
13
14
|
description: A basic library for interacting with the NYPL Digital Collections Repo
|
14
15
|
API
|
@@ -21,25 +22,26 @@ files:
|
|
21
22
|
homepage: https://github.com/timwaters/nypl_repo
|
22
23
|
licenses:
|
23
24
|
- MIT
|
24
|
-
metadata: {}
|
25
25
|
post_install_message:
|
26
26
|
rdoc_options: []
|
27
27
|
require_paths:
|
28
28
|
- lib
|
29
29
|
required_ruby_version: !ruby/object:Gem::Requirement
|
30
|
+
none: false
|
30
31
|
requirements:
|
31
|
-
- - '>='
|
32
|
+
- - ! '>='
|
32
33
|
- !ruby/object:Gem::Version
|
33
34
|
version: '0'
|
34
35
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
36
|
+
none: false
|
35
37
|
requirements:
|
36
|
-
- - '>='
|
38
|
+
- - ! '>='
|
37
39
|
- !ruby/object:Gem::Version
|
38
40
|
version: '0'
|
39
41
|
requirements: []
|
40
42
|
rubyforge_project:
|
41
|
-
rubygems_version:
|
43
|
+
rubygems_version: 1.8.25
|
42
44
|
signing_key:
|
43
|
-
specification_version:
|
45
|
+
specification_version: 3
|
44
46
|
summary: NYPL Repo API Library
|
45
47
|
test_files: []
|
checksums.yaml
DELETED
@@ -1,7 +0,0 @@
|
|
1
|
-
---
|
2
|
-
SHA1:
|
3
|
-
metadata.gz: d74c2cfe6f6f381876592cc50810d3d01ec4afc3
|
4
|
-
data.tar.gz: 930fea52d656c2d7f16919094a26f380d3456827
|
5
|
-
SHA512:
|
6
|
-
metadata.gz: 9c8c9a52ef34d01dd863a850be4ebb4d3f4e0fed5080892aa3aa3863d9b200e6ff49e932feaa679f052028eefe2087c971154e748b15c93cb95c7d024fae7c6c
|
7
|
-
data.tar.gz: f3ff1074e577c66e1c85355556a7c52486f1a08d0f257f602e32bfc19858a10cd99fc18b4df5ade323925640b9aefeb1bdbcfc7c2a76b324c5a79a5fc84ed9ff
|