instagram_connect 0.2.1 → 0.3.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.
Files changed (67) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ci.yml +36 -0
  3. data/CHANGELOG.md +102 -0
  4. data/Gemfile +4 -0
  5. data/README.md +328 -73
  6. data/app/controllers/instagram_connect/comments_controller.rb +1 -1
  7. data/app/controllers/instagram_connect/posts_controller.rb +1 -1
  8. data/app/jobs/instagram_connect/account_readiness_job.rb +150 -0
  9. data/app/jobs/instagram_connect/fetch_media_job.rb +110 -0
  10. data/app/jobs/instagram_connect/profile_sync_job.rb +78 -0
  11. data/app/jobs/instagram_connect/replay_events_job.rb +57 -0
  12. data/app/jobs/instagram_connect/send_message_job.rb +44 -16
  13. data/app/models/instagram_connect/account.rb +27 -1
  14. data/app/models/instagram_connect/api_budget.rb +22 -0
  15. data/app/models/instagram_connect/conversation.rb +99 -11
  16. data/app/models/instagram_connect/inbound_message.rb +11 -3
  17. data/app/models/instagram_connect/insight_snapshot.rb +43 -0
  18. data/app/models/instagram_connect/media_item.rb +38 -0
  19. data/app/models/instagram_connect/mention.rb +37 -0
  20. data/app/models/instagram_connect/message.rb +34 -2
  21. data/app/models/instagram_connect/message_attachment.rb +53 -0
  22. data/app/models/instagram_connect/message_reaction.rb +33 -0
  23. data/app/models/instagram_connect/webhook_event.rb +67 -0
  24. data/db/migrate/20260725194732_create_instagram_connect_webhook_events.rb +52 -0
  25. data/db/migrate/20260725200216_add_messaging_event_fields.rb +89 -0
  26. data/db/migrate/20260725200250_create_instagram_connect_message_reactions.rb +30 -0
  27. data/db/migrate/20260725201320_create_instagram_connect_message_attachments.rb +33 -0
  28. data/db/migrate/20260725202616_add_outbound_send_fields.rb +27 -0
  29. data/db/migrate/20260725203213_create_instagram_connect_media.rb +30 -0
  30. data/db/migrate/20260725203214_create_instagram_connect_mentions.rb +30 -0
  31. data/db/migrate/20260725203215_create_instagram_connect_insight_snapshots.rb +45 -0
  32. data/db/migrate/20260725203306_add_comment_moderation_fields.rb +29 -0
  33. data/db/migrate/20260725204022_add_per_account_token_fields.rb +76 -0
  34. data/db/migrate/20260725204815_create_instagram_connect_api_budgets.rb +29 -0
  35. data/db/migrate/20260725205417_add_conversation_profile_fields.rb +21 -0
  36. data/docs/CONFIGURATION.md +107 -0
  37. data/lib/instagram_connect/client.rb +129 -3
  38. data/lib/instagram_connect/configuration.rb +10 -0
  39. data/lib/instagram_connect/connect.rb +27 -4
  40. data/lib/instagram_connect/engine.rb +1 -0
  41. data/lib/instagram_connect/ingest/dispatcher.rb +151 -0
  42. data/lib/instagram_connect/ingest/envelope.rb +31 -0
  43. data/lib/instagram_connect/ingest/handlers/base.rb +55 -0
  44. data/lib/instagram_connect/ingest/handlers/comments.rb +59 -0
  45. data/lib/instagram_connect/ingest/handlers/mentions.rb +48 -0
  46. data/lib/instagram_connect/ingest/handlers/message_reactions.rb +91 -0
  47. data/lib/instagram_connect/ingest/handlers/messages.rb +135 -0
  48. data/lib/instagram_connect/ingest/handlers/messaging_handover.rb +53 -0
  49. data/lib/instagram_connect/ingest/handlers/messaging_optins.rb +37 -0
  50. data/lib/instagram_connect/ingest/handlers/messaging_postbacks.rb +24 -0
  51. data/lib/instagram_connect/ingest/handlers/messaging_referral.rb +42 -0
  52. data/lib/instagram_connect/ingest/handlers/messaging_seen.rb +37 -0
  53. data/lib/instagram_connect/ingest/handlers/story_insights.rb +62 -0
  54. data/lib/instagram_connect/ingest/handlers/unknown.rb +19 -0
  55. data/lib/instagram_connect/ingest/registry.rb +93 -0
  56. data/lib/instagram_connect/ingest/summary.rb +75 -0
  57. data/lib/instagram_connect/ingest.rb +27 -105
  58. data/lib/instagram_connect/media/attaching.rb +57 -0
  59. data/lib/instagram_connect/media/limits.rb +46 -0
  60. data/lib/instagram_connect/rate_limiter.rb +88 -0
  61. data/lib/instagram_connect/result.rb +26 -6
  62. data/lib/instagram_connect/text_splitter.rb +51 -0
  63. data/lib/instagram_connect/usage.rb +68 -0
  64. data/lib/instagram_connect/version.rb +1 -1
  65. data/lib/instagram_connect.rb +23 -0
  66. metadata +60 -2
  67. data/MIT-LICENSE +0 -20
