Class: DiscourseSubscriptionClient::Subscriptions::UpdateResult

Inherits:
Object
  • Object
show all
Defined in:
lib/discourse_subscription_client/subscriptions/update_result.rb

Constant Summary collapse

SUBSCRIPTION_KEYS =
REQUIRED_SUBSCRIPTION_KEYS + OPTIONAL_SUBSCRIPTION_KEYS

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeUpdateResult

Returns a new instance of UpdateResult.



26
27
28
29
# File 'lib/discourse_subscription_client/subscriptions/update_result.rb', line 26

def initialize
  @errors = []
  @infos = []
end

Instance Attribute Details

#errored_suppliersObject (readonly)

Returns the value of attribute errored_suppliers.



22
23
24
# File 'lib/discourse_subscription_client/subscriptions/update_result.rb', line 22

def errored_suppliers
  @errored_suppliers
end

#errorsObject (readonly)

Returns the value of attribute errors.



22
23
24
# File 'lib/discourse_subscription_client/subscriptions/update_result.rb', line 22

def errors
  @errors
end

#infosObject (readonly)

Returns the value of attribute infos.



22
23
24
# File 'lib/discourse_subscription_client/subscriptions/update_result.rb', line 22

def infos
  @infos
end

Instance Method Details

#connection_error(supplier) ⇒ Object



89
90
91
# File 'lib/discourse_subscription_client/subscriptions/update_result.rb', line 89

def connection_error(supplier)
  error("supplier_connection", supplier)
end

#created_subscription(supplier, subscription_ids: nil) ⇒ Object



105
106
107
# File 'lib/discourse_subscription_client/subscriptions/update_result.rb', line 105

def created_subscription(supplier, subscription_ids: nil)
  info("created_subscription", supplier, subscription_ids: subscription_ids)
end

#error(key, supplier) ⇒ Object



132
133
134
135
# File 'lib/discourse_subscription_client/subscriptions/update_result.rb', line 132

def error(key, supplier)
  @errors << I18n.t("subscription_client.subscriptions.error.#{key}", supplier: supplier.name,
                                                                      supplier_url: supplier.url)
end

#failed_to_create_subscription(supplier, subscription_ids: nil) ⇒ Object



109
110
111
# File 'lib/discourse_subscription_client/subscriptions/update_result.rb', line 109

def failed_to_create_subscription(supplier, subscription_ids: nil)
  info("failed_to_create_subscription", supplier, subscription_ids: subscription_ids)
end

#format_resources_data(resources_data) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/discourse_subscription_client/subscriptions/update_result.rb', line 77

def format_resources_data(resources_data)
  return {} unless resources_data

  resources_data
    .compact
    .map(&:symbolize_keys)
    .select { |data| REQUIRED_RESOURCE_KEYS.all? { |key| data.key?(key) } }
    .each_with_object({}) do |data, result|
      result[data[:resource]] = data.slice(:access_key_id, :secret_access_key)
    end
end

#format_subscriptions_data(subscriptions_data) ⇒ Object



70
71
72
73
74
75
# File 'lib/discourse_subscription_client/subscriptions/update_result.rb', line 70

def format_subscriptions_data(subscriptions_data)
  subscriptions_data
    .map(&:symbolize_keys)
    .each { |data| data[:resource_id] = data[:resource] }
    .select { |data| REQUIRED_SUBSCRIPTION_KEYS.all? { |key| data.key?(key) } }
end

#info(key, supplier = nil, subscription_ids: nil, resource: nil) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/discourse_subscription_client/subscriptions/update_result.rb', line 113

def info(key, supplier = nil, subscription_ids: nil, resource: nil)
  attrs = {}

  if supplier
    attrs = {
      supplier: supplier.name,
      supplier_url: supplier.url,
      deep_interpolation: true,
      price: "",
      resource_name: ""
    }
  end

  attrs.merge!(subscription_ids) if subscription_ids.present?
  attrs[:resource] if resource.present?

  @infos << I18n.t("subscription_client.subscriptions.info.#{key}", **attrs)
end

#no_subscriptions(supplier) ⇒ Object



97
98
99
# File 'lib/discourse_subscription_client/subscriptions/update_result.rb', line 97

def no_subscriptions(supplier)
  info("no_subscriptions", supplier)
end

#no_suppliersObject



93
94
95
# File 'lib/discourse_subscription_client/subscriptions/update_result.rb', line 93

def no_suppliers
  info("no_suppliers")
end

#not_authorized(supplier) ⇒ Object



31
32
33
# File 'lib/discourse_subscription_client/subscriptions/update_result.rb', line 31

def not_authorized(supplier)
  error("not_authorized", supplier)
end

#retrieve_subscriptions(supplier, raw_data) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/discourse_subscription_client/subscriptions/update_result.rb', line 35

def retrieve_subscriptions(supplier, raw_data)
  subscriptions_data = raw_data[:subscriptions].compact

  unless subscriptions_data.present? && subscriptions_data.is_a?(Array)
    error("invalid_response", supplier)
    return []
  end

  subscriptions_data = format_subscriptions_data(subscriptions_data)
  resources_data = format_resources_data(raw_data[:resources])

  # we only care about subscriptions for resources on this instance
  resources = SubscriptionClientResource.where(
    supplier_id: supplier.id,
    name: subscriptions_data.map { |data| data[:resource] }
  )

  subscriptions_data.each_with_object([]) do |data, result|
    resource = resources.select { |r| r.name === data[:resource] }.first

    if resource.present?
      data[:resource_id] = resource.id
      result << OpenStruct.new(
        required: data.slice(*REQUIRED_SUBSCRIPTION_KEYS),
        create: data.slice(*SUBSCRIPTION_KEYS),
        subscription: nil,
        resource: resource,
        resource_data: resources_data[resource.name]
      )
    else
      info("no_resource", supplier, resource: data[:resource])
    end
  end
end

#updated_subscription(supplier, subscription_ids: nil) ⇒ Object



101
102
103
# File 'lib/discourse_subscription_client/subscriptions/update_result.rb', line 101

def updated_subscription(supplier, subscription_ids: nil)
  info("updated_subscription", supplier, subscription_ids: subscription_ids)
end