class NyplRepo::Client

Public Class Methods

new(token, options={}) click to toggle source

NyplRepo::Client.new(ENV["API_TOKEN"], {:debug=>true, :server_url => “api.nypl/api/v1/”})

# File lib/nypl_repo.rb, line 10
def initialize(token, options={})
  @token = token
  @debug = options[:debug] || false
  @server_url = options[:server_url] || "http://api.repo.nypl.org/api/v1"
end

Public Instance Methods

check_error(json) click to toggle source

checks the response for error

# File lib/nypl_repo.rb, line 149
def check_error(json)
  if json["nyplAPI"]["response"]["headers"]["status"] == "error"
      msg = "NYPL Repo API Error: " + json["nyplAPI"]["response"]["headers"]["code"] + " "+ json["nyplAPI"]["response"]["headers"]["message"]
      raise msg
   end

end
get_bibl_uuid(image_id) click to toggle source

get bibliographic container uuid from an image_id

# File lib/nypl_repo.rb, line 117
def get_bibl_uuid(image_id)
  url = "#{@server_url}/items/local_image_id/#{image_id}.json"   
  json = self.get_json(url)
  bibl_uuid = nil
  if json["nyplAPI"]["response"]["numResults"].to_i > 0
    bibl_uuid = json["nyplAPI"]["response"]["uuid"]
  end
 
  return bibl_uuid
end
get_capture_items(c_uuid) click to toggle source

Given a container uuid, or biblographic uuid, returns a list of mods uuids.

# File lib/nypl_repo.rb, line 44
def get_capture_items(c_uuid)
  url = "#{@server_url}/items/#{c_uuid}.json?per_page=500"
  json = self.get_json(url)
  captures = []
  capture = json["nyplAPI"]["response"]["capture"]
  captures << capture

  totalPages = json["nyplAPI"]["request"]["totalPages"].to_i
  if totalPages >= 2
    puts "total pages " + totalPages.to_s if @debug
    (2..totalPages).each do | page |
      puts "page: "+page.to_s if @debug
      newurl = url + "&page=#{page}"
      json = self.get_json(newurl)
      newcapture = json["nyplAPI"]["response"]["capture"]
      captures << newcapture
    end
  end
  captures.flatten!

  captures
end
get_image_id(bibl_uuid, mods_uuid) click to toggle source

gets the image id for an item based on the the bibliographic uuid (container uuid) and the mods uuid (the actual item)

# File lib/nypl_repo.rb, line 100
def get_image_id(bibl_uuid, mods_uuid)
  url = "#{@server_url}/items/#{bibl_uuid}.json?per_page=500"
  json = self.get_json(url)
  image_id = nil
  
  json["nyplAPI"]["response"]["capture"].each do | capture|
  if capture["uuid"] == mods_uuid
    image_id = capture["imageID"]
    break
  end #if
  end if json["nyplAPI"]["response"]["numResults"].to_i > 0

  return image_id
end
get_items_since(query, since_date, until_date) click to toggle source

date format: YYYY-MM-DD physical_location i.e “Map%20Division”&field=physicalLocation

# File lib/nypl_repo.rb, line 18
def get_items_since(query, since_date, until_date)
  url = @server_url+'/items/search.json?q='+query+'&since='+since_date+'&until='+until_date+'&per_page=500'
  json = self.get_json(url)
  results = []
  result = json["nyplAPI"]["response"]["result"]
  results << result
  totalPages = json["nyplAPI"]["request"]["totalPages"].to_i
  
  if totalPages >= 2
    puts "total pages " + totalPages.to_s if @debug
    (2..totalPages).each do | page |
      puts "page: "+page.to_s if @debug
      newurl = url + "&page=#{page}"
      json = self.get_json(newurl)
      newresult = json["nyplAPI"]["response"]["result"]
      results << newresult
    end
  end
  results.flatten!
 
  results
end
get_json(url) click to toggle source
# File lib/nypl_repo.rb, line 158
def get_json(url)
  puts "Calling URL: " + url if @debug

  uri = URI.parse(url)
  http = Net::HTTP.new(uri.host, uri.port)

  headers = { "Authorization" => "Token token=#{@token}" }
  request = Net::HTTP::Get.new(uri.request_uri, headers)
  response = http.request(request)
  body = response.body
  json = JSON.parse(body)

  check_error(json)

  return json
end
get_mods_item(mods_uuid) click to toggle source

get the item detail from a uuid

# File lib/nypl_repo.rb, line 68
def get_mods_item(mods_uuid)
  url = "#{@server_url}/items/mods/#{mods_uuid}.json"
  json = self.get_json(url)
  
  item = nil
  if json["nyplAPI"]["response"]["mods"]
    item = json["nyplAPI"]["response"]["mods"]
  end
  
  return item
end
get_mods_uuid(bibl_uuid, image_id) click to toggle source

gets the mods uuid of the item, passing in the bibliographic uuid and image_id since there could be many maps for the same item

# File lib/nypl_repo.rb, line 82
def get_mods_uuid(bibl_uuid, image_id)
 url = "#{@server_url}/items/#{bibl_uuid}.json?per_page=500"
 json = self.get_json(url)
 mods_uuid = nil
   
 json["nyplAPI"]["response"]["capture"].each do | capture|
   if capture["imageID"] == image_id
     mods_uuid = capture["uuid"]
     break
   end #if
 end if json["nyplAPI"]["response"]["numResults"].to_i > 0


 return mods_uuid
end