A fork from activo-inc fork's from luk4s code which is RoR helper / wrapper for Mautic API and forms. But it is using Redis instead of DB to store the credentials.
- The original code is DB dependent, you must create a table called
mautic_connections
in your DB. - The original code has some routes and views which is not a good idea to expose.
- The
activo-inc fork's
has some good enhancements and additions.
- Added Redis integration to save the tokens.
- Removed
some
un-needed files (i.e. assets). - Removed views and un-needed controller methods.
- Followed RuboCop in some recommendations.
Add this line to your application's Gemfile:
gem 'mautic', github: 'TheDartsCo/mautic-redis-rails'
- Create Mautic Oauth2 API.
- Create
config/initializers/mautic.rb
and fill in your credentials.
Mautic.configure do |config|
# Mautic URL
config.mautic_url = 'https://mautic.my.app'
# Public Key
config.public_key = 'public_key'
# Secret Key
config.secret_key = 'secret_key'
# Redis Connection Config
config.redis_config = { url: 'redis://127.0.0.1:6379' }
end
- Add to
config/routes.rb
mount Mautic::Engine => '/mautic'
- Create Mautic Oauth2 API
- Store the API Public Key and Secret Key in
mautic.rb
initializer file - Authorize it by visiting
your-website-url/mautic/authorize
- Then to use it in your app,
m = Mautic::Connection.new
Get specify contact:
contact = m.contact.find(1) # => #<Mautic::Contact id=1 ...>
Collections of contacts:
m.contacts.where("gmail").each do |contact|
#<Mautic::Contact id=12 ...>
#<Mautic::Contact id=21 ...>
#<Mautic::Contact id=99 ...>
end
New instance of contacts:
contact = m.contacts.new({ email: "newcontactmail@fake.info"} )
contact.save # => true
Update contact
contact.email = ""
contact.save # => false
contact.errors # => [{"code"=>400, "message"=>"email: This field is required.", "details"=>{"email"=>["This field is required."]}}]
Of course you can use more than contact: assets
, emails
, companies
, forms
, points
...
There are two options of usage:
- Use default mautic url from configuration and shortcut class method:
# form: ID of form in Mautic *required*
# url: Mautic URL - default is from configuration
# request: request object (for domain, and forward IP...) *optional*
Mautic::FormHelper.submit(form: "mautic form ID") do |i|
i.form_field1 = "value1"
i.form_field2 = "value2"
end
- Or create instance
# request is *optional*
m = Mautic::FormHelper.new("https://mymautic.com", request)
m.data = {} # hash of attributes
m.push # push data to mautic
Receive webhook from mautic, parse it and prepare for use.
-
add concern to your controller
include Mautic::ReceiveWebHooks
-
in routes must be specify
:mautic_id
, for example:post "webhook/:mautic_id", action: "webhook", on: :collection
- A lot of cleaning.
- Fix all tests.
- Make sure the forms and webhook are working.
Ideas and pull requests are welcome!
The gem is available as open source under the terms of the MIT License.