@@ -3,15 +3,17 @@ module InstagramConnect
3
3
  # API-level failures — callers branch on +success?+ and read +error_code+ /
4
4
  # +error_message+. Transport/programming errors still raise.
5
5
  class Result
6
- attr_reader :success, :id, :error_code, :error_message, :retry_after, :data
6
+ attr_reader :success, :id, :error_code, :error_message, :retry_after, :data, :usage
7
7
 
8
- def initialize(success:, id: nil, error_code: nil, error_message: nil, retry_after: nil, data: {})
8
+ def initialize(success:, id: nil, error_code: nil, error_message: nil, retry_after: nil,
9
+ data: {}, usage: nil)
9
10
  @success = success
10
11
  @id = id
11
12
  @error_code = error_code
12
13
  @error_message = error_message
13
14
  @retry_after = retry_after
14
15
  @data = data || {}
16
+ @usage = usage
15
17
  end
16
18
 
17
19
  def success?
@@ -22,13 +24,31 @@ module InstagramConnect
22
24
  !success?
23
25
  end
24
26
 
27
+ # Meta's cursor for the next page, or nil when this is the last one. Reading
28
+ # `paging.next` rather than incrementing an offset is the only correct way
29
+ # through a Graph collection — results shift under you between calls.
30
+ def next_cursor
31
+ data.dig("paging", "cursors", "after").presence if more?
32
+ end
33
+
34
+ def more?
35
+ data.is_a?(Hash) && data.dig("paging", "next").present?
36
+ end
37
+
38
+ # The rows of a collection response, or [] for a single-object one.
39
+ def records
40
+ list = data["data"]
41
+ list.is_a?(Array) ? list : []
42
+ end
43
+
25
44
  # Convenience builders so call sites read cleanly.
26
- def self.ok(id: nil, data: {})
27
- new(success: true, id: id, data: data)
45
+ def self.ok(id: nil, data: {}, usage: nil)
46
+ new(success: true, id: id, data: data, usage: usage)
28
47
  end
29
48
 
30
- def self.error(message, error_code: nil, retry_after: nil, data: {})
31
- new(success: false, error_message: message, error_code: error_code, retry_after: retry_after, data: data)
49
+ def self.error(message, error_code: nil, retry_after: nil, data: {}, usage: nil)
50
+ new(success: false, error_message: message, error_code: error_code,
51
+ retry_after: retry_after, data: data, usage: usage)
32
52
  end
33
53
  end
34
54
  end
