Class: Matterhorn::Endpoint::Series

Inherits:
Matterhorn::Endpoint show all
Defined in:
lib/matterhorn/endpoint/series.rb

Overview

Matterhorn::Endpoint::Series ===

Instance Attribute Summary

Attributes inherited from Matterhorn::Endpoint

#response_body, #response_code

Instance Method Summary collapse

Methods inherited from Matterhorn::Endpoint

#close, create, #error_code, #error_msg, #error_occurred?, #initialize, open

Constructor Details

This class inherits a constructor from Matterhorn::Endpoint

Instance Method Details

#countObject



139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/matterhorn/endpoint/series.rb', line 139

def count
  count = 0
  begin
    split_response http_endpoint_client.get(
      "series/count"
    )
    count = response_body.to_i
  rescue => ex
    exception_handler('count', ex, {})
  end
  count
end

#create(dublin_core, acl = nil) ⇒ Object

Create a new Series on Mattherhorn. Return the dublin core of the created Series. In the property dcterms:identifier is written the Matterhorn Series Id.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/matterhorn/endpoint/series.rb', line 17

def create(dublin_core, acl = nil)
  dc = nil
  begin
    split_response http_endpoint_client.post(
      "series",
      { 'series' => dublin_core.to_xml, 'acl' => acl ? acl.to_xml : '' }
    )
    dc = Matterhorn::DublinCore.new(response_body)
  rescue => ex
    exception_handler('create', ex, {
        400 => "The required form params were missing to create the series!\n" +
               "    dubline_core:\n#{dublin_core.inspect}\n    acl:\n#{acl.inspect}",
        401 => "Unauthorized. It is required to authenticate first, before create a series!"
      }
    )
  end
  dc
end

#create_property(series_id, name, value) ⇒ Object

Create a new property for a given Series on Mattherhorn. Return no content.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/matterhorn/endpoint/series.rb', line 40

def create_property(series_id, name, value)
  return false    if value.nil? || value.to_s.empty?
  set = false
  begin
    split_response http_endpoint_client.post(
      "series/#{series_id}/property",
      { 'name' => name, 'value' => value.to_s }
    )
    set = true
  rescue => ex
    exception_handler('create_property', ex, {
        400 => "The required path or form params were missing in the request.",
        401 => "If the current user is not authorized to perform this action .",
        404 => "No series with this identifier was found."
      }
    )
  end
  set
end

#delete(series_id) ⇒ Object

————————————————————————————- delete —



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/matterhorn/endpoint/series.rb', line 196

def delete(series_id)
  deleted = false
  begin
    split_response http_endpoint_client.delete(
      "series/#{series_id}"
    )
    deleted = true
  rescue => ex
    exception_handler('delete', ex, {
        401 => "Unauthorized. It is required to authenticate first, " +
               "before delete series #{series_id}.",
        404 => "The series #{series_id} could not be found."
      }
    )
  end
  deleted
end

#delete_property(series_id, name) ⇒ Object



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/matterhorn/endpoint/series.rb', line 215

def delete_property(series_id, name)
  deleted = false
  begin
    split_response http_endpoint_client.delete(
      "series/#{series_id}/property/#{name}"
    )
    deleted = true
  rescue => ex
    exception_handler('delete_property', ex, {
        401 => "The current user is not authorized to perform this action.",
        404 => "The series #{series_id} or property #{name} has not been found."
      }
    )
  end
  deleted
end

#find(options) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/matterhorn/endpoint/series.rb', line 118

def find(options)
  dc_models = []
  begin
    split_response http_endpoint_client.get(
      "series/series.xml#{build_query_str(options)}"
    )
    Nokogiri::XML(response_body).
    xpath("/dublincorelist/xmlns:dublincore", Matterhorn::DublinCore::NS).each do |dc_elem|
      dc_models << Matterhorn::DublinCore.new(dc_elem.to_xml)
    end
  rescue => ex
    exception_handler('find', ex, {
        401 => "Unauthorized. It is required to authenticate first, " +
               "before filter series."
      }
    )
  end
  dc_models
end

#read(series_id) ⇒ Object

————————————————————————————— read —



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/matterhorn/endpoint/series.rb', line 63

def read(series_id)
  dc_model = nil
  begin
    split_response http_endpoint_client.get(
      "series/#{series_id}.xml"
    )
    dc_model = Matterhorn::DublinCore.new(response_body)
  rescue => ex
    exception_handler('read', ex, {
        401 => "Unauthorized. It is required to authenticate first, " +
               "before get the content of series #{series_id}.",
        403 => "It is forbidden to get the content of series #{series_id}.",
        404 => "The content of series #{series_id} could not be get."
      }
    )
  end
  dc_model
end

#read_acl(series_id) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/matterhorn/endpoint/series.rb', line 83

def read_acl(series_id)
  acl_model = nil
  begin
    split_response http_endpoint_client.get(
      "series/#{series_id}/acl.xml"
    )
    acl_model = Matterhorn::Acl.new(response_body)
  rescue => ex
    exception_handler('read_acl', ex, {
        404 => "The acl of series #{series_id} could not be found."
      }
    )
  end
  acl_model
end

#read_property(series_id, name) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/matterhorn/endpoint/series.rb', line 100

def read_property(series_id, name)
  prop_value = nil
  begin
    split_response http_endpoint_client.get(
      "series/#{series_id}/property/#{name}.json"
    )
    prop_value = response_body
  rescue => ex
    exception_handler('read_property', ex, {
        401 => "The current user is not authorized to perform this action.",
        404 => "The series #{series_id} or property #{name} has not been found."
      }
    )
  end
  prop_value
end

#update(dublin_core, acl = nil) ⇒ Object

Updates a given Series on Mattherhorn. In the property dcterms:identifier must be written the Matterhorn Series Id of the Series which should be updated. Return the dublin core of the updated Series.



160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/matterhorn/endpoint/series.rb', line 160

def update(dublin_core, acl = nil)
  series_id = dublin_core.dcterms_identifier
  dc_to_update = read(dublin_core.dcterms_identifier)
  if dc_to_update.nil?
    raise(Matterhorn::Error.new("Series[#{series_id}] was not found on Matterhorn"))
  end
  dc = create(dublin_core, acl)
  if response_code == 204
    dc = read(series_id)
  end
  dc
end

#update_acl(series_id, acl) ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/matterhorn/endpoint/series.rb', line 174

def update_acl(series_id, acl)
  acl_updated = false
  begin
    split_response http_endpoint_client.post(
      "series/#{series_id}/accesscontrol", { 'acl' => acl.to_xml }
    )
    acl_updated = true
  rescue => ex
    exception_handler('update_acl', ex, {
        400 => "Bad request. The required param acl was missing. acl: #{acl.inspect}",
        401 => "Unauthorized. It is required to authenticate first, " +
               "before update the acl of series #{series_id}.",
        404 => "The series #{series_id} could not be found."
      }
    )
  end
  acl_updated
end