class RSS::Connector

Utility class encapsulating synchronous communication with RSS.

Constants

BASIC_AUTH_CREDENTIALS
BASIC_AUTH_PASSWORD
BASIC_AUTH_USERNAME

Attributes

oid[R]
scope[R]

Public Class Methods

new(oid, scope) click to toggle source

@param oid [String] Organziation ID @param scope [String]

# File lib/rss/connector.rb, line 24
def initialize(oid, scope)
  @oid   = oid
  @scope = scope
end

Public Instance Methods

create_or_update_rule(rule_id, attributes = {}) click to toggle source

@raise [RuntimeError] RSS request failed

# File lib/rss/connector.rb, line 134
def create_or_update_rule(rule_id, attributes = {})
  path = "#{base_path}/rules/#{rule_id}"
  response = self.class.authenticated_put(
    path, query: prepare_rule_attributes(attributes)
  )

  case response.code
  when 200, 201 then JSON.parse(response.body)
  else fail "RSS: 'PUT #{path}' failed with status = #{response.code}."
  end
end
create_or_update_series(series_id, attributes = {}) click to toggle source

@raise [RuntimeError] RSS request failed

# File lib/rss/connector.rb, line 56
def create_or_update_series(series_id, attributes = {})
  path = "#{base_path}/series/#{series_id}"
  response = self.class.authenticated_put(
    path, query: prepare_series_attributes(attributes)
  )

  case response.code
  when 200, 201 then JSON.parse(response.body)
  else fail "RSS: 'PUT #{path}' failed with status = #{response.code}."
  end
end
create_rule(attributes = {}) click to toggle source

@raise [RuntimeError] RSS request failed

# File lib/rss/connector.rb, line 121
def create_rule(attributes = {})
  path = "#{base_path}/rules"
  response = self.class.authenticated_post(
    path, query: prepare_rule_attributes(attributes)
  )

  case response.code
  when 201 then JSON.parse(response.body)
  else fail "RSS: 'POST #{path}' failed with status = #{response.code}."
  end
end
create_series(attributes = {}) click to toggle source

@raise [RuntimeError] RSS request failed

# File lib/rss/connector.rb, line 43
def create_series(attributes = {})
  path = "#{base_path}/series"
  response = self.class.authenticated_post(
    path, query: prepare_series_attributes(attributes)
  )

  case response.code
  when 201 then JSON.parse(response.body)
  else fail "RSS: 'POST #{path}' failed with status = #{response.code}."
  end
end
delete_occurrence(occurrence_id) click to toggle source

@param occurrence_id [String] @raise [RuntimeError] RSS request failed

# File lib/rss/connector.rb, line 223
def delete_occurrence(occurrence_id)
  path = "#{base_path}/occurrences/#{occurrence_id}"
  response = self.class.authenticated_delete(path)

  case response.code
  when 200 then JSON.parse(response.body)
  when 404 then nil
  else fail "RSS: 'DELETE #{path}' failed with status = #{response.code}."
  end
end
delete_rule(rule_id) click to toggle source

@raise [RuntimeError] RSS request failed

# File lib/rss/connector.rb, line 161
def delete_rule(rule_id)
  path = "#{base_path}/rules/#{rule_id}"
  response = self.class.authenticated_delete(path)

  case response.code
  when 200 then JSON.parse(response.body)
  when 404 then nil
  else fail "RSS: 'DELETE #{path}' failed with status = #{response.code}."
  end
end
delete_series(series_id) click to toggle source

@raise [RuntimeError] RSS request failed

# File lib/rss/connector.rb, line 83
def delete_series(series_id)
  path = "#{base_path}/series/#{series_id}"
  response = self.class.authenticated_delete(path)

  case response.code
  when 200 then JSON.parse(response.body)
  when 404 then nil
  else fail "RSS: 'DELETE #{path}' failed with status = #{response.code}."
  end
end
get_occurrence(occurrence_id) click to toggle source

@param occurrence_id [String] @raise [RuntimeError] RSS request failed

# File lib/rss/connector.rb, line 190
def get_occurrence(occurrence_id)
  path = "#{base_path}/occurrences/#{occurrence_id}"
  response = self.class.authenticated_get(path)

  case response.code
  when 200
    occurrence_hash = JSON.parse(response.body)['occurrence']
    build_occurrence_payload(occurrence_hash)
  when 404 then nil
  else fail "RSS: 'GET #{path}' failed with status = #{response.code}."
  end
end
get_occurrences(filters = {}) click to toggle source

@raise [RuntimeError] RSS request failed

# File lib/rss/connector.rb, line 173
def get_occurrences(filters = {}) # rubocop:disable MethodLength
  path = "#{base_path}/occurrences"
  response = self.class.authenticated_get(
    path, query: sanitize_filters(filters)
  )

  case response.code
  when 200
    (JSON.parse(response.body)['occurrences'] || []).map do |h|
      build_occurrence_payload(h)
    end
  else fail "RSS: 'GET #{path}' failed with status = #{response.code}."
  end
end
get_rule(rule_id) click to toggle source

@raise [RuntimeError] RSS request failed

# File lib/rss/connector.rb, line 95
def get_rule(rule_id)
  path = "#{base_path}/rules/#{rule_id}"
  response = self.class.authenticated_get(path)

  case response.code
  when 200 then JSON.parse(response.body)
  when 404 then nil
  else fail "RSS: 'GET #{path}' failed with status = #{response.code}."
  end
end
get_rules(filters = {}) click to toggle source