@@ -0,0 +1,51 @@
1
+ module InstagramConnect
2
+ # Splits a reply that exceeds Meta's 1000-byte message ceiling into parts that
3
+ # fit.
4
+ #
5
+ # Splitting rather than truncating is deliberate: a customer reading half an
6
+ # answer is worse than reading two bubbles, and the operator who wrote it has
7
+ # no way to know it was cut.
8
+ module TextSplitter
9
+ module_function
10
+
11
+ # Returns the parts to send, in order. A message already within the limit
12
+ # comes back as a single-element array, so callers have one code path.
13
+ def split(text, limit: Media::Limits::TEXT_MAX_BYTES)
14
+ text = text.to_s
15
+ return [ text ] if text.bytesize <= limit
16
+
17
+ parts = []
18
+ remaining = text
19
+
20
+ while remaining.bytesize > limit
21
+ chunk = take(remaining, limit)
22
+ parts << chunk.rstrip
23
+ remaining = remaining[chunk.length..].to_s.lstrip
24
+ end
25
+
26
+ # Text that divides exactly on a break leaves nothing over; appending an
27
+ # empty part would send an empty bubble.
28
+ parts << remaining unless remaining.empty?
29
+ parts
30
+ end
31
+
32
+ # The longest prefix that fits, broken at whitespace where possible so words
33
+ # survive. Falls back to a hard character break for input with no
34
+ # whitespace at all — a long URL, or a script that does not use spaces.
35
+ def take(text, limit)
36
+ candidate = +""
37
+ last_break = nil
38
+
39
+ text.each_char do |char|
40
+ break if candidate.bytesize + char.bytesize > limit
41
+
42
+ candidate << char
43
+ last_break = candidate.length if char.match?(/\s/)
44
+ end
45
+
46
+ return candidate if last_break.nil?
47
+
48
+ candidate[0, last_break]
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,68 @@
1
+ require "json"
2
+
3
+ module InstagramConnect
4
+ # Meta's own account of how much of the rate budget a call just consumed,
5
+ # parsed from the response headers.
6
+ #
7
+ # This is worth having because the documented formula — 4800 calls per 24
8
+ # hours multiplied by the account's impressions — depends on a number the
9
+ # messaging API never returns. Any budget computed locally is a guess. These
10
+ # headers are Meta telling you the answer directly, so they are treated as
11
+ # ground truth and the arithmetic only as a fallback.
12
+ class Usage
13
+ HEADERS = %w[x-business-use-case-usage x-app-usage].freeze
14
+
15
+ # Back off well before the wall: at 100% Meta stops answering, and the
16
+ # penalty is measured in hours.
17
+ WARN_AT = 80
18
+ CRITICAL_AT = 95
19
+
20
+ attr_reader :call_count, :total_cputime, :total_time, :estimated_time_to_regain_access
21
+
22
+ def initialize(call_count: 0, total_cputime: 0, total_time: 0, estimated_time_to_regain_access: nil)
23
+ @call_count = call_count.to_i
24
+ @total_cputime = total_cputime.to_i
25
+ @total_time = total_time.to_i
26
+ @estimated_time_to_regain_access = estimated_time_to_regain_access
27
+ end
28
+
29
+ # Returns nil when Meta sent no usage headers, which is normal for some
30
+ # endpoints — absence is not zero usage and must not be read as headroom.
31
+ def self.from_headers(headers)
32
+ raw = HEADERS.filter_map { |name| headers&.[](name) }.first
33
+ return nil if raw.blank?
34
+
35
+ payload = JSON.parse(raw)
36
+ # The business header is keyed by object id and holds an array; the app
37
+ # header is a flat hash.
38
+ metrics = payload.is_a?(Hash) && payload.values.first.is_a?(Array) ? payload.values.first.first : payload
39
+ return nil unless metrics.is_a?(Hash)
40
+
41
+ new(
42
+ call_count: metrics["call_count"],
43
+ total_cputime: metrics["total_cputime"],
44
+ total_time: metrics["total_time"],
45
+ estimated_time_to_regain_access: metrics["estimated_time_to_regain_access"]
46
+ )
47
+ rescue JSON::ParserError
48
+ nil
49
+ end
50
+
51
+ # The worst of the three, since exhausting any one of them throttles the app.
52
+ def percentage
53
+ [ call_count, total_cputime, total_time ].max
54
+ end
55
+
56
+ def warning?
57
+ percentage >= WARN_AT
58
+ end
59
+
60
+ def critical?
61
+ percentage >= CRITICAL_AT
62
+ end
63
+
64
+ def retry_after
65
+ estimated_time_to_regain_access.to_i * 60 if estimated_time_to_regain_access.to_i.positive?
66
+ end
67
+ end
68
+ end
@@ -1,3 +1,3 @@
1
1
  module InstagramConnect
2
- VERSION = "0.2.1"
2
+ VERSION = "0.3.1"
3
3
  end
@@ -1,6 +1,7 @@
1
1
  require "logger"
2
2
  require_relative "instagram_connect/version"
3
3
  require_relative "instagram_connect/errors"
4
+ require_relative "instagram_connect/usage"
4
5
  require_relative "instagram_connect/result"
5
6
  require_relative "instagram_connect/configuration"
6
7
  require_relative "instagram_connect/auth/strategy"
@@ -10,8 +11,12 @@ require_relative "instagram_connect/auth"
10
11
  require_relative "instagram_connect/client"
11
12
  require_relative "instagram_connect/connect"
12
13
  require_relative "instagram_connect/signature_verifier"
