Module: Webhookdb::Replicator::IncreaseV1Mixin
- Included in:
- IncreaseACHTransferV1, IncreaseAccountNumberV1, IncreaseAccountTransferV1, IncreaseAccountV1, IncreaseCheckTransferV1, IncreaseEventV1, IncreaseLimitV1, IncreaseTransactionV1, IncreaseWireTransferV1
- Defined in:
- lib/webhookdb/replicator/increase_v1_mixin.rb
Instance Method Summary collapse
- #_api_url ⇒ Object
- #_app_sint ⇒ Object
- #_auth_headers ⇒ Object
- #_fetch_backfill_page(pagination_token, **_kwargs) ⇒ Object
- #_fetch_enrichment(resource, _event, _request) ⇒ Object
- #_mixin_backfill_path ⇒ Object
- #_mixin_backfill_url ⇒ Object
- #_mixin_object_type ⇒ Object
- #_prepare_for_insert(resource, event, request, enrichment) ⇒ Object
- #_resource_and_event(request) ⇒ Object
- #_timestamp_column_name ⇒ Object
- #_update_where_expr ⇒ Object
- #_webhook_response(request) ⇒ Object
- #calculate_backfill_state_machine ⇒ Object
- #handle_event?(event) ⇒ Boolean
- #on_dependency_webhook_upsert(_replicator, payload) ⇒ Object
Instance Method Details
#_api_url ⇒ Object
29 |
# File 'lib/webhookdb/replicator/increase_v1_mixin.rb', line 29 def _api_url = "https://api.increase.com" |
#_app_sint ⇒ Object
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_headers ⇒ Object
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_path ⇒ Object
27 |
# File 'lib/webhookdb/replicator/increase_v1_mixin.rb', line 27 def _mixin_backfill_path = "/#{self._mixin_object_type}s" |
#_mixin_backfill_url ⇒ Object
28 |
# File 'lib/webhookdb/replicator/increase_v1_mixin.rb', line 28 def _mixin_backfill_url = "#{self._api_url}#{self._mixin_backfill_path}" |
#_mixin_object_type ⇒ Object
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_name ⇒ Object
12 13 14 15 |
# File 'lib/webhookdb/replicator/increase_v1_mixin.rb', line 12 def # We derive updated_at from the event, or use 'now' return :updated_at end |
#_update_where_expr ⇒ Object
17 18 19 20 |
# File 'lib/webhookdb/replicator/increase_v1_mixin.rb', line 17 def _update_where_expr ts = self. 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_machine ⇒ Object
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
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 |