@raise [RuntimeError] RSS request failed

# File lib/rss/connector.rb, line 107
def get_rules(filters = {})
  path = "#{base_path}/rules"
  response = self.class.authenticated_get(
    path, query: sanitize_filters(filters)
  )

  case response.code
  when 200 then JSON.parse(response.body)['rules']
  when 404 then nil
  else fail "RSS: 'GET #{path}' failed with status = #{response.code}."
  end
end
get_series(series_id) click to toggle source

@param series_id [String] @raise [RuntimeError] RSS request failed

# File lib/rss/connector.rb, line 31
def get_series(series_id)
  path = "#{base_path}/series/#{series_id}"
  response = self.class.authenticated_get(path)

  case response.code
  when 200 then JSON.parse(response.body)['series']
  when 404 then nil
  else fail "RSS: 'GET #{path}' failed with status = #{response.code}."
  end
end
update_occurrence(occurrence_id, attributes = {}) click to toggle source

@param occurrence_id [String] @raise [RuntimeError] RSS request failed rubocop:disable MethodLength

# File lib/rss/connector.rb, line 206
def update_occurrence(occurrence_id, attributes = {})
  path = "#{base_path}/occurrences/#{occurrence_id}"
  response = self.class.authenticated_patch(
    path, query: prepare_occurrence_attributes(attributes)
  )

  case response.code
  when 200
    occurrence_hash = JSON.parse(response.body)['occurrence']
    build_occurrence_payload(occurrence_hash)
  when 404 then nil
  else fail "RSS: 'PATCH #{path}' failed with status = #{response.code}."
  end
end
update_rule(rule_id, attributes = {}) click to toggle source

@raise [RuntimeError] RSS request failed

# File lib/rss/connector.rb, line 147
def update_rule(rule_id, attributes = {})
  path = "#{base_path}/rules/#{rule_id}"
  response = self.class.authenticated_patch(
    path, query: prepare_rule_attributes(attributes)
  )

  case response.code
  when 200 then JSON.parse(response.body)
  when 404 then nil
  else fail "RSS: 'PATCH #{path}' failed with status = #{response.code}."
  end
end
update_series(series_id, attributes = {}) click to toggle source

@raise [RuntimeError] RSS request failed

# File lib/rss/connector.rb, line 69
def update_series(series_id, attributes = {})
  path = "#{base_path}/series/#{series_id}"
  response = self.class.authenticated_patch(
    path, query: prepare_series_attributes(attributes)
  )

  case response.code
  when 200 then JSON.parse(response.body)
  when 404 then nil
  else fail "RSS: 'PATCH #{path}' failed with status = #{response.code}."
  end
end

Private Instance Methods

base_path() click to toggle source
# File lib/rss/connector.rb, line 245
def base_path
  "/v1/#{oid}/#{scope}"
end
build_occurrence_payload(occurrence_hash) click to toggle source

@param occurrence_hash [Hash]

# File lib/rss/connector.rb, line 308
def build_occurrence_payload(occurrence_hash)
  occurrence_hash.tap do |p|
    p['starts_at'] = Time.zone.parse(p['starts_at']) if p['starts_at']
    p['ends_at']   = Time.zone.parse(p['ends_at'])   if p['ends_at']
  end if occurrence_hash
end
iso8601_param(p) click to toggle source
# File lib/rss/connector.rb, line 303
def iso8601_param(p)
  p.respond_to?(:iso8601) ? p.iso8601 : p.to_s
end
prepare_occurrence_attributes(occurrence_attributes = {}) click to toggle source

@param occurrence_attributes [Hash] @return [Hash]

# File lib/rss/connector.rb, line 279
def prepare_occurrence_attributes(occurrence_attributes = {})
  {}.tap do |attrs|
    if occurrence_attributes[:starts_at].present?
      attrs[:starts_at] = iso8601_param(occurrence_attributes[:starts_at])
    end

    if occurrence_attributes[:ends_at].present?
      attrs[:ends_at] = iso8601_param(occurrence_attributes[:ends_at])
    end
  end
end
prepare_rule_attributes(rule_attributes = {}, series_attributes = nil) click to toggle source

@param rule_attributes [Hash] @return [Hash]

# File lib/rss/connector.rb, line 263
def prepare_rule_attributes(rule_attributes = {}, series_attributes = nil)
  {}.tap do |attrs|
    rule = Rule.new(rule_attributes)

    attrs[:id] = rule.id # core is always creating the rule id now
    attrs[:recurrence] = rule.recurrence

    if series_attributes
      attrs[:duration] = series_attributes[:duration] || rule.duration
      attrs[:time_zone] = series_attributes[:time_zone]
    end
  end
end
prepare_series_attributes(series_attributes = {}) click to toggle source

@param series_attributes [Hash] @return [Hash]

# File lib/rss/connector.rb, line 251
def prepare_series_attributes(series_attributes = {})
  (series_attributes || {}).tap do |attributes|
    attributes[:rules] ||= []

    attributes[:rules].map! do |rule_attributes|
      prepare_rule_attributes(rule_attributes, series_attributes)
    end
  end
end
sanitize_filters(filters = {}) click to toggle source
# File lib/rss/connector.rb, line 291
def sanitize_filters(filters = {})
  filters.tap do |f|
    if f[:interval_starts_at].present?
      f[:interval_starts_at] = iso8601_param(f[:interval_starts_at])
    end

    if f[:interval_ends_at].present?
      f[:interval_ends_at] = iso8601_param(f[:interval_ends_at])
    end
  end
end