14
+ require_relative "instagram_connect/media/limits"
15
+ require_relative "instagram_connect/media/attaching"
16
+ require_relative "instagram_connect/text_splitter"
13
17
  require_relative "instagram_connect/messaging_window"
14
18
  require_relative "instagram_connect/ingest"
19
+ require_relative "instagram_connect/rate_limiter"
15
20
  require_relative "instagram_connect/doctor"
16
21
 
17
22
  # InstagramConnect connects a Rails app to Instagram over the official Meta
@@ -25,6 +30,24 @@ require_relative "instagram_connect/doctor"
25
30
  # c.verify_token = Rails.application.credentials.dig(:instagram_connect, :verify_token)
26
31
  # end
27
32
  module InstagramConnect
33
+ # Every table the gem's migrations own, in dependency order (parents first).
34
+ # Hosts use it to audit what the engine added to their schema; the suite uses
35
+ # it to assert the migrations actually built what the models expect.
36
+ TABLES = %w[
37
+ instagram_connect_accounts
38
+ instagram_connect_conversations
39
+ instagram_connect_messages
40
+ instagram_connect_inbound_messages
41
+ instagram_connect_comments
42
+ instagram_connect_webhook_events
43
+ instagram_connect_message_reactions
44
+ instagram_connect_message_attachments
45
+ instagram_connect_media
46
+ instagram_connect_mentions
47
+ instagram_connect_insight_snapshots
48
+ instagram_connect_api_budgets
49
+ ].freeze
50
+
28
51
  class << self
29
52
  def configuration
30
53
  @configuration ||= Configuration.new
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: instagram_connect
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kshitiz Sinha
@@ -51,6 +51,20 @@ dependencies:
51
51
  - - ">="
52
52
  - !ruby/object:Gem::Version
53
53
  version: '1.0'
54
+ - !ruby/object:Gem::Dependency
55
+ name: marcel
56
+ requirement: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '1.0'
61
+ type: :runtime
62
+ prerelease: false
63
+ version_requirements: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '1.0'
54
68
  description: 'A mountable Rails engine that connects your app to Instagram: receive
55
69
  and reply to DMs and comments in real time over HMAC-verified webhooks, publish
