Class: Europeana::Record

Inherits:
Object
  • Object
show all
Defined in:
lib/europeana/record.rb

Overview

Interface to the Europeana API Record method

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, params = {}) ⇒ Record

Returns a new instance of Record.

Parameters:

  • id (String)

    Europeana ID of the record

  • params (Hash) (defaults to: {})

    Request parameters


20
21
22
23
# File 'lib/europeana/record.rb', line 20

def initialize(id, params = {})
  self.id = id
  self.params = params
end

Instance Attribute Details

#idObject

Europeana ID of the record


11
12
13
# File 'lib/europeana/record.rb', line 11

def id
  @id
end

#paramsObject

Request parameters to send to the API


14
15
16
# File 'lib/europeana/record.rb', line 14

def params
  @params
end

Instance Method Details

#getHash

Sends a request for this record to the API

Returns:

  • (Hash)

    Record data


77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/europeana/record.rb', line 77

def get
  uri = request_uri
  http = Net::HTTP.new(uri.host, uri.port)
  request = Net::HTTP::Get.new(uri.request_uri)
  retries = Europeana.max_retries
  
  begin
    response = http.request(request)
  rescue Timeout::Error, Errno::ECONNREFUSED, EOFError
    retries -= 1
    raise unless retries > 0
    sleep Europeana.retry_delay
    retry
  end
  
  json = JSON.parse(response.body)
  raise Errors::RequestError, json['error'] unless json['success']
  json
rescue JSON::ParserError
  if response.code.to_i == 404    # Handle HTML 404 responses on malformed record ID, emulating API's
    # JSON response.

    raise Errors::RequestError, "Invalid record identifier: #{@id}"
  else
    raise Errors::ResponseError
  end
end

#params_with_authenticationHash

Returns query params with API key added

Returns:

  • (Hash)

Raises:


30
31
32
33
# File 'lib/europeana/record.rb', line 30

def params_with_authentication
  raise Europeana::Errors::MissingAPIKeyError unless Europeana.api_key.present?
  params.merge(:wskey => Europeana.api_key)
end

#request_uriURI

Gets the URI for this Record request with parameters

Returns:

  • (URI)

66
67
68
69
70
# File 'lib/europeana/record.rb', line 66

def request_uri
  uri = URI.parse(Europeana::URL + "/record" + "#{@id}.json")
  uri.query = params_with_authentication.to_query
  uri
end