Class: AmplifySyndication::API

Inherits:
Object
  • Object
show all
Defined in:
lib/amplify_syndication/api.rb

Instance Method Summary collapse

Constructor Details

#initialize(client = Client.new) ⇒ API

Returns a new instance of API.



5
6
7
# File 'lib/amplify_syndication/api.rb', line 5

def initialize(client = Client.new)
  @client = client
end

Instance Method Details

#each_initial_download_batch(resource: "Property", batch_size: 100, fields: ["ModificationTimestamp", "ListingKey"], filter: nil, sleep_seconds: 10, checkpoint: { last_timestamp: "1970-01-01T00:00:00Z", last_key: 0 }) ⇒ Object

Iterate over replication batches, yielding [batch, checkpoint].

You can persist checkpoint per batch to resume later if something fails.



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/amplify_syndication/api.rb', line 199

def each_initial_download_batch(
  resource: "Property",
  batch_size: 100,
  fields: ["ModificationTimestamp", "ListingKey"],
  filter: nil,
  sleep_seconds: 10,
  checkpoint: { last_timestamp: "1970-01-01T00:00:00Z", last_key: 0 }
)
  loop do
    batch = fetch_initial_download_batch(
      resource: resource,
      batch_size: batch_size,
      fields: fields,
      filter: filter,
      checkpoint: checkpoint
    )

    break if batch.empty?

    yield(batch, checkpoint) if block_given?

    # Update checkpoint automatically based on the last record in the batch
    last_record = batch.last
    checkpoint[:last_timestamp] = last_record["ModificationTimestamp"]
    checkpoint[:last_key]       = last_record["ListingKey"]

    break if batch.size < batch_size

    sleep(sleep_seconds) if sleep_seconds.positive?
  end
end

#each_lookup_batch(batch_size: 50, sleep_seconds: 10, filter: nil) ⇒ Object

Iterate over Lookup records in batches, yielding each batch.

Example:

api.each_lookup_batch(batch_size: 100, filter: "LookupStatus eq 'Active'") do |batch|
  batch.each { |row| VowLookup.upsert_from_row(row) }
end


73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/amplify_syndication/api.rb', line 73

def each_lookup_batch(batch_size: 50, sleep_seconds: 10, filter: nil)
  skip = 0

  loop do
    batch = fetch_lookup_batch(skip: skip, top: batch_size, filter: filter)
    break if batch.empty?

    yield(batch) if block_given?

    skip += batch_size
    sleep(sleep_seconds) if sleep_seconds.positive?
  end
end

#fetch_all_lookups(batch_size: 50, sleep_seconds: 10, filter: nil) ⇒ Object

Fetch all Lookup rows into memory (simple usage).

For long-running syncs, prefer each_lookup_batch so your app can handle persistence/checkpointing per batch.



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/amplify_syndication/api.rb', line 91

def fetch_all_lookups(batch_size: 50, sleep_seconds: 10, filter: nil)
  results = []

  each_lookup_batch(batch_size: batch_size,
                    sleep_seconds: sleep_seconds,
                    filter: filter) do |batch|
    results.concat(batch)
  end

  results
end

#fetch_all_media_for_resource(resource_name, resource_key, batch_size: 100, sleep_seconds: 1) ⇒ Object



329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# File 'lib/amplify_syndication/api.rb', line 329

def fetch_all_media_for_resource(resource_name, resource_key, batch_size: 100, sleep_seconds: 1)
  filter = "(ResourceRecordKey eq '#{resource_key}' and ResourceName eq '#{resource_name}')"

  results = []
  skip = 0

  loop do
    query_options = {
      "$filter" => filter,
      "$orderby" => "ModificationTimestamp,MediaKey",
      "$top"     => batch_size,
      "$skip"    => skip
    }

    response = fetch_with_options("Media", query_options)
    batch = response["value"] || []
    break if batch.empty?

    results.concat(batch)

    break if batch.size < batch_size
    skip += batch_size
    sleep(sleep_seconds)
  end

  results
end

#fetch_filtered_properties(filter: nil, select: nil, orderby: nil, top: nil, skip: nil, count: nil) ⇒ Object

Fetch properties with specific filtering, sorting, and pagination



141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/amplify_syndication/api.rb', line 141

