Class: FlowChat::Instagram::Configuration
- Inherits:
-
Object
- Object
- FlowChat::Instagram::Configuration
- Includes:
- NamedConfiguration
- Defined in:
- lib/flow_chat/instagram/configuration.rb
Constant Summary collapse
- LOGIN_PATHS =
:facebook is the Instagram API with Facebook Login: the linked Page speaks through graph.facebook.com. :instagram is the Instagram API with Instagram Login: the Instagram professional account speaks for itself through graph.instagram.com, with no Page in the picture.
[:facebook, :instagram].freeze
Instance Attribute Summary collapse
-
#access_token ⇒ Object
Returns the value of attribute access_token.
-
#app_id ⇒ Object
Returns the value of attribute app_id.
-
#app_secret ⇒ Object
Returns the value of attribute app_secret.
-
#instagram_account_id ⇒ Object
Returns the value of attribute instagram_account_id.
-
#login ⇒ Object
Returns the value of attribute login.
-
#name ⇒ Object
Returns the value of attribute name.
-
#page_id ⇒ Object
Returns the value of attribute page_id.
-
#skip_signature_validation ⇒ Object
Returns the value of attribute skip_signature_validation.
-
#verify_token ⇒ Object
Returns the value of attribute verify_token.
Class Method Summary collapse
Instance Method Summary collapse
-
#account_id ⇒ Object
What a send is addressed to.
- #api_base_url ⇒ Object
- #api_headers ⇒ Object
- #attachment_upload_url ⇒ Object
-
#initialize(name) ⇒ Configuration
constructor
A new instance of Configuration.
- #messages_url ⇒ Object
- #valid? ⇒ Boolean
-
#webhook_account_id ⇒ Object
The id an inbound webhook's entry.id names, which is a different question from account_id above and has a different answer on the Facebook Login path.
Methods included from NamedConfiguration
Constructor Details
#initialize(name) ⇒ Configuration
Returns a new instance of Configuration.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/flow_chat/instagram/configuration.rb', line 16 def initialize(name) @name = name @access_token = nil @page_id = nil @instagram_account_id = nil @verify_token = nil @app_id = nil @app_secret = nil @skip_signature_validation = false @login = :facebook FlowChat.logger.debug { "Instagram::Configuration: Initialized configuration with name: #{name || "anonymous"}" } register_as(name) if name.present? end |
Instance Attribute Details
#access_token ⇒ Object
Returns the value of attribute access_token.
12 13 14 |
# File 'lib/flow_chat/instagram/configuration.rb', line 12 def access_token @access_token end |
#app_id ⇒ Object
Returns the value of attribute app_id.
12 13 14 |
# File 'lib/flow_chat/instagram/configuration.rb', line 12 def app_id @app_id end |
#app_secret ⇒ Object
Returns the value of attribute app_secret.
12 13 14 |
# File 'lib/flow_chat/instagram/configuration.rb', line 12 def app_secret @app_secret end |
#instagram_account_id ⇒ Object
Returns the value of attribute instagram_account_id.
12 13 14 |
# File 'lib/flow_chat/instagram/configuration.rb', line 12 def instagram_account_id @instagram_account_id end |
#login ⇒ Object
Returns the value of attribute login.
14 15 16 |
# File 'lib/flow_chat/instagram/configuration.rb', line 14 def login @login end |
#name ⇒ Object
Returns the value of attribute name.
12 13 14 |
# File 'lib/flow_chat/instagram/configuration.rb', line 12 def name @name end |
#page_id ⇒ Object
Returns the value of attribute page_id.
12 13 14 |
# File 'lib/flow_chat/instagram/configuration.rb', line 12 def page_id @page_id end |
#skip_signature_validation ⇒ Object
Returns the value of attribute skip_signature_validation.
12 13 14 |
# File 'lib/flow_chat/instagram/configuration.rb', line 12 def skip_signature_validation @skip_signature_validation end |
#verify_token ⇒ Object
Returns the value of attribute verify_token.
12 13 14 |
# File 'lib/flow_chat/instagram/configuration.rb', line 12 def verify_token @verify_token end |
Class Method Details
.from_credentials ⇒ Object
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 |
# File 'lib/flow_chat/instagram/configuration.rb', line 44 def self.from_credentials FlowChat.logger.info { "Instagram::Configuration: Loading configuration from credentials/environment" } config = new(nil) if defined?(Rails) && Rails.respond_to?(:application) && Rails.application&.credentials&.instagram FlowChat.logger.debug { "Instagram::Configuration: Loading from Rails credentials" } credentials = Rails.application.credentials.instagram config.access_token = credentials[:access_token] config.page_id = credentials[:page_id] config.instagram_account_id = credentials[:instagram_account_id] config.verify_token = credentials[:verify_token] config.app_id = credentials[:app_id] config.app_secret = credentials[:app_secret] config.skip_signature_validation = credentials[:skip_signature_validation] || false config.login = (credentials[:login] || "facebook").to_sym else FlowChat.logger.debug { "Instagram::Configuration: Loading from environment variables" } config.access_token = ENV["INSTAGRAM_ACCESS_TOKEN"] config.page_id = ENV["INSTAGRAM_PAGE_ID"] config.instagram_account_id = ENV["INSTAGRAM_ACCOUNT_ID"] config.verify_token = ENV["INSTAGRAM_VERIFY_TOKEN"] config.app_id = ENV["INSTAGRAM_APP_ID"] config.app_secret = ENV["INSTAGRAM_APP_SECRET"] config.skip_signature_validation = ENV["INSTAGRAM_SKIP_SIGNATURE_VALIDATION"] == "true" config.login = (ENV["INSTAGRAM_LOGIN"] || "facebook").to_sym end if config.valid? FlowChat.logger.info { "Instagram::Configuration: Configuration loaded successfully - #{config.login} login, account #{config.account_id}" } else FlowChat.logger.warn { "Instagram::Configuration: Incomplete configuration loaded - missing required fields" } end config end |
Instance Method Details
#account_id ⇒ Object
What a send is addressed to. An account reached through a Page answers as that Page over graph.facebook.com; an account with no Page answers for itself over graph.instagram.com. This is not the id an inbound delivery names, which is webhook_account_id below.
111 112 113 |
# File 'lib/flow_chat/instagram/configuration.rb', line 111 def account_id (login == :instagram) ? instagram_account_id : page_id end |
#api_base_url ⇒ Object
135 136 137 |
# File 'lib/flow_chat/instagram/configuration.rb', line 135 def api_base_url (login == :instagram) ? FlowChat::Config.instagram.instagram_login_api_base_url : FlowChat::Config.instagram.api_base_url end |
#api_headers ⇒ Object
139 140 141 142 143 144 |
# File 'lib/flow_chat/instagram/configuration.rb', line 139 def api_headers { "Authorization" => "Bearer #{access_token}", "Content-Type" => "application/json" } end |
#attachment_upload_url ⇒ Object
131 132 133 |
# File 'lib/flow_chat/instagram/configuration.rb', line 131 def "#{api_base_url}/#{account_id}/message_attachments" end |
#messages_url ⇒ Object
127 128 129 |
# File 'lib/flow_chat/instagram/configuration.rb', line 127 def "#{api_base_url}/#{account_id}/messages" end |
#valid? ⇒ Boolean
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/flow_chat/instagram/configuration.rb', line 81 def valid? # Both ids are required, because sending and receiving key on # different ones and only on the :facebook path do they differ. # account_id is what a send is addressed as (the Page there); # instagram_account_id is what an inbound delivery names in entry.id # on both paths, which is what webhook_account_id encodes. # # Checking only account_id passed a :facebook configuration that had # never been given an instagram_account_id, and the gateway then # rejected every delivery it received: the id it compares against was # blank, and a blank expectation matches nothing. A configuration that # answers the handshake and then refuses all traffic is worse than one # that admits up front it is incomplete. # # Wrapped so a predicate answers true or false rather than nil, which # the bare && chain returns for a missing first field. Intercom and # Telegram already do this and pin it in their tests. is_valid = !!(access_token && !access_token.to_s.empty? && verify_token && !verify_token.to_s.empty? && account_id && !account_id.to_s.empty? && webhook_account_id && !webhook_account_id.to_s.empty?) FlowChat.logger.debug { "Instagram::Configuration: Configuration valid: #{is_valid}" } is_valid end |
#webhook_account_id ⇒ Object
The id an inbound webhook's entry.id names, which is a different question from account_id above and has a different answer on the Facebook Login path.
The top-level object of a delivery decides the id space, and this
gateway only ever handles instagram (see expected_webhook_object),
which names the Instagram professional account. That holds on both
paths, so unlike account_id this does not depend on login.
123 124 125 |
# File 'lib/flow_chat/instagram/configuration.rb', line 123 def webhook_account_id instagram_account_id end |