Module: Webhookdb::Replicator::IncreaseV1Mixin

Instance Method Summary collapse

Instance Method Details

#_api_urlObject



29
# File 'lib/webhookdb/replicator/increase_v1_mixin.rb', line 29

def _api_url = "https://api.increase.com"

#_app_sintObject



59
# File 'lib/webhookdb/replicator/increase_v1_mixin.rb', line 59

def _app_sint = Webhookdb::Replicator.find_at_root!(self.service_integration, service_name: "increase_app_v1")

#_auth_headersObject



61
62
63
# File 'lib/webhookdb/replicator/increase_v1_mixin.rb', line 61

def _auth_headers
  return {"Authorization" => ("Bearer " + self._app_sint.backfill_key)}
end

#_fetch_backfill_page(pagination_token, **_kwargs) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/webhookdb/replicator/increase_v1_mixin.rb', line 79

def _fetch_backfill_page(pagination_token, **_kwargs)
  query = {}
  (query[:cursor] = pagination_token) if pagination_token.present?
  fetched_at = Time.now
  response = Webhookdb::Http.get(
    self._mixin_backfill_url,
    query,
    headers: self._auth_headers,
    logger: self.logger,
    timeout: Webhookdb::Increase.http_timeout,
  )
  data = response.parsed_response
  next_page_param = data.dig("response_metadata", "next_cursor")
  rows = data["data"]
  # In general, we want to use webhooks/events to keep rows updated.
  # But if we are backfilling, touch the 'updated at' timestamp to make sure
  # these rows get inserted.
  # It does mess up history, but we can't get that history to be accurate
  # in the case of a backfill anyway.
  rows.each { |r| r["updated_at"] = fetched_at }
  return rows, next_page_param
end

#_fetch_enrichment(resource, _event, _request) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/webhookdb/replicator/increase_v1_mixin.rb', line 33

def _fetch_enrichment(resource, _event, _request)
  # If the resource type isn't what we expect, it must be an event.
  # In that case, we need to fetch the resource from the API,
  # and replace the event body in prepare_for_insert.
  # The updated_at becomes the event's created_at,
  # which should be fine- it's better than setting updated_at to 'now'
  # since that will be confusing as it looks like a resource was recently updated.
  rtype = resource.fetch("type")
  return nil if rtype == self._mixin_object_type
  raise Webhookdb::InvalidPrecondition, "unexpected resource: #{resource}" unless
    rtype == "event" && resource.fetch("associated_object_type") == self._mixin_object_type
  response = Webhookdb::Http.get(
    self._mixin_backfill_url + "/#{resource.fetch('associated_object_id')}",
    {},
    headers: self._auth_headers,
    logger: self.logger,
    timeout: Webhookdb::Increase.http_timeout,
  )
  return response.parsed_response.merge("updated_at" => resource.fetch("created_at"))
end

#_mixin_backfill_pathObject



27
# File 'lib/webhookdb/replicator/increase_v1_mixin.rb', line 27

def _mixin_backfill_path = "/#{self._mixin_object_type}s"

#_mixin_backfill_urlObject



28
# File 'lib/webhookdb/replicator/increase_v1_mixin.rb', line 28

def _mixin_backfill_url = "#{self._api_url}#{self._mixin_backfill_path}"

#_mixin_object_typeObject

Raises:

  • (NotImplementedError)


26
# File 'lib/webhookdb/replicator/increase_v1_mixin.rb', line 26

def _mixin_object_type = raise NotImplementedError

#_prepare_for_insert(resource, event, request, enrichment) ⇒ Object



54
55
56
57
# File 'lib/webhookdb/replicator/increase_v1_mixin.rb', line 54

def _prepare_for_insert(resource, event, request, enrichment)
  resource = enrichment if enrichment
  return super(resource, event, request, nil)
end

#_resource_and_event(request) ⇒ Object



10
# File 'lib/webhookdb/replicator/increase_v1_mixin.rb', line 10

def _resource_and_event(request) = request.body

#_timestamp_column_nameObject



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

def _timestamp_column_name
  # We derive updated_at from the event, or use 'now'
  return :updated_at
end

#_update_where_exprObject



17
18
19
20
# File 'lib/webhookdb/replicator/increase_v1_mixin.rb', line 17

def _update_where_expr
  ts = self._timestamp_column_name
  return self.qualified_table_sequel_identifier[ts] < Sequel[:excluded][ts]
end

#_webhook_response(request) ⇒ Object



6
7
8
# File 'lib/webhookdb/replicator/increase_v1_mixin.rb', line 6

def _webhook_response(request)
  return Webhookdb::Increase.webhook_response(request, self.service_integration.webhook_secret)
end

#calculate_backfill_state_machineObject



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/webhookdb/replicator/increase_v1_mixin.rb', line 65

def calculate_backfill_state_machine
  if (step = self.calculate_dependency_state_machine_step(dependency_help: ""))
    step.output = %(This replicator is managed automatically using OAuth through Increase.
Head over to #{Webhookdb::Replicator::IncreaseAppV1.descriptor.install_url} to learn more.)
    return step
  end
  step = Webhookdb::Replicator::StateMachineStep.new
  step.needs_input = false
  step.output = %(Great! We are going to start backfilling your #{self.resource_name_plural}.
#{self._query_help_output})
  step.complete = true
  return step
end

#handle_event?(event) ⇒ Boolean

Returns:

  • (Boolean)


31
# File 'lib/webhookdb/replicator/increase_v1_mixin.rb', line 31

def handle_event?(event) = event.fetch("associated_object_type") == self._mixin_object_type

#on_dependency_webhook_upsert(_replicator, payload) ⇒ Object



22
23
24
# File 'lib/webhookdb/replicator/increase_v1_mixin.rb', line 22

def on_dependency_webhook_upsert(_replicator, payload, **)
  self.upsert_webhook_body(payload)
end