def fetch_filtered_properties(filter: nil, select: nil, orderby: nil, top: nil, skip: nil, count: nil)
  query_options = {
    "$filter" => filter,
    "$select" => select,
    "$orderby" => orderby,
    "$top" => top,
    "$skip" => skip,
    "$count" => count
  }.compact

  fetch_with_options("Property", query_options)
end

#fetch_initial_download_batch(resource: "Property", batch_size: 100, fields: ["ModificationTimestamp", "ListingKey"], filter: nil, checkpoint: { last_timestamp: "1970-01-01T00:00:00Z", last_key: 0 }) ⇒ Object

Build and fetch a single replication batch for a resource.



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/amplify_syndication/api.rb', line 168

def fetch_initial_download_batch(
  resource: "Property",
  batch_size: 100,
  fields: ["ModificationTimestamp", "ListingKey"],
  filter: nil,
  checkpoint: { last_timestamp: "1970-01-01T00:00:00Z", last_key: 0 }
)
  encoded_ts = URI.encode_www_form_component(checkpoint[:last_timestamp])

  # checkpoint filter: everything strictly after the last (timestamp, key) pair
  checkpoint_filter = "(ModificationTimestamp gt #{encoded_ts}) " \
                      "or (ModificationTimestamp eq #{encoded_ts} and ListingKey gt '#{checkpoint[:last_key]}')"

  conditions = []
  conditions << "(#{filter})" if filter
  conditions << "(#{checkpoint_filter})"

  query_options = {
    "$select" => fields.join(","),
    "$filter" => conditions.join(" and "),
    "$orderby" => "ModificationTimestamp,ListingKey",
    "$top" => batch_size
  }

  response = fetch_with_options(resource, query_options)
  response["value"] || []
end

#fetch_lookup_batch(skip:, top: 50, filter: nil) ⇒ Object

Fetch a single Lookup page (batch) with optional filter.

filter can be:

- a String: "LookupStatus eq 'Active'"
- an Array of strings: ["LookupStatus eq 'Active'", "LookupName eq 'PropertyType'"]


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/amplify_syndication/api.rb', line 51

def fetch_lookup_batch(skip:, top: 50, filter: nil)
  query_options = {
    "$top"  => top,
    "$skip" => skip
  }

  if filter
    combined_filter =
      filter.is_a?(Array) ? filter.join(" and ") : filter
    query_options["$filter"] = combined_filter
  end

  response = fetch_with_options("Lookup", query_options)
  response["value"] || []
end

#fetch_media_by_key(media_key) ⇒ Object

Fetch a media record by MediaKey



308
309
310
311
# File 'lib/amplify_syndication/api.rb', line 308

def fetch_media_by_key(media_key)
  endpoint = "Media('#{media_key}')"
  @client.get(endpoint)
end

#fetch_metadataObject

Fetch metadata



12
13
14
# File 'lib/amplify_syndication/api.rb', line 12

def 
  @client.get("$metadata?$format=json")
end

#fetch_property_by_key(listing_key) ⇒ Object

Fetch full details of a property by ListingKey



299
300
301
302
303
# File 'lib/amplify_syndication/api.rb', line 299

def fetch_property_by_key(listing_key)
  endpoint = "Property('#{listing_key}')"
  puts "Fetching property details for ListingKey: #{listing_key}"
  @client.get(endpoint)
end

#fetch_property_countObject

Fetch the total count of properties



155
156
157
# File 'lib/amplify_syndication/api.rb', line 155

def fetch_property_count
  fetch_filtered_properties(count: "true", top: 0)
end

#fetch_property_data(limit = 1) ⇒ Object

Fetch basic property data (simple test helper)



131
132
133
# File 'lib/amplify_syndication/api.rb', line 131

def fetch_property_data(limit = 1)
  @client.get("Property", "$top" => limit)
end

#fetch_property_fields(batch_size: 50, sleep_seconds: 10) ⇒ Object

Fetch all Field records for the Property resource in a single call (still paginated under the hood).



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/amplify_syndication/api.rb', line 20

def fetch_property_fields(batch_size: 50, sleep_seconds: 10)
  offset = 0
  fields = []

  loop do
    query_options = {
      "$filter" => "ResourceName eq 'Property'",
      "$top"    => batch_size,
      "$skip"   => offset
    }.compact

    response = fetch_with_options("Field", query_options)
    batch    = response["value"] || []
    break if batch.empty?

    fields.concat(batch)
    offset += batch_size

    sleep(sleep_seconds) if sleep_seconds.positive?
  end

  fields
