Class: PurlFetcher::Client

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/purl_fetcher/client.rb,
lib/purl_fetcher/client/mods.rb,
lib/purl_fetcher/client/publish.rb,
lib/purl_fetcher/client/version.rb,
lib/purl_fetcher/client/unpublish.rb,
lib/purl_fetcher/client/release_tags.rb,
lib/purl_fetcher/client/upload_files.rb,
lib/purl_fetcher/client/legacy_publish.rb,
lib/purl_fetcher/client/publish_shelve.rb,
lib/purl_fetcher/client/direct_upload_request.rb,
lib/purl_fetcher/client/direct_upload_response.rb

Defined Under Namespace

Classes: AlreadyDeletedResponseError, Error, LegacyPublish, Mods, NotFoundResponseError, Publish, PublishShelve, Reader, ReleaseTags, ResponseError, Unpublish, UploadFiles

Constant Summary collapse

VERSION =
"1.5.4"
DirectUploadRequest =

This models the JSON that we send to the server.

Data.define(:checksum, :byte_size, :content_type, :filename) do
  def self.from_file(hexdigest:, byte_size:, file_name:, content_type:)
    new(checksum: hex_to_base64_digest(hexdigest),
        byte_size: byte_size,
        content_type: clean_content_type(content_type),
        filename: file_name)
  end

  def to_h
    {
      blob: { filename: filename, byte_size: byte_size, checksum: checksum,
              content_type: self.class.clean_content_type(content_type) }
    }
  end

  def to_json(*_args)
    JSON.generate(to_h)
  end

  def self.clean_content_type(content_type)
    return "application/octet-stream" if content_type.blank?

    # ActiveStorage is expecting "application/x-stata-dta" not "application/x-stata-dta;version=14"
    content_type.split(";").first
  end

  def self.hex_to_base64_digest(hexdigest)
    [ [ hexdigest ].pack("H*") ].pack("m0")
  end
end
DirectUploadResponse =
Data.define(:id, :key, :checksum, :byte_size, :content_type,
:filename, :metadata, :created_at, :direct_upload,
:signed_id, :service_name) do
  def with_filename(filename)
    self.class.new(**deconstruct_keys(nil).merge(filename:))
  end
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#configObject

Returns the value of attribute config.



52
53
54
# File 'lib/purl_fetcher/client.rb', line 52

def config
  @config
end

Class Method Details

.configure(url:, logger: default_logger, token: nil) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/purl_fetcher/client.rb', line 35

def configure(url:, logger: default_logger, token: nil)
  instance.config = Config.new(
    url: url,
    logger: logger,
    token: token
  )

  instance
end

.default_loggerObject



45
46
47
# File 'lib/purl_fetcher/client.rb', line 45

def default_logger
  Logger.new($stdout)
end

Instance Method Details

#delete(path:) ⇒ Object

Send an DELETE request

Parameters:

  • path (String)

    the path for the API request

Raises:



56
57
58
59
60
61
62
63
# File 'lib/purl_fetcher/client.rb', line 56

def delete(path:)
  response = connection.delete(path)

  raise AlreadyDeletedResponseError, response.body if response.status == 409
  raise "unexpected response: #{response.status} #{response.body}" unless response.success?

  response.body
end

#post(path:, body:) ⇒ Object

Send an POST request

Parameters:

  • path (String)

    the path for the API request

  • body (String)

    the body of the POST request



68
69
70
71
72
73
74
75
76
# File 'lib/purl_fetcher/client.rb', line 68

def post(path:, body:)
  response = connection.post(path) do |request|
    request.body = body
  end

  raise "unexpected response: #{response.status} #{response.body}" unless response.success?

  response.body
end

#put(path:, body:, headers: {}) ⇒ Object

Send an PUT request

Parameters:

  • path (String)

    the path for the API request

  • body (String)

    the body of the POST request

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

    extra headers to add to the SDR API request



82
83
84
85
86
87
88
89
90
91
# File 'lib/purl_fetcher/client.rb', line 82

def put(path:, body:, headers: {})
  response = connection.put(path) do |request|
    request.body = body
    request.headers = default_headers.merge(headers)
  end

  raise "unexpected response: #{response.status} #{response.body}" unless response.success?

  response.body
end