mautic 1.0.1 → 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 395ca0792f8a56986bc97cc6a267deb9c24cdb44
4
- data.tar.gz: 15c8e1583f8ea06db7683400978c8bf064b54e77
3
+ metadata.gz: 4be984905747a5913cd7271d2f0f8b2475a22cf6
4
+ data.tar.gz: 14020973bd047ea58c470d3270f1b3000ded51f4
5
5
  SHA512:
6
- metadata.gz: bfff68b112c2edb739a49d4d549d1165f91d728b412c4a4e24219387e23ba65b7ec4f3ddc6892ddb3206b494e85f99b1b31d3b3238bc262cdf12859d7ca337d5
7
- data.tar.gz: 5ec16c2043356e0118c0b509a8358b8902b750645e30092b3fa50f92c21aa24de986b1ca17c91b6232a5a5ae1848ff57cb691712c7c1ab5a1d11f9c2e7181c42
6
+ metadata.gz: 4f116539c9dc6a253e3373bf91c06bcc88600c120237bbb376769b1b3c2c6fb8711e6ed4dc9e828c53dbd2115bfd6166528b1c4726c2ded28dfde27d23b4713a
7
+ data.tar.gz: 65cf3ee03b29e4561ea9828f5f3ee1a6b78a0d71d7124642512e64af12315378bfd9369c705c9d1201334c9d231cdbc815dbd411f9d223c51304bfb5ee0e7b7b
data/README.md CHANGED
@@ -5,6 +5,13 @@ RoR helper / wrapper for Mautic API and forms
5
5
  ### Gem provides API connection to your Mautic(s)
6
6
  1. Create mautic connection
7
7
  2. Authorize it
8
+
9
+ In mautic you need add API oauth2 login.
10
+ For URI callback allow:
11
+ ```
12
+ http://localhost:3000/mautic/connections/:ID/oauth2
13
+ ```
14
+ ID = is your Mautic::Connection ID
8
15
 
9
16
  Find connection which you want to use:
