Class: Surfliner::MetadataConsumer::SuperskunkClient

Inherits:
Object
  • Object
show all
Defined in:
lib/surfliner/metadata_consumer/superskunk_client.rb

Overview

Retrieves resources from Superskunk.

Defined Under Namespace

Classes: UnexpectedResponse

Constant Summary collapse

DEFAULT_JSONLD_PROFILE =

The JSON-LD profile to request when retrieving the resource

"tag:surfliner.gitlab.io,2022:api/oai_dc"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user_agent: SuperskunkClient.default_user_agent, jsonld_profile: DEFAULT_JSONLD_PROFILE) ⇒ SuperskunkClient

Returns a new instance of SuperskunkClient.



11
12
13
14
# File 'lib/surfliner/metadata_consumer/superskunk_client.rb', line 11

def initialize(user_agent: SuperskunkClient.default_user_agent, jsonld_profile: DEFAULT_JSONLD_PROFILE)
  @user_agent = user_agent
  @jsonld_profile = jsonld_profile
end

Instance Attribute Details

#jsonld_profileObject (readonly)

Returns the value of attribute jsonld_profile.



9
10
11
# File 'lib/surfliner/metadata_consumer/superskunk_client.rb', line 9

def jsonld_profile
  @jsonld_profile
end

#user_agentObject (readonly)

Returns the value of attribute user_agent.



8
9
10
# File 'lib/surfliner/metadata_consumer/superskunk_client.rb', line 8

def user_agent
  @user_agent
end

Class Method Details

.default_user_agentObject



21
22
23
# File 'lib/surfliner/metadata_consumer/superskunk_client.rb', line 21

def default_user_agent
  ENV.fetch("USER_AGENT_PRODUCT_NAME") { "surfliner.daylight" } # TODO: make this more obviously configurable?
end

.get(url) ⇒ Object



17
18
19
# File 'lib/surfliner/metadata_consumer/superskunk_client.rb', line 17

def get(url)
  new.get(url)
end

Instance Method Details

#get(url) ⇒ Hash

Retrieves the specified resource.

Parameters:

  • url (String)

    The resource URL as a string

Returns:

  • (Hash)

    parsed JSON response data

Raises:



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/surfliner/metadata_consumer/superskunk_client.rb', line 31

def get(url)
  uri = URI(url)
  req = Net::HTTP::Get.new(uri)
  req["Accept"] = "application/ld+json;profile=\"#{jsonld_profile}\""
  req["User-Agent"] = user_agent

  response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == "https") do |http|
    http.request(req)
  end

  case response
  when Net::HTTPSuccess
    JSON.parse(response.body)
  when Net::HTTPRedirection
    get(response["Location"])
  else
    raise UnexpectedResponse.new(uri, response.code.to_i)
  end
end