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.



3
4
5
# File 'lib/amplify_syndication/api.rb', line 3

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

Instance Method Details

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

Fetch properties with specific filtering, sorting, and pagination



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/amplify_syndication/api.rb', line 23

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_media_by_key(media_key) ⇒ Object

Fetch a media record by MediaKey



125
126
127
128
# File 'lib/amplify_syndication/api.rb', line 125

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

#fetch_media_by_resource(resource_name, resource_key, batch_size = 100) ⇒ Object

Fetch media by ResourceName and ResourceRecordKey



146
147
148
149
150
151
152
153
154
# File 'lib/amplify_syndication/api.rb', line 146

def fetch_media_by_resource(resource_name, resource_key, batch_size = 100)
  filter = "ResourceRecordKey eq '#{resource_key}' and ResourceName eq '#{resource_name}'"
  query_options = {
    "$filter" => filter,
    "$orderby" => "ModificationTimestamp,MediaKey",
    "$top" => batch_size
  }
  fetch_with_options("Media", query_options)
end

#fetch_metadataObject

Fetch metadata



8
9
10
# File 'lib/amplify_syndication/api.rb', line 8

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

#fetch_property_by_key(listing_key) ⇒ Object

Fetch full details of a property by ListingKey



116
117
118
119
120
# File 'lib/amplify_syndication/api.rb', line 116

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



36
37
38
# File 'lib/amplify_syndication/api.rb', line 36

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

#fetch_property_data(limit = 1) ⇒ Object

Fetch basic property data



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

def fetch_property_data(limit = 1)
  @client.get("Property", "$top" => limit)
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



131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/amplify_syndication/api.rb', line 131

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 }) ⇒ Object

Fetch updates since the last checkpoint



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/amplify_syndication/api.rb', line 96

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

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

Fetch data with query options



18
19
20
# File 'lib/amplify_syndication/api.rb', line 18

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

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

Perform initial download for replication



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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/amplify_syndication/api.rb', line 43

def perform_initial_download(
  resource: "Property",
  batch_size: 100,
  fields: ["ModificationTimestamp", "ListingKey"],
  filter: nil,
  checkpoint: { last_timestamp: "1970-01-01T00:00:00Z", last_key: 0 }
)
  puts "Starting initial download..."
  all_records = [] # Array to collect all records

  loop do
    puts "Fetching batch with timestamp > #{checkpoint[:last_timestamp]} and key > #{checkpoint[:last_key]}..."

    # Build batch filter
    batch_filter = []
    batch_filter << "#{filter}" if filter
    batch_filter << "(ModificationTimestamp gt #{URI.encode_www_form_component(checkpoint[:last_timestamp])})"
    batch_filter << "or (ModificationTimestamp eq #{URI.encode_www_form_component(checkpoint[:last_timestamp])} and ListingKey gt '#{checkpoint[:last_key]}')"
    batch_filter = batch_filter.join(" ")

    # Query options
    query_options = {
      "$select" => fields.join(","),
      "$filter" => batch_filter,
      "$orderby" => "ModificationTimestamp,ListingKey",
      "$top" => batch_size
    }

    # Debugging: Print the full query options
    puts "Query options: #{query_options.inspect}"

    # Send request
    response = fetch_with_options(resource, query_options)
    records = response["value"]
    break if records.empty?

    # Collect batch records
    all_records.concat(records)

    # Update checkpoint with the last record in the batch
    last_record = records.last
    checkpoint[:last_timestamp] = last_record["ModificationTimestamp"]
    checkpoint[:last_key] = last_record["ListingKey"]

    # Stop if the number of records is less than the batch size
    break if records.size < batch_size
  end

  puts "Initial download complete."
  all_records # Return the collected records
end