Class: OpenActive::Rpde::RpdeBody

Inherits:
BaseModel
  • Object
show all
Defined in:
lib/openactive/rpde/rpde_body.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Methods inherited from BaseModel

#assign_attributes, define_property, deserialize, #deserialize, deserialize_class, #initialize, #serialize, serialize, #set_property, #to_json

Methods included from Concerns::TypeChecker

#check_types, included

Methods included from Concerns::JsonLdSerializable

included, #to_h, #values

Constructor Details

This class inherits a constructor from OpenActive::BaseModel

Instance Attribute Details

#itemsObject

Returns the value of attribute items



6
7
8
# File 'lib/openactive/rpde/rpde_body.rb', line 6

def items
  @items
end

#licenseObject

Returns the value of attribute license



7
8
9
# File 'lib/openactive/rpde/rpde_body.rb', line 7

def license
  @license
end

#nextObject

Returns the value of attribute next



5
6
7
# File 'lib/openactive/rpde/rpde_body.rb', line 5

def next
  @next
end

Class Method Details

.create(feed_base_url:, modified: nil, id: nil, change_number: nil, items:) ⇒ Object

Create an RpdeBody object from a given feed base URL, an array of RpdeItem, and either a modified int, and id, or a change_number. Checks are performed to see if the provided “modified” and “id” attributes are valid.

Parameters:

  • opts (Hash)

    the options to create a message with.

Returns:

  • self

Raises:



29
30
31
32
33
34
35
36
37
# File 'lib/openactive/rpde/rpde_body.rb', line 29

def self.create(feed_base_url:, modified: nil, id: nil, change_number: nil, items:)
  if !modified.nil? && !id.nil? && change_number.nil?
    create_from_modified_id(feed_base_url, modified, id, items)
  elsif modified.nil? && id.nil? && !change_number.nil?
    create_from_next_change_number(feed_base_url, change_number, items)
  else
    raise ArgumentError
  end
end

.create_from_modified_id(feed_base_url, modified, id, items) ⇒ Object

Create an RpdeBody object from a given feed base URL, a modified int, an id, and an array of RpdeItem. Checks are performed to see if the provided “modified” and “id” attributes are valid.

Parameters:

Returns:

  • self

Raises:



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/openactive/rpde/rpde_body.rb', line 59

def self.create_from_modified_id(feed_base_url, modified, id, items)
  rpde_body = new(
    items: items,
  )
  if !items.empty?
    first_item = items.first

    # Checks that the afterId and afterTimestamp provided are not the
    # first item in the feed (helps detect whether query is correct)
    if first_item.modified.to_s == modified.to_s && first_item.id.to_s == id.to_s
      raise Exceptions::FirstTimeAfterTimestampAndAfterIdException
    end

    current_modified = -1
    current_id = first_item.id
    items.each do |item|
      if item.state == OpenActive::Rpde::RpdeState::DELETED && !item.data.nil?
        raise Exceptions::DeletedItemsDataException
      end

      if item.state.nil? || item.kind.nil? || item.modified.nil? || item.id.nil?
        raise Exceptions::IncompleteItemsDataException
      end

      unless item.modified > current_modified ||
             (item.modified == current_modified && item.id > current_id)
        raise Exceptions::ModifiedIdItemsOrderException
      end

      current_modified = item.modified
      current_id = item.id
    end
    # Create 'next' URL depending on whether there are items available
    rpde_body.next = "#{feed_base_url}?afterTimestamp=#{CGI.escape(items.last.modified.to_s)}&"\
                     "afterId=#{CGI.escape(items.last.id.to_s)}"
  elsif !modified.nil? && !id.nil?
    rpde_body.next = "#{feed_base_url}?afterTimestamp=#{CGI.escape(modified.to_s)}&"\
                     "afterId=#{CGI.escape(id.to_s)}"
  end
  rpde_body
end

.create_from_next_change_number(feed_base_url, change_number, items) ⇒ Object

Create an RpdeBody object from a given feed base URL, a change number int, and an array of RpdeItem. Checks are performed to see if the provided “changeNumber” attribute is valid.

Parameters:

Returns:

  • self

Raises:



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/openactive/rpde/rpde_body.rb', line 120

def self.create_from_next_change_number(feed_base_url, change_number, items)
  rpde_body = new(
    items: items,
  )
  if !items.empty?
    first_item = items.first

    # Checks that the afterId and afterTimestamp provided are not the
    # first item in the feed (helps detect whether query is correct)
    raise Exceptions::FirstTimeAfterChangeNumberException if first_item.modified == change_number

    current_change_number = -1
    items.each do |item|
      if item.state == OpenActive::Rpde::RpdeState::DELETED && !item.data.nil?
        raise Exceptions::DeletedItemsDataException
      end

      if item.state.nil? || item.kind.nil? || item.modified.nil? || item.id.nil?
        raise Exceptions::IncompleteItemsDataException
      end

      raise Exceptions::NextChangeNumbersItemsOrderException unless item.modified > current_change_number

      current_change_number = item.modified
    end
    # Create 'next' URL depending on whether there are items available
    rpde_body.next = "#{feed_base_url}?afterChangeNumber=#{CGI.escape(items.last.modified.to_s)}"
  else
    unless change_number.nil?
      rpde_body.next = "#{feed_base_url}?afterChangeNumber=#{CGI.escape(change_number.to_s)}"
    end
  end
  rpde_body
end