ractive_campaign 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: bea9a04deb5b0cdfadc355f3e4ee086355a97bab723e4cdff4c1b6b7caf5b2ee
4
- data.tar.gz: d7725b4597f94558074d7b56c2592f9ad30b18c6afb5f0df90dd2cc215b0abed
3
+ metadata.gz: a1f97cdac78234978c3b5e8d87359883ba8844d646fd7ed2d0c41a50168537f4
4
+ data.tar.gz: 55f1c00eb4a214570eb4a9d97c43e62e0d396f725769b81a62f096d111902315
5
5
  SHA512:
6
- metadata.gz: 17b27596b7f367df37952a6b4256d906d1e5cc5bd1697088b94dccd4c9cfe93f52f637af47db98db9d85111f8ef571877b0ea7c0e5d93c61a554f82df35c3a3d
7
- data.tar.gz: 9770a5164db9c6d2923025db4a5e3c5a1a87d1e83db4a9c74fd175630e1144ee8f4bedd3db6c5281a4eeb0167d3ccffbc9cd1f1b084ad39ebfc16ebc6e0fff25
6
+ metadata.gz: a86d70655730f310016ba26ea518d0e53f461479de71cd9dd72e214c07c72b86983ed499b641eb3cb866d28724bdd88513464442e5cdd4244d5f076ac1fa7c86
7
+ data.tar.gz: 5b76063e260e6df6d49854111b74f4192352d3ed2b0c3d81326644519510d64194def26f22da60bd8661966a78be0d83155288bc124d7a006d70e8dc8e0a6ead
data/Gemfile.lock CHANGED
@@ -1,13 +1,11 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ractive_campaign (0.1.0)
4
+ ractive_campaign (0.1.1)
5
5
  activemodel (~> 6.1.4)
6
- activesupport (~> 6.1.4)
7
6
  dry-configurable (~> 0.7.0)
8
7
  faraday (~> 1.8.0)
9
8
  faraday_middleware (~> 1.2.0)
10
- oj (>= 3.0, < 4.0)
11
9
 
12
10
  GEM
13
11
  remote: https://rubygems.org/
@@ -62,7 +60,6 @@ GEM
62
60
  method_source (1.0.0)
63
61
  minitest (5.14.4)
64
62
  multipart-post (2.1.1)
65
- oj (3.13.9)
66
63
  parallel (1.21.0)
67
64
  parser (3.0.2.0)
68
65
  ast (~> 2.4.1)
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
1
  # RactiveCampaign
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/ractive_campaign`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
3
+ Simple wrapper for ActiveCampaign API v3
6
4
 
7
5
  ## Installation
8
6
 
@@ -22,13 +20,52 @@ Or install it yourself as:
22
20
 
23
21
  ## Usage
24
22
 
25
- TODO: Write usage instructions here
23
+ ### Config
24
+
25
+ ```ruby
26
+ # config/initializers/ractive_campaign.rb
27
+
28
+ ActiveCampaign.configure do |config|
29
+ config.api_url = ENV["AC_API_URL"] # ex.: https://mystartup.api-us1.com/api/3
30
+ config.api_key = ENV["AC_API_KEY"]
31
+ end
32
+ ```
33
+
34
+ ### Examples
35
+
36
+ #### Contact
26
37
 
27
- ## Development
38
+ ```ruby
39
+ # new
40
+ harry = ActiveCampaign::Contact.new firstName: "Harry", lastName: "Potter", email: "potter.hurray@hogwash.com"
41
+ harry.save
28
42
 
