Module: Webhookdb::Replicator::SponsyV1Mixin

Includes:
DBAdapter::ColumnTypes
Included in:
SponsyCustomerV1, SponsyPlacementV1, SponsyPublicationV1, SponsySlotV1, SponsyStatusV1
Defined in:
lib/webhookdb/replicator/sponsy_v1_mixin.rb

Defined Under Namespace

Classes: PublicationChildBackfiller

Constant Summary

Constants included from DBAdapter::ColumnTypes

DBAdapter::ColumnTypes::BIGINT, DBAdapter::ColumnTypes::BIGINT_ARRAY, DBAdapter::ColumnTypes::BOOLEAN, DBAdapter::ColumnTypes::COLUMN_TYPES, DBAdapter::ColumnTypes::DATE, DBAdapter::ColumnTypes::DECIMAL, DBAdapter::ColumnTypes::DOUBLE, DBAdapter::ColumnTypes::FLOAT, DBAdapter::ColumnTypes::INTEGER, DBAdapter::ColumnTypes::INTEGER_ARRAY, DBAdapter::ColumnTypes::OBJECT, DBAdapter::ColumnTypes::TEXT, DBAdapter::ColumnTypes::TEXT_ARRAY, DBAdapter::ColumnTypes::TIMESTAMP, DBAdapter::ColumnTypes::UUID

Instance Method Summary collapse

Instance Method Details

#_parallel_backfillObject



78
# File 'lib/webhookdb/replicator/sponsy_v1_mixin.rb', line 78

def _parallel_backfill = Webhookdb::Sponsy.parallel_backfill

#_publication_backfillers(tail, publication_ids: nil, publication_slugs: nil) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/webhookdb/replicator/sponsy_v1_mixin.rb', line 115

def _publication_backfillers(tail, publication_ids: nil, publication_slugs: nil)
  raise Webhookdb::Replicator::CredentialsMissing, "This Sponsy integration is missing a dependency with auth" if
    self.find_api_key.blank?

  publications_svc = self.service_integration.depends_on.replicator
  backfillers = publications_svc.admin_dataset(timeout: :fast) do |pub_ds|
    pub_ds = Webhookdb::Dbutil.reduce_expr(
      pub_ds,
      :|,
      [publication_ids && Sequel[sponsy_id: publication_ids], publication_slugs && Sequel[slug: publication_slugs]],
    )
    pub_ds = pub_ds.where(deleted_at: nil)
    pub_ds.select(:sponsy_id).map do |publication|
      PublicationChildBackfiller.new(
        service: self,
        publication_id: publication.fetch(:sponsy_id),
        tail:,
      )
    end
  end
  return backfillers
end

#_remote_key_columnObject



8
9
10
# File 'lib/webhookdb/replicator/sponsy_v1_mixin.rb', line 8

def _remote_key_column
  return Webhookdb::Replicator::Column.new(:sponsy_id, TEXT, data_key: "id")
end

#_resource_and_event(request) ⇒ Object



48
49
50
# File 'lib/webhookdb/replicator/sponsy_v1_mixin.rb', line 48

def _resource_and_event(request)
  return request.body, nil
end

#_timestamp_column_nameObject



12
13
14
# File 'lib/webhookdb/replicator/sponsy_v1_mixin.rb', line 12

def _timestamp_column_name
  return :updated_at
end

#_ts_columnsObject



16
17
18
19
20
21
22
23
24
25
# File 'lib/webhookdb/replicator/sponsy_v1_mixin.rb', line 16

def _ts_columns
  return [
    Webhookdb::Replicator::Column.new(:created_at, TIMESTAMP, data_key: "createdAt"),
    Webhookdb::Replicator::Column.new(
      :updated_at, TIMESTAMP,
      data_key: "updatedAt",
      defaulter: Webhookdb::Replicator::Column.defaulter_from_resource_field(:created_at),
    ),
  ]
end

#_update_where_exprObject