56
70
  posts, and manage OAuth tokens — using the official Instagram Graph API (Instagram-Login
@@ -67,7 +81,6 @@ files:
67
81
  - CHANGELOG.md
68
82
  - Gemfile
69
83
  - LICENSE.txt
70
- - MIT-LICENSE
71
84
  - README.md
72
85
  - Rakefile
73
86
  - app/assets/stylesheets/instagram_connect/application.css
@@ -79,16 +92,27 @@ files:
79
92
  - app/controllers/instagram_connect/posts_controller.rb
80
93
  - app/controllers/instagram_connect/webhooks_controller.rb
81
94
  - app/helpers/instagram_connect/application_helper.rb
95
+ - app/jobs/instagram_connect/account_readiness_job.rb
82
96
  - app/jobs/instagram_connect/application_job.rb
97
+ - app/jobs/instagram_connect/fetch_media_job.rb
83
98
  - app/jobs/instagram_connect/ingest_job.rb
99
+ - app/jobs/instagram_connect/profile_sync_job.rb
84
100
  - app/jobs/instagram_connect/refresh_tokens_job.rb
101
+ - app/jobs/instagram_connect/replay_events_job.rb
85
102
  - app/jobs/instagram_connect/send_message_job.rb
86
103
  - app/models/instagram_connect/account.rb
104
+ - app/models/instagram_connect/api_budget.rb
87
105
  - app/models/instagram_connect/application_record.rb
88
106
  - app/models/instagram_connect/comment.rb
89
107
  - app/models/instagram_connect/conversation.rb
90
108
  - app/models/instagram_connect/inbound_message.rb
109
+ - app/models/instagram_connect/insight_snapshot.rb
110
+ - app/models/instagram_connect/media_item.rb
111
+ - app/models/instagram_connect/mention.rb
91
112
  - app/models/instagram_connect/message.rb
113
+ - app/models/instagram_connect/message_attachment.rb
114
+ - app/models/instagram_connect/message_reaction.rb
115
+ - app/models/instagram_connect/webhook_event.rb
92
116
  - app/views/instagram_connect/comments/_comment.html.erb
93
117
  - app/views/instagram_connect/comments/index.html.erb
94
118
  - app/views/instagram_connect/conversations/_composer.html.erb
@@ -106,6 +130,19 @@ files:
106
130
  - db/migrate/20260715072550_create_instagram_connect_messages.rb
107
131
  - db/migrate/20260715072551_create_instagram_connect_inbound_messages.rb
108
132
  - db/migrate/20260715072552_create_instagram_connect_comments.rb
133
+ - db/migrate/20260725194732_create_instagram_connect_webhook_events.rb
134
+ - db/migrate/20260725200216_add_messaging_event_fields.rb
135
+ - db/migrate/20260725200250_create_instagram_connect_message_reactions.rb
136
+ - db/migrate/20260725201320_create_instagram_connect_message_attachments.rb
137
+ - db/migrate/20260725202616_add_outbound_send_fields.rb
138
+ - db/migrate/20260725203213_create_instagram_connect_media.rb
139
+ - db/migrate/20260725203214_create_instagram_connect_mentions.rb
140
+ - db/migrate/20260725203215_create_instagram_connect_insight_snapshots.rb
141
+ - db/migrate/20260725203306_add_comment_moderation_fields.rb
142
+ - db/migrate/20260725204022_add_per_account_token_fields.rb
143
+ - db/migrate/20260725204815_create_instagram_connect_api_budgets.rb
144
+ - db/migrate/20260725205417_add_conversation_profile_fields.rb
145
+ - docs/CONFIGURATION.md
109
146
  - docs/app_review_guide.md
110
147
  - docs/auth_paths.md
111
148
  - docs/roadmap.md
@@ -126,10 +163,31 @@ files:
126
163
  - lib/instagram_connect/engine.rb
127
164
  - lib/instagram_connect/errors.rb
128
165
  - lib/instagram_connect/ingest.rb
166
+ - lib/instagram_connect/ingest/dispatcher.rb
167
+ - lib/instagram_connect/ingest/envelope.rb
168
+ - lib/instagram_connect/ingest/handlers/base.rb
169
+ - lib/instagram_connect/ingest/handlers/comments.rb
170
+ - lib/instagram_connect/ingest/handlers/mentions.rb
171
+ - lib/instagram_connect/ingest/handlers/message_reactions.rb
172
+ - lib/instagram_connect/ingest/handlers/messages.rb
173
+ - lib/instagram_connect/ingest/handlers/messaging_handover.rb
174
+ - lib/instagram_connect/ingest/handlers/messaging_optins.rb
175
+ - lib/instagram_connect/ingest/handlers/messaging_postbacks.rb
176
+ - lib/instagram_connect/ingest/handlers/messaging_referral.rb
177
+ - lib/instagram_connect/ingest/handlers/messaging_seen.rb
178
+ - lib/instagram_connect/ingest/handlers/story_insights.rb
179
+ - lib/instagram_connect/ingest/handlers/unknown.rb
180
+ - lib/instagram_connect/ingest/registry.rb
181
+ - lib/instagram_connect/ingest/summary.rb
182
+ - lib/instagram_connect/media/attaching.rb
183
+ - lib/instagram_connect/media/limits.rb
129
184
  - lib/instagram_connect/messaging_window.rb
130
185
  - lib/instagram_connect/railtie.rb
186
+ - lib/instagram_connect/rate_limiter.rb
131
187
  - lib/instagram_connect/result.rb
132
188
  - lib/instagram_connect/signature_verifier.rb
189
+ - lib/instagram_connect/text_splitter.rb
190
+ - lib/instagram_connect/usage.rb
133
191
  - lib/instagram_connect/version.rb
134
192
  homepage: https://github.com/kshtzkr/instagram_connect
135
193
  licenses:
data/MIT-LICENSE DELETED
@@ -1,20 +0,0 @@
1
- Copyright (c) 2026 Kshitiz Sinha
2
-
3
- Permission is hereby granted, free of charge, to any person obtaining
4
- a copy of this software and associated documentation files (the
5
- "Software"), to deal in the Software without restriction, including
6
- without limitation the rights to use, copy, modify, merge, publish,
7
- distribute, sublicense, and/or sell copies of the Software, and to
8
- permit persons to whom the Software is furnished to do so, subject to
9
- the following conditions:
10
-
11
- The above copyright notice and this permission notice shall be
12
- included in all copies or substantial portions of the Software.
13
-
14
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17
- IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18
- CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19
- TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20
- SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.