29
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
43
+ # create
44
+ hermione = ActiveCampaign::Contact.new firstName: "Hermione", lastName: "Granger", email: "mione@drtooth.com"
45
+
46
+ # update
47
+ ron = ActiveCampaign::Contact.create email: "ron1988@hogwash.com", lastName: "Weasley"
48
+ ron.firstName = "Ron"
49
+ ron.save
50
+ ```
30
51
 
31
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
52
+ #### Tag
53
+
54
+ ```ruby
55
+ ActiveCampaign::Contact.create tag: "hogwarts"
56
+ ```
57
+
58
+ #### Contact tags
59
+
60
+ ```ruby
61
+ harry = ActiveCampaign::Contact.find_by email: "potter.hurray@hogwash.com"
62
+
63
+ # add a contact tag
64
+ harry.add_tag "hogwargs"
65
+
66
+ # remove a tag
67
+ harry.remove_tag "hogwarts"
68
+ ```
32
69
 
33
70
  ## Contributing
34
71
 
@@ -4,20 +4,6 @@ module ActiveCampaign
4
4
  module Attributes # :nodoc:
5
5
  extend ActiveSupport::Concern
6
6
 
7
- attr_accessor :all_attrs
8
-
9
- DEFAULT_ATTRS = %i[
10
- id
11
- cdate
12
- created_timestamp
13
- updated_timestamp
14
- created_utc_timestamp
15
- updated_utc_timestamp
16
- created_by
17
- updated_by
18
- links
19
- ].freeze
20
-
21
7
  def only_changes_to_params
22
8
  return nil unless changed?
23
9
 
@@ -29,7 +15,7 @@ module ActiveCampaign
29
15
  end
30
16
 
31
17
  def to_params
32
- iv = instance_variables - DEFAULT_ATTRS.map { |da| :"@#{da}" } - %i[@mutations_from_database @mutations_before_last_save]
18
+ iv = instance_variables - %i[@mutations_from_database @mutations_before_last_save]
33
19
 
34
20
  {
35
21
  self.class.root_element => iv.map { |v| [v.to_s.delete("@"), instance_variable_get(v)] }.to_h
@@ -39,6 +25,7 @@ module ActiveCampaign
39
25
  module ClassMethods # :nodoc:
40
26
  def define_attributes(*attrs)
41
27
  attrs.each do |attr|
28
+ # ActiveModel's define_attribute_methods
42
29
  define_attribute_methods attr
43
30
 
44
31
  class_eval <<-RUBY, __FILE__, __LINE__ + 1
@@ -63,6 +50,8 @@ module ActiveCampaign
63
50
  end
64
51
 
65
52
  def instantiate_record(klass, data)
53
+ define_attributes(*data.keys)
54
+
66
55
  record = klass.new data
67
56
  record.clear_changes_information
68
57
  record
@@ -75,11 +64,11 @@ module ActiveCampaign
75
64
  end
76
65
 
77
66
  def endpoint
78
- name.demodulize.underscore.pluralize
67
+ name.demodulize.camelize(:lower).pluralize
79
68
  end
80
69
 
81
70
  def root_element
82
- name.demodulize.underscore.to_sym
71
+ name.demodulize.camelize(:lower).to_sym
83
72
  end
84
73
 
85
74
  def root_elements
@@ -10,11 +10,17 @@ module ActiveCampaign
10
10
  HTTP_METHODS.each do |method|
11
11
  class_eval <<-RUBY, __FILE__, __LINE__ + 1
12
12
  def #{method}(path, params={})
13
+ root = params.delete(:_root)
14
+
13
15
  send(:'#{method}_raw', path, params) do |parsed_data, response|
14
16
  return {} unless [200, 201].include?(parsed_data[:status_code])
15
17
  return {} unless parsed_data[:data].present?
16
18
 
17
- data = parsed_data[:data].first.last
19
+ data = if root
20
+ parsed_data[:data][root.to_sym]
21
+ else
22
+ parsed_data[:data].first.last
23
+ end
18
24
 
19
25
  if data.is_a?(Array)
20
26
  new_records data
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveCampaign
4
+ class Account < Model # :nodoc:
5
+ define_attributes :name, :accountUrl
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveCampaign
4
+ class Address < Model # :nodoc:
5
+ define_attributes :company_name, :address_1, :country
6
+ end
7
+ end
@@ -1,36 +1,49 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveCampaign
4
- class Contact < Model # :nodoc:
4
+ #
5
+ # @example Contacts
6
+ #
7
+ # ActiveCampaign::Contact.find(1)
8
+ # ActiveCampaign::Contact.find_by email: "contact-1@mail.com"
9
+ # ActiveCampaign::Contact.find(1).destroy
10
+ # ActiveCampaign::Contact.find(1).contact_tags
11
+ #
12
+ class Contact < Model
5
13
  define_attributes :email,
6
14
  :phone,
7
15
  :firstName,
8
- :lastName,
9
- :orgid,
10
- :orgname,
11
- :segmentio_id,
12
- :bounced_hard,
13
- :bounced_soft,
14
- :bounced_date,
15
- :ip,
16
- :ua,
17
- :hash,
18
- :socialdata_lastcheck,
19
- :email_local,
20
- :email_domain,
21
- :sentcnt,
22
- :rating_tstamp,
23
- :gravatar,
24
- :deleted,
25
- :anonymized,
26
- :adate,
27
- :udate,
28
- :edate,
29
- :deleted_at,
30
- :email_empty,
31
- :mpp_tracking,
32
- :scoreValues,
33
- :accountContacts,
34
- :organization
16
+ :lastName
17
+
18
+ # @example Find contact's tags.
19
+ #
20
+ # ActiveCampaign::Contact.find(1).contact_tags
21
+ # ActiveCampaign::Contact.find(1).contact_tags.last.destroy
22
+ #
23
+ def contact_tags
24
+ ContactTag.get "#{self.class.endpoint}/#{id}/contactTags"
25
+ end
26
+
27
+ # @example Apply a tag to a contact.
28
+ #
29
+ # ActiveCampaign::Contact.find(1).add_tag "tag-name"
30
+ #
31
+ def add_tag(tag)
32
+ tag_id = Tag.find_by(tag: tag)&.id
33
+
34
+ ActiveCampaign::ContactTag.create contact: id, tag: tag_id if tag_id
35
+ end
36
+
37
+ # @example Remove a tag from a contact.
38
+ #
39
+ # ActiveCampaign::Contact.find(1).remove_tag "tag-name"
40
+ #
41
+ def remove_tag(tag)
42
+ tag_id = Tag.find_by(tag: tag)&.id
43
+
44
+ contact_tag = contact_tags.filter_map { |ct| ct if ct.tag == tag_id }.last
45
+
46
+ contact_tag&.destroy
47
+ end
35
48
  end
36
49
  end
@@ -1,7 +1,40 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveCampaign
4
- class ContactTag < Model # :nodoc:
5
- define_attributes :tag_id, :contact_id
4
+ #
5
+ # @example ContactTags
6
+ #
7
+ # contact = ActiveCampaign::Contact.find(email: "contact-email@mail.com")
8
+ # tag = ActiveCampaign::Tag.find_by(tag: "tag-name")
9
+ #
10
+ # ActiveCampaing::ContactTag.create contact: contact, tag: tag
11
+ #
12
+ class ContactTag < Model
13
+ define_attributes :contact, :tag
14
+
15
+ def create
16
+ self.tag = extract_id_from_tag(tag)
17
+ self.contact = extract_id_from_contact(contact)
18
+
19
+ super
20
+ end
21
+
22
+ private
23
+
24
+ def extract_id_from_tag(tag)
25
+ case tag
26
+ when ::ActiveCampaign::Tag then tag.id
27
+ else
28
+ tag
29
+ end
30
+ end
31
+
32
+ def extract_id_from_contact(contact)
33
+ case contact
34
+ when ::ActiveCampaign::Contact then contact.id
35
+ else
36
+ contact
37
+ end
38
+ end
6
39
  end
7
40
  end
@@ -8,23 +8,23 @@ module ActiveCampaign
8
8
  include ActiveCampaign::ApiHttp
9
9
  include ActiveCampaign::Attributes
10
10
 
11
- define_attributes(*DEFAULT_ATTRS)
12
-
13
11
  class << self
14
12
  def all
15
13
  get endpoint
16
14
  end
17
15
 
18
16
  def filter(args = {})
19
- get "#{endpoint}?#{args}"
17
+ query = args.map { |k, v| "filters[#{k}]=#{v}" }.join "&"
18
+
19
+ get "#{endpoint}?#{query}"
20
20
  end
21
21
 
22
22
  def find_by(args = {})
23
- filter(args.to_query).last
23
+ filter(args).last
24
24
  end
25
25
 
26
26
  def find(id)
27
- get "#{endpoint}/#{id}"
27
+ get "#{endpoint}/#{id}", { _root: root_element }
28
28
  end
29
29
 
30
30
  def save(**args)
@@ -37,7 +37,7 @@ module ActiveCampaign
37
37
  end
38
38
 
39
39
  def save
40
- return update if id.present?
40
+ return update if defined?(id) && id.present?
41
41
 
42
42
  create
43
43
  end
@@ -47,17 +47,21 @@ module ActiveCampaign
47
47
 
48
48
  result = self.class.put "#{self.class.endpoint}/#{id}", only_changes_to_params
49
49
 
50
+ assign_attributes result.to_params.first.last
51
+
50
52
  changes_applied
51
53
 
52
- result
54
+ self
53
55
  end
54
56
 
55
57
  def create
56
58
  result = self.class.post self.class.endpoint, to_params
57
59
 
60
+ assign_attributes result.to_params.first.last
61
+
58
62
  changes_applied
59
63
 
60
- result
64
+ self
61
65
  end
62
66
 
63
67
  def destroy
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveCampaign
4
+ class Note < Model # :nodoc:
5
+ define_attributes :note, :relid, :reltype
6
+ end
7
+ end
@@ -2,12 +2,6 @@
2
2
 
3
3
  module ActiveCampaign
4
4
  class Tag < Model # :nodoc:
5
- define_attributes :tagType, :tag, :description, :subscriber_count
6
-
7
- class << self
8
- def find_by(tag:)
9
- filter({ tags: { tag: tag } }.to_query).last
10
- end
11
- end
5
+ define_attributes :tag, :tagType, :description
12
6
  end
13
7
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveCampaign
4
- VERSION = "0.1.0"
4
+ VERSION = "0.1.1"
5
5
  end
@@ -12,7 +12,6 @@ require "active_support/inflector"
12
12
  require "active_campaign/version"
13
13
  require "active_campaign/parser"
14
14
  require "active_campaign/api_attributes"
15
- require "active_campaign/api_errors"
16
15
  require "active_campaign/api"
17
16
  require "active_campaign/api_http"
18
17
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ractive_campaign
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
  - Wedson Lima
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-11-13 00:00:00.000000000 Z
11
+ date: 2021-11-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -24,20 +24,6 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: 6.1.4
27
- - !ruby/object:Gem::Dependency
28
- name: activesupport
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - "~>"
32
- - !ruby/object:Gem::Version
33
- version: 6.1.4
34
- type: :runtime
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - "~>"
39
- - !ruby/object:Gem::Version
40
- version: 6.1.4
41
27
  - !ruby/object:Gem::Dependency
42
28
  name: dry-configurable
43
29
  requirement: !ruby/object:Gem::Requirement
@@ -80,26 +66,6 @@ dependencies:
80
66
  - - "~>"
81
67
  - !ruby/object:Gem::Version
82
68
  version: 1.2.0
83
- - !ruby/object:Gem::Dependency
84
- name: oj
85
- requirement: !ruby/object:Gem::Requirement
86
- requirements:
87
- - - ">="
88
- - !ruby/object:Gem::Version
89
- version: '3.0'
90
- - - "<"
91
- - !ruby/object:Gem::Version
92
- version: '4.0'
93
- type: :runtime
94
- prerelease: false
95
- version_requirements: !ruby/object:Gem::Requirement
96
- requirements:
97
- - - ">="
98
- - !ruby/object:Gem::Version
99
- version: '3.0'
100
- - - "<"
101
- - !ruby/object:Gem::Version
102
- version: '4.0'
103
69
  description: Active Campaign - API v3
104
70
  email:
105
71
  - wedson.sousa.lima@gmail.com
@@ -121,12 +87,14 @@ files:
121
87
  - lib/active_campaign.rb
122
88
  - lib/active_campaign/api.rb
123
89
  - lib/active_campaign/api_attributes.rb
124
- - lib/active_campaign/api_errors.rb
125
90
  - lib/active_campaign/api_http.rb
91
+ - lib/active_campaign/models/account.rb
92
+ - lib/active_campaign/models/address.rb
126
93
  - lib/active_campaign/models/contact.rb
127
94
  - lib/active_campaign/models/contact_tag.rb
128
95
  - lib/active_campaign/models/list.rb
129
96
  - lib/active_campaign/models/model.rb
97
+ - lib/active_campaign/models/note.rb
130
98
  - lib/active_campaign/models/tag.rb
131
99
  - lib/active_campaign/parser.rb
132
100
  - lib/active_campaign/version.rb
@@ -1,47 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module ActiveCampaign
4
- class Error < RuntimeError # :nodoc:
5
- def initialize(response = nil, exception = nil)
6
- super
7
-
8
- self.response = response
9
- @exception = exception
10
- end
11
-
12
- def message
13
- if response.nil?
14
- super
15
- else
16
- <<~MESSAGE
17
- STATUS: #{response.status}
18
- URL: #{env.url}
19
- REQUEST HEADERS: #{env.request_headers}
20
- RESPONSE_HEADERS: #{env.response_headers}
21
- REQUEST_BODY: #{env.request_body}\n\n"
22
- RESPONSE_BODY: #{response.body}\n\n"
23
- MESSAGE
24
- end
25
- end
26
-
27
- private
28
-
29
- def env
30
- @env ||= response.env
31
- end
32
-
33
- attr_accessor :response
34
- end
35
-
36
- class ClientError < Error
37
- end
38
-
39
- class UnauthorizedError < ClientError
40
- end
41
-
42
- class ResourceNotFound < ClientError
43
- end
44
-
45
- class UnprocessableEntityError < ClientError
46
- end
47
- end