end

#fetch_recent_media(filter: "ImageSizeDescription eq 'Large' and ResourceName eq 'Property'", modification_date: "2023-07-27T04:00:00Z", orderby: "ModificationTimestamp,MediaKey", batch_size: 100) ⇒ Object

Fetch recently created/modified media records



314
315
316
317
318
319
320
321
322
323
324
325
326
327
# File 'lib/amplify_syndication/api.rb', line 314

def fetch_recent_media(
  filter: "ImageSizeDescription eq 'Large' and ResourceName eq 'Property'",
  modification_date: "2023-07-27T04:00:00Z",
  orderby: "ModificationTimestamp,MediaKey",
  batch_size: 100
)
  query_options = {
    "$filter"  => "#{filter} and ModificationTimestamp ge #{modification_date}",
    "$orderby" => orderby,
    "$top"     => batch_size
  }

  fetch_with_options("Media", query_options)
end

#fetch_updates(resource: "Property", batch_size: 100, fields: ["ModificationTimestamp", "ListingKey"], filter: nil, checkpoint: { last_timestamp: "1970-01-01T00:00:00Z", last_key: 0 }, sleep_seconds: 10) ⇒ Object

Fetch updates since the last checkpoint.

If a block is given, yields each batch; otherwise returns all updates in a single array (same as perform_initial_download).



266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/amplify_syndication/api.rb', line 266

def fetch_updates(
  resource: "Property",
  batch_size: 100,
  fields: ["ModificationTimestamp", "ListingKey"],
  filter: nil,
  checkpoint: { last_timestamp: "1970-01-01T00:00:00Z", last_key: 0 },
  sleep_seconds: 10
)
  if block_given?
    each_initial_download_batch(
      resource: resource,
      batch_size: batch_size,
      fields: fields,
      filter: filter,
      sleep_seconds: sleep_seconds,
      checkpoint: checkpoint
    ) do |batch, cp|
      yield(batch, cp)
    end
    nil
  else
    perform_initial_download(
      resource: resource,
      batch_size: batch_size,
      fields: fields,
      filter: filter,
      sleep_seconds: sleep_seconds,
      checkpoint: checkpoint
    )
  end
end

#fetch_with_options(resource, query_options = {}) ⇒ Object

Fetch data with query options against an arbitrary resource



136
137
138
# File 'lib/amplify_syndication/api.rb', line 136

def fetch_with_options(resource, query_options = {})
  @client.get_with_options(resource, query_options)
end

#lookup(lookup_name, batch_size: 50, sleep_seconds: 10) ⇒ Object

Get all rows for a single LookupName (convenience helper).



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/amplify_syndication/api.rb', line 104

def lookup(lookup_name, batch_size: 50, sleep_seconds: 10)
  offset = 0
  values = []

  loop do
    query_options = {
      "$filter" => "LookupName eq '#{lookup_name}'",
      "$top"    => batch_size,
      "$skip"   => offset
    }.compact

    response = fetch_with_options("Lookup", query_options)
    batch    = response["value"] || []
    break if batch.empty?

    values.concat(batch)
    offset += batch_size

    sleep(sleep_seconds) if sleep_seconds.positive?
  end

  values
end

#perform_initial_download(resource: "Property", batch_size: 100, fields: ["ModificationTimestamp", "ListingKey"], filter: nil, sleep_seconds: 10, checkpoint: { last_timestamp: "1970-01-01T00:00:00Z", last_key: 0 }) ⇒ Object

Perform initial download for replication, buffering all results into memory (simple usage).

For large datasets, prefer each_initial_download_batch.



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/amplify_syndication/api.rb', line 235

def perform_initial_download(
  resource: "Property",
  batch_size: 100,
  fields: ["ModificationTimestamp", "ListingKey"],
  filter: nil,
  sleep_seconds: 10,
  checkpoint: { last_timestamp: "1970-01-01T00:00:00Z", last_key: 0 }
)
  results = []

  puts "Starting initial download..."

  each_initial_download_batch(
    resource: resource,
    batch_size: batch_size,
    fields: fields,
    filter: filter,
    sleep_seconds: sleep_seconds,
    checkpoint: checkpoint
  ) do |batch, _checkpoint|
    results.concat(batch)
  end

  puts "Initial download complete."
  results
end