52
53
54
# File 'lib/webhookdb/replicator/sponsy_v1_mixin.rb', line 52

def _update_where_expr
  return self.qualified_table_sequel_identifier[:updated_at] < Sequel[:excluded][:updated_at]
end

#_verify_backfill_err_msgObject



27
28
29
# File 'lib/webhookdb/replicator/sponsy_v1_mixin.rb', line 27

def _verify_backfill_err_msg
  return "Looks like your API key is invalid."
end

#_webhook_response(_request) ⇒ Object



56
57
58
59
# File 'lib/webhookdb/replicator/sponsy_v1_mixin.rb', line 56

def _webhook_response(_request)
  # There are no webhooks to respond to, these are backfill-only integrations
  return Webhookdb::WebhookResponse.ok
end

#api_urlObject



31
32
33
# File 'lib/webhookdb/replicator/sponsy_v1_mixin.rb', line 31

def api_url
  return "https://api.getsponsy.com"
end

#auth_headersObject



35
36
37
# File 'lib/webhookdb/replicator/sponsy_v1_mixin.rb', line 35

def auth_headers
  return {"X-Api-Key" => self.find_api_key}
end

#calculate_backfill_state_machineWebhookdb::Replicator::StateMachineStep



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/webhookdb/replicator/sponsy_v1_mixin.rb', line 62

def calculate_backfill_state_machine
  check_dep = self.class.descriptor.dependency_descriptor
  if check_dep && (step = self.calculate_dependency_state_machine_step(dependency_help: ""))
    return step
  end
  step = Webhookdb::Replicator::StateMachineStep.new
  step.output = %(We will start replicating #{self.resource_name_plural} into your WebhookDB database.

#{self._query_help_output(prefix: "Once data is available, you can query #{self.resource_name_plural}.")})
  return step.completed
end

#fetch_sponsy_page(tail, pagination_token, last_backfilled) ⇒ Object

Paginate from most recently updated. We paginate until either:

  • There are no more pages (the ‘after cursor’ is nil), or

  • the updated at timestamp predates the time we last backfilled, meaning we probably already saw this update.



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/webhookdb/replicator/sponsy_v1_mixin.rb', line 85

def fetch_sponsy_page(tail, pagination_token, last_backfilled)
  url = self.api_url + tail
  begin
    response = Webhookdb::Http.get(
      url,
      query: {
        limit: Webhookdb::Sponsy.page_size.to_s,
        afterCursor: pagination_token,
        orderBy: "updatedAt",
        orderDirection: "DESC",
      },
      headers: self.auth_headers,
      logger: self.logger,
      timeout: Webhookdb::Sponsy.http_timeout,
    )
  rescue Webhookdb::Http::Error => e
    raise e unless e.status == 404
    self.logger.warn("sponsy_404", error: e)
    return [], nil
  end

  data = response.parsed_response.fetch("data")
  after_cursor = response.parsed_response.fetch("cursor", {}).fetch("afterCursor", nil)
  return data, nil if after_cursor.nil?
  return [], nil if data.empty?
  last_updated = data.last.fetch("updatedAt")
  return data, nil if last_updated < (last_backfilled || Time.at(0))
  return data, after_cursor
end

#find_api_keyObject



44
45
46
# File 'lib/webhookdb/replicator/sponsy_v1_mixin.rb', line 44

def find_api_key
  return self.root_integration.backfill_secret
end

#on_dependency_webhook_upsert(_replicator, _payload) ⇒ Object



74
75
76
# File 'lib/webhookdb/replicator/sponsy_v1_mixin.rb', line 74

def on_dependency_webhook_upsert(_replicator, _payload, *)
  return
end

#root_integrationObject



39
40
41
42
# File 'lib/webhookdb/replicator/sponsy_v1_mixin.rb', line 39

def root_integration
  return @root_integration ||= Webhookdb::Replicator.find_at_root!(self.service_integration,
                                                                   service_name: "sponsy_publication_v1",)
end