10
17
  ```ruby
@@ -39,9 +39,40 @@ module Mautic
39
39
  end
40
40
 
41
41
  def request(type, path, params = {})
42
- raise NotImplementedError
42
+ @last_request = [type, path, params]
43
+ response = raise NotImplementedError
44
+ parse_response(response)
43
45
  end
44
46
 
47
+ private
48
+
49
+ def parse_response(response)
50
+ case response.status
51
+ when 400
52
+ raise Mautic::ValidationError.new(response)
53
+ when 404
54
+ raise Mautic::RecordNotFound.new(response)
55
+ when 200, 201
56
+ json = JSON.parse(response.body) rescue {}
57
+ Array(json['errors']).each do |error|
58
+ case error['code'].to_i
59
+ when 401
60
+ raise Mautic::TokenExpiredError.new(response) if @try_to_refresh
61
+ @try_to_refresh = true
62
+ refresh!
63
+ json = request(*@last_request)
64
+ when 404
65
+ raise Mautic::RecordNotFound.new(response)
66
+ else
67
+ raise Mautic::RequestError.new(response)
68
+ end
69
+ end
70
+ else
71
+ raise Mautic::RequestError.new(response)
72
+ end
73
+
74
+ json
75
+ end
45
76
 
46
77
  end
47
78
  end
@@ -30,21 +30,9 @@ module Mautic
30
30
  end
31
31
 
32
32
  def request(type, path, params = {})
33
- json = JSON.parse connection.request(type, path, params).body
34
- Array(json['errors']).each do |error|
35
- case error['code']
36
- when 400
37
- # Validation error
38
- when 401
39
- raise Mautic::TokenExpiredError.new(error['message']) if @try_to_refresh
40
- @try_to_refresh = true
41
- refresh!
42
- json = request(type, path, params)
43
- else
44
- raise Mautic::AuthorizeError.new("#{error['code']} - #{error['message']}")
45
- end
46
- end
47
- json
33
+ @last_request = [type, path, params]
34
+ response = connection.request(type, path, params)
35
+ parse_response(response)
48
36
  end
49
37
 
50
38
  private
@@ -1,6 +1,8 @@
1
1
  module Mautic
2
2
  class Contact < Model
3
3
 
4
+ alias_attribute :first_name, :firstname
5
+ alias_attribute :last_name, :lastname
4
6
  def self.in(connection)
5
7
  Proxy.new(connection, endpoint, default_params: { search: '!is:anonymous' })
6
8
  end
@@ -8,14 +8,46 @@ module Mautic
8
8
  autoload :Proxy, 'mautic/proxy'
9
9
  autoload :Model, 'mautic/model'
10
10
 
11
- class TokenExpiredError < StandardError
11
+ class RequestError < StandardError
12
+
13
+ attr_reader :response, :errors
14
+
15
+ def initialize(response, message = nil)
16
+ @errors ||= []
17
+ @response = response
18
+ json_body = JSON.parse(response.body) rescue {}
19
+ message ||= Array(json_body['errors']).collect do |error|
20
+ msg = error['code'].to_s
21
+ msg << " (#{error['type']}):" if error['type']
22
+ msg << " #{error['message']}"
23
+ @errors << error['message']
24
+ msg
25
+ end.join(', ')
26
+
27
+ super(message)
28
+ end
29
+
12
30
  end
13
31
 
14
- class ValidationError < StandardError
32
+ class TokenExpiredError < RequestError
33
+ end
34
+
35
+ class ValidationError < RequestError
36
+
37
+ def initialize(response, message = nil)
38
+ @response = response
39
+ json_body = JSON.parse(response.body) rescue {}
40
+ @errors = Array(json_body['errors']).inject({}) { |mem, var| mem.merge!(var['details']); mem }
41
+ message ||= @errors.collect { |field, msg| "#{field}: #{msg.join(', ')}" }.join('; ')
42
+ super(response, message)
43
+ end
44
+
45
+ end
15
46
 
47
+ class AuthorizeError < RequestError
16
48
  end
17
49
 
18
- class AuthorizeError < StandardError
50
+ class RecordNotFound < RequestError
19
51
  end
20
52
 
21
53
  configure do |config|
@@ -32,6 +32,7 @@ module Mautic
32
32
  @table = MauticHash.new
33
33
  self.attributes = { created_at: hash['dateAdded']&.to_time, updated_at: hash['dateModified']&.to_time } if hash
34
34
  assign_attributes(hash)
35
+ clear_changes
35
36
  end
36
37
 
37
38
  def save(force = false)
@@ -40,29 +41,37 @@ module Mautic
40
41
 
41
42
  def update(force = false)
42
43
  return false if changes.blank?
43
- json = @connection.request((force && :put || :patch), "api/#{endpoint}/#{id}/edit", { body: to_h })
44
- if json['errors']
45
- self.errors = json['errors']
46
- else
44
+ begin
45
+ json = @connection.request((force && :put || :patch), "api/#{endpoint}/#{id}/edit", { body: to_h })
47
46
  self.attributes = json[endpoint.singularize]
47
+ clear_changes
48
+ rescue ValidationError => e
49
+ self.errors = e.errors
48
50
  end
49
- json['errors'].blank?
51
+
52
+ self.errors.blank?
50
53
  end
51
54
 
52
55
  def create
53
- json = @connection.request(:post, "api/#{endpoint}/#{id}/new", { body: to_h })
54
- if json['errors']
55
- self.errors = json['errors']
56
- else
56
+ begin
57
+ json = @connection.request(:post, "api/#{endpoint}/#{id}/new", { body: to_h })
57
58
  self.attributes = json[endpoint.singularize]
59
+ clear_changes
60
+ rescue ValidationError => e
61
+ self.errors = e.errors
58
62
  end
59
- json['errors'].blank?
63
+
64
+ self.errors.blank?
60
65
  end
61
66
 
62
67
  def destroy
63
- json = @connection.request(:delete, "api/#{endpoint}/#{id}/delete")
64
- self.errors = json['errors'] if json['errors']
65
- json['errors'].blank?
68
+ begin
69
+ @connection.request(:delete, "api/#{endpoint}/#{id}/delete")
70
+ true
71
+ rescue RequestError => e
72
+ self.errors = e.errors
73
+ false
74
+ end
66
75
  end
67
76
 
68
77
  def changes
@@ -73,20 +82,23 @@ module Mautic
73
82
  @table.to_h
74
83
  end
75
84
 
76
- private
77
-
78
- def endpoint
79
- self.class.endpoint
80
- end
81
-
82
85
  def attributes=(hash)
83
86
  hash.each_pair do |k, v|
84
87
  k = k.to_sym
85
88
  @table[k] = v
86
89
  end
90
+ end
91
+
92
+ private
93
+
94
+ def clear_changes
87
95
  @table.instance_variable_set(:@changes, nil)
88
96
  end
89
97
 
98
+ def endpoint
99
+ self.class.endpoint
100
+ end
101
+
90
102
  def assign_attributes(source = nil)
91
103
  source ||= {}
92
104
  data = {}
@@ -1,3 +1,3 @@
1
1
  module Mautic
2
- VERSION = '1.0.1'
2
+ VERSION = '1.0.3'
3
3
  end
@@ -1,4 +1,7 @@
1
1
  ENV['RAILS_ENV'] ||= 'test'
2
+ require 'simplecov'
3
+ SimpleCov.start
4
+
2
5
  require File.expand_path("../dummy/config/environment.rb", __FILE__)
3
6
  # Prevent database truncation if the environment is production
4
7
  abort('The Rails environment is running in production mode!') if Rails.env.production?
@@ -3,6 +3,7 @@ require 'factory_bot_rails'
3
3
  require 'database_cleaner'
4
4
  require 'faker'
5
5
  require 'webmock/rspec'
6
+ require 'pry-rails'
6
7
  # require 'capybara/rspec'
7
8
 
8
9
  ActiveRecord::Migration.maintain_test_schema!
@@ -34,10 +35,6 @@ RSpec.configure do |config|
34
35
  end
35
36
 
36
37
  config.include FactoryBot::Syntax::Methods
37
- # DatabaseCleaner.clean_with(
38
- # :truncation,
39
- # except: %w(ar_internal_metadata)
40
- # )
41
38
 
42
39
  config.before(:suite) do
43
40
  DatabaseCleaner.clean_with(:truncation, except: %w(ar_internal_metadata))
@@ -47,10 +44,6 @@ RSpec.configure do |config|
47
44
  DatabaseCleaner.strategy = :transaction
48
45
  end
49
46
 
50
- config.before(:each, js: true) do
51
- DatabaseCleaner.strategy = :truncation
52
- end
53
-
54
47
  config.before(:each) do
55
48
  DatabaseCleaner.start
56
49
  end
@@ -65,21 +58,4 @@ RSpec.configure do |config|
65
58
 
66
59
  config.filter_rails_from_backtrace!
67
60
 
68
- # config.before(:each) do |ex|
69
- # meta = ex.metadata
70
- # unless meta[:null]
71
- # case meta[:logged]
72
- # when :admin
73
- # logged(Symphonia::User, FactoryBot.create(:admin_user))
74
- # when true
75
- # logged(Symphonia::User, FactoryBot.create(:user))
76
- # end
77
- # end
78
- # end
79
-
80
- end
81
-
82
-
83
- def logged(model, user)
84
- allow(model).to receive(:current).and_return(user)
85
61
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mautic
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lukáš Pokorný
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-11-23 00:00:00.000000000 Z
11
+ date: 2017-12-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -215,7 +215,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
215
215
  version: '0'
216
216
  requirements: []
217
217
  rubyforge_project:
218
- rubygems_version: 2.6.12
218
+ rubygems_version: 2.6.13
219
219
  signing_key:
220
220
  specification_version: 4
221
221
  summary: Ruby on Rails Mautic integration