amplify_syndication 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d2710bd59fa2d3c01b488bba30392f7bdf296741fbc698e8119c2a65c3ba5c33
4
- data.tar.gz: d61cecb2245118e3db17c27c973c4c9a32f37d7f65f159bc48973da0c32edda4
3
+ metadata.gz: 1e358033fc0c9fcb6425cd506509dfaf76347de8c1b83ec82fb36c294d2d3ebe
4
+ data.tar.gz: f3fbf71ab860d4aa2dcb4c6f5d07c0123d8879d5a7fa3f9f12c03373a73db4f8
5
5
  SHA512:
6
- metadata.gz: 707bb585b4afc0d5476d874464d477f2632d51df1d7a3bd76c56a6a0f1c863fa71cdc4e9b8fd74e8740040527beafe9a2e1a84ed028fd0148911c826fb747562
7
- data.tar.gz: 54e3c195ab9a89484722166f2a1146227c85d65e8c0e47ee4aa70c28b6c5f35edbfb20aaa58bcc31aadf9527f51f9d2be887e8ebb25bc33cfdf4489cec409de0
6
+ metadata.gz: 599a8ef6b78335a7c0cbda47af06e45e16343a211b3126e5e871691731ffd874a97244d813a027f3c4548ed69db401b340fe4e038b1c746564da64b1b61e1ea9
7
+ data.tar.gz: fe9c628b9d876fcb5e9894f30a547be623e619800ffabc451f1d101a07fbb6524ec24c3321a22bea053b9e84d030391f743f0ee5b35bfa38bc4ec96876efc108
data/.DS_Store ADDED
Binary file
@@ -0,0 +1,156 @@
1
+ module AmplifySyndication
2
+ class API
3
+ def initialize(client = Client.new)
4
+ @client = client
5
+ end
6
+
7
+ # Fetch metadata
8
+ def fetch_metadata
9
+ @client.get("$metadata?$format=json")
10
+ end
11
+
12
+ # Fetch basic property data
13
+ def fetch_property_data(limit = 1)
14
+ @client.get("Property", "$top" => limit)
15
+ end
16
+
17
+ # Fetch data with query options
18
+ def fetch_with_options(resource, query_options = {})
19
+ @client.get_with_options(resource, query_options)
20
+ end
21
+
22
+ # Fetch properties with specific filtering, sorting, and pagination
23
+ def fetch_filtered_properties(filter: nil, select: nil, orderby: nil, top: nil, skip: nil, count: nil)
24
+ query_options = {
25
+ "$filter" => filter,
26
+ "$select" => select,
27
+ "$orderby" => orderby,
28
+ "$top" => top,
29
+ "$skip" => skip,
30
+ "$count" => count
31
+ }.compact
32
+ fetch_with_options("Property", query_options)
33
+ end
34
+
35
+ # Fetch the total count of properties
36
+ def fetch_property_count
37
+ fetch_filtered_properties(count: "true", top: 0)
38
+ end
39
+
40
+ ### Replication Methods ###
41
+
42
+ # Perform initial download for replication
43
+ def perform_initial_download(
44
+ resource: "Property",
45
+ batch_size: 100,
46
+ fields: ["ModificationTimestamp", "ListingKey"],
47
+ filter: nil,
48
+ checkpoint: { last_timestamp: "1970-01-01T00:00:00Z", last_key: 0 }
49
+ )
50
+ puts "Starting initial download..."
51
+ all_records = [] # Array to collect all records
52
+
53
+ loop do
54
+ puts "Fetching batch with timestamp > #{checkpoint[:last_timestamp]} and key > #{checkpoint[:last_key]}..."
55
+
56
+ # Build batch filter
57
+ batch_filter = []
58
+ batch_filter << "#{filter}" if filter
59
+ batch_filter << "(ModificationTimestamp gt #{URI.encode_www_form_component(checkpoint[:last_timestamp])})"
60
+ batch_filter << "or (ModificationTimestamp eq #{URI.encode_www_form_component(checkpoint[:last_timestamp])} and ListingKey gt '#{checkpoint[:last_key]}')"
61
+ batch_filter = batch_filter.join(" ")
62
+
63
+ # Query options
64
+ query_options = {
65
+ "$select" => fields.join(","),
66
+ "$filter" => batch_filter,
67
+ "$orderby" => "ModificationTimestamp,ListingKey",
68
+ "$top" => batch_size
69
+ }
70
+
71
+ # Debugging: Print the full query options
72
+ puts "Query options: #{query_options.inspect}"
73
+
74
+ # Send request
75
+ response = fetch_with_options(resource, query_options)
76
+ records = response["value"]
77
+ break if records.empty?
78
+
79
+ # Collect batch records
80
+ all_records.concat(records)
81
+
82
+ # Update checkpoint with the last record in the batch
83
+ last_record = records.last
84
+ checkpoint[:last_timestamp] = last_record["ModificationTimestamp"]
85
+ checkpoint[:last_key] = last_record["ListingKey"]
86
+
87
+ # Stop if the number of records is less than the batch size
88
+ break if records.size < batch_size
89
+ end
90
+
91
+ puts "Initial download complete."
92
+ all_records # Return the collected records
93
+ end
94
+
95
+ # Fetch updates since the last checkpoint
96
+ def fetch_updates(
97
+ resource: "Property",
98
+ batch_size: 100,
99
+ fields: ["ModificationTimestamp", "ListingKey"],
100
+ filter: nil,
101
+ checkpoint: { last_timestamp: "1970-01-01T00:00:00Z", last_key: 0 }
102
+ )
103
+ perform_initial_download(
104
+ resource: resource,
105
+ batch_size: batch_size,
106
+ fields: fields,
107
+ filter: filter,
108
+ checkpoint: checkpoint
109
+ ) do |batch|
110
+ # Process updates
111
+ yield(batch) if block_given?
112
+ end
113
+ end
114
+
115
+ # Fetch full details of a property by ListingKey
116
+ def fetch_property_by_key(listing_key)
117
+ endpoint = "Property('#{listing_key}')"
118
+ puts "Fetching property details for ListingKey: #{listing_key}"
119
+ @client.get(endpoint)
120
+ end
121
+
122
+ ### Media Methods ###
123
+
124
+ # Fetch a media record by MediaKey
125
+ def fetch_media_by_key(media_key)
126
+ endpoint = "Media('#{media_key}')"
127
+ @client.get(endpoint)
128
+ end
129
+
130
+ # Fetch recently created/modified media records
131
+ def fetch_recent_media(
132
+ filter: "ImageSizeDescription eq 'Large' and ResourceName eq 'Property'",
133
+ modification_date: "2023-07-27T04:00:00Z",
134
+ orderby: "ModificationTimestamp,MediaKey",
135
+ batch_size: 100
136
+ )
137
+ query_options = {
138
+ "$filter" => "#{filter} and ModificationTimestamp ge #{modification_date}",
139
+ "$orderby" => orderby,
140
+ "$top" => batch_size
141
+ }
142
+ fetch_with_options("Media", query_options)
143
+ end
144
+
145
+ # Fetch media by ResourceName and ResourceRecordKey
146
+ def fetch_media_by_resource(resource_name, resource_key, batch_size = 100)
147
+ filter = "ResourceRecordKey eq '#{resource_key}' and ResourceName eq '#{resource_name}'"
148
+ query_options = {
149
+ "$filter" => filter,
150
+ "$orderby" => "ModificationTimestamp,MediaKey",
151
+ "$top" => batch_size
152
+ }
153
+ fetch_with_options("Media", query_options)
154
+ end
155
+ end
156
+ end
@@ -0,0 +1,44 @@
1
+ require "httpclient"
2
+ require "json"
3
+ require "uri"
4
+
5
+ module AmplifySyndication
6
+ class Client
7
+ def initialize
8
+ @http_client = HTTPClient.new
9
+ @base_url = AmplifySyndication.configuration.base_url
10
+ @access_token = AmplifySyndication.configuration.access_token
11
+ end
12
+
13
+ def get(endpoint, params = {})
14
+ url = "#{@base_url}/#{endpoint}".gsub(%r{//}, '/').sub(%r{:/}, '://')
15
+ headers = {
16
+ "Authorization" => "Bearer #{@access_token}",
17
+ "Accept" => "application/json"
18
+ }
19
+ response = @http_client.get(url, params, headers)
20
+ parse_response(response)
21
+ end
22
+
23
+ def get_with_options(endpoint, options = {})
24
+ query_string = options.map { |key, value| "#{key}=#{value}" }.join("&")
25
+ get("#{endpoint}?#{query_string}")
26
+ end
27
+
28
+ private
29
+
30
+ # Converts a hash of query options into a URL-encoded query string
31
+ def build_query_string(options)
32
+ URI.encode_www_form(options)
33
+ end
34
+
35
+ # Parses JSON response, raises an error for non-200 responses
36
+ def parse_response(response)
37
+ if response.status == 200
38
+ JSON.parse(response.body)
39
+ else
40
+ raise StandardError, "HTTP Error: #{response.status} - #{response.body}"
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,10 @@
1
+ module AmplifySyndication
2
+ class Configuration
3
+ attr_accessor :access_token, :base_url
4
+
5
+ def initialize
6
+ @access_token = nil
7
+ @base_url = "https://query.ampre.ca/odata"
8
+ end
9
+ end
10
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module AmplifySyndication
4
- VERSION = "0.1.0"
4
+ VERSION = "0.1.1"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: amplify_syndication
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stephen Higgins
@@ -32,11 +32,15 @@ executables: []
32
32
  extensions: []
33
33
  extra_rdoc_files: []
34
34
  files:
35
+ - ".DS_Store"
35
36
  - CHANGELOG.md
36
37
  - LICENSE.txt
37
38
  - README.md
38
39
  - Rakefile
39
40
  - lib/amplify_syndication.rb
41
+ - lib/amplify_syndication/api.rb
42
+ - lib/amplify_syndication/client.rb
43
+ - lib/amplify_syndication/configuration.rb
40
44
  - lib/amplify_syndication/version.rb
41
45
  - sig/amplify_syndication.rbs
42
46
  homepage: https://github.com/gryphonandrook/amplify_syndication