email_octopus 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rubocop.yml +6 -0
- data/.ruby-version +1 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +49 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +107 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/email_octopus.gemspec +27 -0
- data/lib/email_octopus.rb +32 -0
- data/lib/email_octopus/api.rb +43 -0
- data/lib/email_octopus/api/error.rb +14 -0
- data/lib/email_octopus/api/error/api_key_invalid.rb +10 -0
- data/lib/email_octopus/api/error/invalid_parameters.rb +10 -0
- data/lib/email_octopus/api/error/not_found.rb +11 -0
- data/lib/email_octopus/api/error/unauthorized.rb +9 -0
- data/lib/email_octopus/api/response.rb +51 -0
- data/lib/email_octopus/campaign.rb +11 -0
- data/lib/email_octopus/contact.rb +33 -0
- data/lib/email_octopus/list.rb +18 -0
- data/lib/email_octopus/model.rb +117 -0
- data/lib/email_octopus/query.rb +48 -0
- data/lib/email_octopus/version.rb +3 -0
- metadata +126 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d93d5ea9e02ab61638101f02e6bece5ed0fba97d
|
4
|
+
data.tar.gz: 598e7709477dde8e7688b40d5db504f67e86227f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: efa2c775d446299a38953ddc6d5a4c8685006f27d18a13f84e4ef6a8e04b3325109169cc4152f588daeaa1399e3f5be4b212feaea57554ad20920cda3b70691a
|
7
|
+
data.tar.gz: 892ae1e20b19a3fd2c53663504a24f474e599deddf89b468759f60579fdd0c052d56e22fefd5a5257affe4e3e72f724a88c747da71ad6070f25947e57a65693f
|
data/.gitignore
ADDED
data/.rubocop.yml
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.3.1
|
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, and in the interest of
|
4
|
+
fostering an open and welcoming community, we pledge to respect all people who
|
5
|
+
contribute through reporting issues, posting feature requests, updating
|
6
|
+
documentation, submitting pull requests or patches, and other activities.
|
7
|
+
|
8
|
+
We are committed to making participation in this project a harassment-free
|
9
|
+
experience for everyone, regardless of level of experience, gender, gender
|
10
|
+
identity and expression, sexual orientation, disability, personal appearance,
|
11
|
+
body size, race, ethnicity, age, religion, or nationality.
|
12
|
+
|
13
|
+
Examples of unacceptable behavior by participants include:
|
14
|
+
|
15
|
+
* The use of sexualized language or imagery
|
16
|
+
* Personal attacks
|
17
|
+
* Trolling or insulting/derogatory comments
|
18
|
+
* Public or private harassment
|
19
|
+
* Publishing other's private information, such as physical or electronic
|
20
|
+
addresses, without explicit permission
|
21
|
+
* Other unethical or unprofessional conduct
|
22
|
+
|
23
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
24
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
25
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
26
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
27
|
+
threatening, offensive, or harmful.
|
28
|
+
|
29
|
+
By adopting this Code of Conduct, project maintainers commit themselves to
|
30
|
+
fairly and consistently applying these principles to every aspect of managing
|
31
|
+
this project. Project maintainers who do not follow or enforce the Code of
|
32
|
+
Conduct may be permanently removed from the project team.
|
33
|
+
|
34
|
+
This code of conduct applies both within project spaces and in public spaces
|
35
|
+
when an individual is representing the project or its community.
|
36
|
+
|
37
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
38
|
+
reported by contacting a project maintainer at tscott@weblinc.com. All
|
39
|
+
complaints will be reviewed and investigated and will result in a response that
|
40
|
+
is deemed necessary and appropriate to the circumstances. Maintainers are
|
41
|
+
obligated to maintain confidentiality with regard to the reporter of an
|
42
|
+
incident.
|
43
|
+
|
44
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
45
|
+
version 1.3.0, available at
|
46
|
+
[http://contributor-covenant.org/version/1/3/0/][version]
|
47
|
+
|
48
|
+
[homepage]: http://contributor-covenant.org
|
49
|
+
[version]: http://contributor-covenant.org/version/1/3/0/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Tom Scott
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
# EmailOctopus
|
2
|
+
|
3
|
+
An API client for the [EmailOctopus][] email marketing service, allowing
|
4
|
+
you to create/view/update/destroy campaigns, lists, and contacts.
|
5
|
+
|
6
|
+
For more information on how to set up your account, read the [API docs][].
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem 'email_octopus'
|
14
|
+
```
|
15
|
+
|
16
|
+
And then execute:
|
17
|
+
|
18
|
+
$ bundle
|
19
|
+
|
20
|
+
Or install it yourself as:
|
21
|
+
|
22
|
+
$ gem install email_octopus
|
23
|
+
|
24
|
+
## Usage
|
25
|
+
|
26
|
+
First, configure the `EmailOctopus` library so it can be used with your
|
27
|
+
account. After obtaining your API key, add the following before your
|
28
|
+
application is initialized. For Rails apps, this might be
|
29
|
+
**config/initializers/email_octopus.rb**
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
EmailOctopus.configure do |config|
|
33
|
+
config.api_key = 'your-email-octopus-api-key'
|
34
|
+
end
|
35
|
+
```
|
36
|
+
|
37
|
+
You can now perform CRUD actions on the various resources that the API
|
38
|
+
provides.
|
39
|
+
|
40
|
+
Below is a short summary of what you can do with the library. For more
|
41
|
+
information, read the [RDoc][] documentation.
|
42
|
+
|
43
|
+
### Creating a new list
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
list = EmailOctopus::List.create name: 'Newsletter'
|
47
|
+
```
|
48
|
+
|
49
|
+
### Adding a contact to that list
|
50
|
+
|
51
|
+
```ruby
|
52
|
+
list = EmailOctopus::List.find 'previous-list-id'
|
53
|
+
contact = EmailOctopus::Contact.create(
|
54
|
+
first_name: 'Lester',
|
55
|
+
last_name: 'Tester',
|
56
|
+
email_address: 'lester.tester@example.com',
|
57
|
+
subscribed: true,
|
58
|
+
list_id: list.id
|
59
|
+
)
|
60
|
+
```
|
61
|
+
|
62
|
+
### Creating a new campaign for that list
|
63
|
+
|
64
|
+
```ruby
|
65
|
+
list = EmailOctopus::List.find 'previous-list-id'
|
66
|
+
campaign = EmailOctopus::Campaign.new(
|
67
|
+
list_id: list.id,
|
68
|
+
name: 'hello world',
|
69
|
+
content: 'test'
|
70
|
+
)
|
71
|
+
```
|
72
|
+
|
73
|
+
### Removing a user from the contact list
|
74
|
+
|
75
|
+
```ruby
|
76
|
+
contact = EmailOctopus::Contact.find 'previous-contact-id'
|
77
|
+
contact.destroy
|
78
|
+
```
|
79
|
+
|
80
|
+
## Development
|
81
|
+
|
82
|
+
After checking out the repo, run `bin/setup` to install dependencies.
|
83
|
+
Then, run `rake test` to run the tests. You can also run `bin/console`
|
84
|
+
for an interactive prompt that will allow you to experiment.
|
85
|
+
|
86
|
+
To install this gem onto your local machine, run `bundle exec rake install`.
|
87
|
+
|
88
|
+
To release a new version, update the version number in `version.rb`, and then
|
89
|
+
run `bundle exec rake release`, which will create a git tag for the version,
|
90
|
+
push git commits and tags, and push the `.gem` file to
|
91
|
+
[rubygems.org](https://rubygems.org).
|
92
|
+
|
93
|
+
## Contributing
|
94
|
+
|
95
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/tubbo/email_octopus.
|
96
|
+
This project is intended to be a safe, welcoming space for collaboration, and
|
97
|
+
contributors are expected to adhere to the [Contributor Covenant][] code of conduct.
|
98
|
+
|
99
|
+
## License
|
100
|
+
|
101
|
+
The gem is available as open source under the terms of the [MIT License][].
|
102
|
+
|
103
|
+
[EmailOctopus]: http://emailoctopus.com
|
104
|
+
[API docs]: http://emailoctopus.com/api-documentation
|
105
|
+
[Contributor Covenant]: http://contributor-covenant.org
|
106
|
+
[MIT License]: http://opensource.org/licenses/MIT
|
107
|
+
[RDoc]: http://rubydoc.info/github/tubbo/email_octopus/master/frames.html
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "email_octopus"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'email_octopus/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "email_octopus"
|
8
|
+
spec.version = EmailOctopus::VERSION
|
9
|
+
spec.authors = ["Tom Scott"]
|
10
|
+
spec.email = ["tscott@weblinc.com"]
|
11
|
+
|
12
|
+
spec.summary = 'API Client for Email Octopus'
|
13
|
+
spec.description = 'API client for Email Octopus'
|
14
|
+
spec.homepage = 'https://github.com/tubbo/email_octopus'
|
15
|
+
spec.license = 'MIT'
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.12"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
25
|
+
|
26
|
+
spec.add_dependency 'activemodel'
|
27
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'active_model'
|
3
|
+
require 'net/http'
|
4
|
+
require 'json'
|
5
|
+
require 'email_octopus/version'
|
6
|
+
|
7
|
+
# Client for the EmailOctopus API
|
8
|
+
#
|
9
|
+
# https://emailoctopus.com/api-documentation/
|
10
|
+
module EmailOctopus
|
11
|
+
include ActiveSupport::Configurable
|
12
|
+
|
13
|
+
extend ActiveSupport::Autoload
|
14
|
+
|
15
|
+
autoload :API
|
16
|
+
autoload :Model
|
17
|
+
autoload :Campaign
|
18
|
+
autoload :List
|
19
|
+
autoload :Contact
|
20
|
+
|
21
|
+
autoload_under 'api' do
|
22
|
+
autoload :Response
|
23
|
+
autoload :Error
|
24
|
+
end
|
25
|
+
|
26
|
+
autoload_under 'api/error' do
|
27
|
+
autoload :ApiKeyInvalid
|
28
|
+
autoload :InvalidParameters
|
29
|
+
autoload :NotFound
|
30
|
+
autoload :Unauthorized
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'net/http'
|
3
|
+
require 'json'
|
4
|
+
require 'email_octopus/api/response'
|
5
|
+
|
6
|
+
module EmailOctopus
|
7
|
+
# HTTP API gateway to communicate with Email Octopus.
|
8
|
+
class API
|
9
|
+
HOST = 'emailoctopus.com/api/1.1'
|
10
|
+
PORT = 443
|
11
|
+
HEADERS = {
|
12
|
+
'Content-Type' => 'application/json'
|
13
|
+
}.freeze
|
14
|
+
|
15
|
+
def initialize(api_key)
|
16
|
+
@http = Net::HTTP.new HOST, PORT, use_ssl: true
|
17
|
+
@api_key = api_key
|
18
|
+
end
|
19
|
+
|
20
|
+
def get(path)
|
21
|
+
Response.new @http.get("#{path}?api_key=#{@api_key}", HEADERS)
|
22
|
+
end
|
23
|
+
|
24
|
+
def post(path, body = {})
|
25
|
+
body['api_key'] = @api_key
|
26
|
+
Response.new @http.post(path, body.to_json, HEADERS)
|
27
|
+
end
|
28
|
+
|
29
|
+
def patch(path, body = {})
|
30
|
+
body['api_key'] = @api_key
|
31
|
+
Response.new @http.patch(path, body.to_json, HEADERS)
|
32
|
+
end
|
33
|
+
|
34
|
+
def put(path, body = {})
|
35
|
+
body['api_key'] = @api_key
|
36
|
+
Response.new @http.put(path, body.to_json, HEADERS)
|
37
|
+
end
|
38
|
+
|
39
|
+
def delete(path)
|
40
|
+
Response.new @http.delete("#{path}?api_key=#{@api_key}", HEADERS)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module EmailOctopus
|
4
|
+
class API
|
5
|
+
# Base error class, also thrown when unknown errors occur.
|
6
|
+
class Error < StandardError
|
7
|
+
# @param response [EmailOctopus::API::Response] Original response.
|
8
|
+
def initialize(response)
|
9
|
+
@response = response
|
10
|
+
super @response.body['message']
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module EmailOctopus
|
4
|
+
class API
|
5
|
+
# Response object that parses out JSON.
|
6
|
+
class Response
|
7
|
+
delegate :status, :headers, to: :@raw
|
8
|
+
|
9
|
+
def initialize(response)
|
10
|
+
@raw = response
|
11
|
+
raise error_class, self if error?
|
12
|
+
end
|
13
|
+
|
14
|
+
def status
|
15
|
+
@raw.status
|
16
|
+
end
|
17
|
+
|
18
|
+
def headers
|
19
|
+
@raw.headers
|
20
|
+
end
|
21
|
+
|
22
|
+
def success?
|
23
|
+
@raw.is_a? Net::HTTPSuccess
|
24
|
+
end
|
25
|
+
|
26
|
+
def error?
|
27
|
+
!success?
|
28
|
+
end
|
29
|
+
|
30
|
+
def body
|
31
|
+
JSON.parse @raw.body
|
32
|
+
end
|
33
|
+
|
34
|
+
def error_class
|
35
|
+
return unless error?
|
36
|
+
case body['code']
|
37
|
+
when 'INVALID_PARAMETERS'
|
38
|
+
Error::InvalidParameters
|
39
|
+
when 'API_KEY_INVALID'
|
40
|
+
Error::ApiKeyInvalid
|
41
|
+
when 'UNAUTHORISED'
|
42
|
+
Error::Unauthorized
|
43
|
+
when 'NOT_FOUND'
|
44
|
+
Error::NotFound
|
45
|
+
else
|
46
|
+
Error
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module EmailOctopus
|
4
|
+
# Contact of a list
|
5
|
+
class Contact < Model
|
6
|
+
attribute :id
|
7
|
+
attribute :list_id
|
8
|
+
attribute :first_name
|
9
|
+
attribute :last_name
|
10
|
+
attribute :email_address
|
11
|
+
attribute :subscribed
|
12
|
+
attribute :created_at
|
13
|
+
|
14
|
+
def self.where(list_id: '')
|
15
|
+
api = API.new EmailOctopus.config.api_key
|
16
|
+
api.get("/lists/#{list_id}/contacts").body['data'].map do |params|
|
17
|
+
new(params)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def as_json
|
22
|
+
attributes.reject do |(key, _val)|
|
23
|
+
key.to_s =~ 'list_id'
|
24
|
+
end.to_h
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def base_url
|
30
|
+
"/lists/#{list_id}/contacts"
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'email_octopus/contact'
|
3
|
+
|
4
|
+
module EmailOctopus
|
5
|
+
# Mailing list.
|
6
|
+
class List < Model
|
7
|
+
attribute :id
|
8
|
+
attribute :name
|
9
|
+
|
10
|
+
def contacts
|
11
|
+
Contact.where list_id: id
|
12
|
+
end
|
13
|
+
|
14
|
+
def create_contact(params = {})
|
15
|
+
Contact.create params.merge(list_id: id)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,117 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'email_octopus/api'
|
3
|
+
require 'active_model/naming'
|
4
|
+
|
5
|
+
module EmailOctopus
|
6
|
+
# Common code for model objects.
|
7
|
+
class Model
|
8
|
+
extend ActiveModel::Naming
|
9
|
+
|
10
|
+
# @!attribute attributes [r]
|
11
|
+
# @return [Hash] Attributes related to this model.
|
12
|
+
attr_reader :attributes
|
13
|
+
|
14
|
+
# @param params [Hash] Initial attributes for this model.
|
15
|
+
def initialize(params = {})
|
16
|
+
@attributes = params
|
17
|
+
@api = API.new(EmailOctopus.config.api_key)
|
18
|
+
end
|
19
|
+
|
20
|
+
# Instantiate a new resource and immediately persist it on the API.
|
21
|
+
#
|
22
|
+
# @param params [Hash] Initial attributes to save.
|
23
|
+
def self.create(params = {})
|
24
|
+
new(params).tap(&:save)
|
25
|
+
end
|
26
|
+
|
27
|
+
# Find a resource by its given ID.
|
28
|
+
#
|
29
|
+
# @param id [String] ID provided by EmailOctopus for this resource.
|
30
|
+
def self.find(id)
|
31
|
+
new(id: id).tap(&:persisted?)
|
32
|
+
end
|
33
|
+
|
34
|
+
# Start a new query on a group of model resources.
|
35
|
+
#
|
36
|
+
# @return [Query] for this model class.
|
37
|
+
def self.all
|
38
|
+
Query.new self
|
39
|
+
end
|
40
|
+
|
41
|
+
# Delegate all class method calls that aren't defined on the model
|
42
|
+
# class to its +Query+.
|
43
|
+
#
|
44
|
+
# @param method [Symbol] Name of the method to call.
|
45
|
+
# @param arguments [Array] Arguments to pass to the method.
|
46
|
+
# @throws [NoMethodError] when method not defined on +all+
|
47
|
+
# @return [Query]
|
48
|
+
def self.method_missing(method, *arguments)
|
49
|
+
return super unless respond_to? method
|
50
|
+
all.public_send method, *arguments
|
51
|
+
end
|
52
|
+
|
53
|
+
# Define a new attribute method on the model that reads from the
|
54
|
+
# +attributes+ hash.
|
55
|
+
#
|
56
|
+
# @param name [Symbol] Name of the attribute
|
57
|
+
def self.attribute(name)
|
58
|
+
define_method name do
|
59
|
+
attributes[name.to_s]
|
60
|
+
end
|
61
|
+
|
62
|
+
define_method "#{name}=" do |value|
|
63
|
+
attributes[name.to_s] = value
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
#
|
68
|
+
def persisted?
|
69
|
+
id.present? && reload!
|
70
|
+
end
|
71
|
+
|
72
|
+
# Run validations and persist to the database.
|
73
|
+
def save
|
74
|
+
valid? && persist!
|
75
|
+
end
|
76
|
+
|
77
|
+
def as_json
|
78
|
+
attributes
|
79
|
+
end
|
80
|
+
|
81
|
+
def to_json
|
82
|
+
as_json.to_json
|
83
|
+
end
|
84
|
+
|
85
|
+
def destroy
|
86
|
+
@api.delete(base_path).success?
|
87
|
+
end
|
88
|
+
|
89
|
+
def reload!
|
90
|
+
@attributes = @api.get(path).body
|
91
|
+
end
|
92
|
+
|
93
|
+
private
|
94
|
+
|
95
|
+
def create
|
96
|
+
return true unless new_record?
|
97
|
+
@api.post(base_url, to_json).success?
|
98
|
+
end
|
99
|
+
|
100
|
+
def update
|
101
|
+
return true unless persisted?
|
102
|
+
@api.patch(url, to_json).success?
|
103
|
+
end
|
104
|
+
|
105
|
+
def persist!
|
106
|
+
create || update
|
107
|
+
end
|
108
|
+
|
109
|
+
def base_path
|
110
|
+
"/#{model_name.collection}"
|
111
|
+
end
|
112
|
+
|
113
|
+
def path
|
114
|
+
"#{base_path}/#{id}"
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module EmailOctopus
|
4
|
+
# Runs a query on model data.
|
5
|
+
class Query
|
6
|
+
def initialize(model)
|
7
|
+
@model = model
|
8
|
+
@api = API.new(EmailOctopus.config.api_key)
|
9
|
+
end
|
10
|
+
|
11
|
+
def limit(num)
|
12
|
+
@limit = num
|
13
|
+
self
|
14
|
+
end
|
15
|
+
|
16
|
+
def page(num)
|
17
|
+
@page = num
|
18
|
+
self
|
19
|
+
end
|
20
|
+
|
21
|
+
def each
|
22
|
+
results.each { |result| yield result }
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def results
|
28
|
+
@api.get(path).body['data'].map { |params| @model.new(params) }
|
29
|
+
end
|
30
|
+
|
31
|
+
def path
|
32
|
+
"/#{@model.resource_name}?#{params}"
|
33
|
+
end
|
34
|
+
|
35
|
+
def params
|
36
|
+
URI.encode_www_form(attributes)
|
37
|
+
end
|
38
|
+
|
39
|
+
def attributes
|
40
|
+
{
|
41
|
+
limit: @limit,
|
42
|
+
page: @page
|
43
|
+
}.each_with_object({}) do |memo, (key, val)|
|
44
|
+
memo[key] = val unless val.nil?
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
metadata
ADDED
@@ -0,0 +1,126 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: email_octopus
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Tom Scott
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-12-05 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.12'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.12'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '5.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '5.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: activemodel
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: API client for Email Octopus
|
70
|
+
email:
|
71
|
+
- tscott@weblinc.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rubocop.yml"
|
78
|
+
- ".ruby-version"
|
79
|
+
- ".travis.yml"
|
80
|
+
- CODE_OF_CONDUCT.md
|
81
|
+
- Gemfile
|
82
|
+
- LICENSE.txt
|
83
|
+
- README.md
|
84
|
+
- Rakefile
|
85
|
+
- bin/console
|
86
|
+
- bin/setup
|
87
|
+
- email_octopus.gemspec
|
88
|
+
- lib/email_octopus.rb
|
89
|
+
- lib/email_octopus/api.rb
|
90
|
+
- lib/email_octopus/api/error.rb
|
91
|
+
- lib/email_octopus/api/error/api_key_invalid.rb
|
92
|
+
- lib/email_octopus/api/error/invalid_parameters.rb
|
93
|
+
- lib/email_octopus/api/error/not_found.rb
|
94
|
+
- lib/email_octopus/api/error/unauthorized.rb
|
95
|
+
- lib/email_octopus/api/response.rb
|
96
|
+
- lib/email_octopus/campaign.rb
|
97
|
+
- lib/email_octopus/contact.rb
|
98
|
+
- lib/email_octopus/list.rb
|
99
|
+
- lib/email_octopus/model.rb
|
100
|
+
- lib/email_octopus/query.rb
|
101
|
+
- lib/email_octopus/version.rb
|
102
|
+
homepage: https://github.com/tubbo/email_octopus
|
103
|
+
licenses:
|
104
|
+
- MIT
|
105
|
+
metadata: {}
|
106
|
+
post_install_message:
|
107
|
+
rdoc_options: []
|
108
|
+
require_paths:
|
109
|
+
- lib
|
110
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
111
|
+
requirements:
|
112
|
+
- - ">="
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: '0'
|
115
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
116
|
+
requirements:
|
117
|
+
- - ">="
|
118
|
+
- !ruby/object:Gem::Version
|
119
|
+
version: '0'
|
120
|
+
requirements: []
|
121
|
+
rubyforge_project:
|
122
|
+
rubygems_version: 2.5.1
|
123
|
+
signing_key:
|
124
|
+
specification_version: 4
|
125
|
+
summary: API Client for Email Octopus
|
126
|
+
test_files: []
|