Class: Talis::Bibliography::Work

Inherits:
Resource show all
Extended by:
Forwardable, Talis::Bibliography, OAuthService
Defined in:
lib/talis/bibliography/work.rb

Overview

Represents bibliographic works API operations provided by the Metatron gem https://github.com/talis/metatron_rb

In order to perform remote operations, the client must be configured with a valid OAuth client that is allowed to query nodes:

Talis::Authentication.client_id = 'client_id'
Talis::Authentication.client_secret = 'client_secret'

Instance Attribute Summary collapse

Attributes included from OAuthService

#client_id, #client_secret, #oauth_host

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Talis::Bibliography

api_client

Methods inherited from Resource

handle_response, new_req_id

Constructor Details

#initialize(work_data = nil) ⇒ Work

Returns a new instance of Work.



90
91
92
93
94
95
96
# File 'lib/talis/bibliography/work.rb', line 90

def initialize(work_data = nil)
  if work_data.is_a? MetatronClient::WorkData
    parse_work_data work_data
  else
    @work_data = MetatronClient::WorkData.new
  end
end

Instance Attribute Details

#assetsObject (readonly)

TODO: call assets route if not set



104
105
106
# File 'lib/talis/bibliography/work.rb', line 104

def assets
  @assets
end

#idObject

Returns the value of attribute id.



19
20
21
# File 'lib/talis/bibliography/work.rb', line 19

def id
  @id
end

#manifestationsObject (readonly)

TODO: call manifestation route if not set



99
100
101
# File 'lib/talis/bibliography/work.rb', line 99

def manifestations
  @manifestations
end

#titleObject

Returns the value of attribute title.



19
20
21
# File 'lib/talis/bibliography/work.rb', line 19

def title
  @title
end

#typeObject

Returns the value of attribute type.



19
20
21
# File 'lib/talis/bibliography/work.rb', line 19

def type
  @type
end

#work_dataObject (readonly)

Returns the value of attribute work_data.



18
19
20
# File 'lib/talis/bibliography/work.rb', line 18

def work_data
  @work_data
end

Class Method Details

.find(request_id: new_req_id, query:, include: [], opts: {}) ⇒ MetatronClient::WorkResultSet

Search for bibliographic works

where works are of type Talis::Bibliography::Work, which are also available

directly via the Enumerable methods: each, find, find_all, first, last

Parameters:

  • request_id (String) (defaults to: new_req_id)

    (‘uuid’) unique ID for the remote request.

  • query (String)

    the query to filter works on

  • include (Array) (defaults to: [])

    the related resources to associate with each work see https://github.com/talis/metatron_rb/blob/master/docs/DefaultApi.md#2_works_get

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

    Can include key/value pairs for offset (default: 0), limit (default 20), and escape_query (boolean), which will escape any reserved characters from the query parameter

Returns:

  • (MetatronClient::WorkResultSet)

    containing data and meta attributes. The structure is as follows:

    {
      data: [work1, work2, work3, work4, work5],
      meta: { offset: 0, count: 20, limit: 5 }
      included: [manifestation|assset1, manifestation|assset2]
    }
    

Raises:



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/talis/bibliography/work.rb', line 45

def find(request_id: new_req_id, query:, include: [], opts: {})
  query = escape_query(query) if opts[:escape_query]
  offset = opts[:offset] || 0
  limit = opts[:limit] || 20
  search_works(request_id, query, offset, limit, include)
rescue MetatronClient::ApiError => error
  begin
    handle_response(error)
  rescue Talis::NotFoundError
    empty_result(offset, limit)
  end
end

.get(request_id: new_req_id, id:) ⇒ Object

Fetch a single work by id

Parameters:

  • request_id (String) (defaults to: new_req_id)

    (‘uuid’) unique ID for the remote request.

  • id (String)

    the ID of the work to fetch.

Returns:

  • Talis::Bibliography::Work or nil if the work cannot be found.

Raises:



66
67
68
69
70
71
72
73
74
# File 'lib/talis/bibliography/work.rb', line 66

def get(request_id: new_req_id, id:)
  new api_client(request_id).works_work_id_assets_get(id, token).data
rescue MetatronClient::ApiError => error
  begin
    handle_response(error)
  rescue Talis::NotFoundError
    nil
  end
end

Instance Method Details

#hydrate_relationships(resources) ⇒ Object

By default, the metatron client returns generic ResourceLink objects as the related resources. When passed an array of Metatron::ResourceData objects, it will replace the ResourceLink objects with more appropriately typed objects

Parameters:

  • resources (Array)

    an array of Metatron::ResourceData objects



113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/talis/bibliography/work.rb', line 113

def hydrate_relationships(resources)
  manifestations.map! do |m|
    resource = find_relationship_in_included m.manifestation_data.to_hash,
                                             resources
    return m unless resource
    hydrate_manifestation_assets resource, resources
    Manifestation.new(MetatronClient::ManifestationData.new(
                        resource.to_hash
    ))
  end
  nil
end