mailerlite 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: efd9c3192e8b0b5803b0e38ad265b850315cc347
4
+ data.tar.gz: dbd161446be6234bd65db1eda6c06cea96d8dcfc
5
+ SHA512:
6
+ metadata.gz: 5bdacde3271be3b651fdb21a664585f3b4fc2d33cb2bc59899fcdb7b777784768956710c9d42ed53d2019645ebd264740694e12b14ee914e7ecefc8d6dd82bff
7
+ data.tar.gz: e26de30288431b82a8c099ac20428db695947420b878ea1a72c799be73cf68f8ed4646b4d7ec46c314e7ba6d921eb04a80c5c565ccc4a73573a6f0de9c6fe596
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,25 @@
1
+ sudo: false
2
+
3
+ before_install:
4
+ - gem install bundler
5
+
6
+ bundler_args: --without development
7
+
8
+ language: ruby
9
+
10
+ rvm:
11
+ - 1.9.3
12
+ - 2.0.0
13
+ - 2.1.0
14
+ - 2.2.0
15
+ - 2.3.0
16
+ - ruby-head
17
+ - jruby-19mode
18
+ - jruby-head
19
+ - rbx
20
+
21
+ matrix:
22
+ allow_failures:
23
+ - rvm: ruby-head
24
+ - rvm: jruby-head
25
+ - rvm: rbx
data/Gemfile ADDED
@@ -0,0 +1,15 @@
1
+ source 'https://rubygems.org'
2
+
3
+ group :development, :test do
4
+ gem 'pry'
5
+ end
6
+
7
+ group :test do
8
+ gem 'codeclimate-test-reporter', require: false
9
+ gem 'coveralls', '~> 0.8.13', require: false
10
+ gem 'guard-rspec', '~> 4.6.5'
11
+ gem 'simplecov', '~> 0.11.2', require: false
12
+ gem 'webmock', '~> 1.24.3'
13
+ end
14
+
15
+ gemspec
data/README.md ADDED
@@ -0,0 +1,52 @@
1
+ # MailerLite API Ruby wrapper
2
+
3
+ A Ruby gem which helps to communicate with [MailerLite][mailerlite] API.
4
+
5
+ [![Gem Version](http://img.shields.io/gem/v/mailerlite.svg?style=flat-square)][rubygems]
6
+ [![Build Status](http://img.shields.io/travis/jpalumickas/mailerlite-ruby.svg?style=flat-square)][travis]
7
+ [![Dependency Status](http://img.shields.io/gemnasium/jpalumickas/mailerlite-ruby.svg?style=flat-square)][gemnasium]
8
+ [![Coverage Status](http://img.shields.io/coveralls/jpalumickas/mailerlite-ruby/master.svg?style=flat-square)][coveralls]
9
+ [![Code Climate](http://img.shields.io/codeclimate/github/jpalumickas/mailerlite-ruby.svg?style=flat-square)][codeclimate]
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'mailerlite'
17
+ ```
18
+
19
+ ## Usage
20
+
21
+ ### Authentication
22
+
23
+ ```ruby
24
+ client = MailerLite::Client.new(api_key: 'my-secret-api-key')
25
+ ```
26
+
27
+ Or create file under `config/initializers/mailerlite.rb`
28
+
29
+ ```ruby
30
+ MailerLite.configure do |config|
31
+ config.api_key = 'my-secret-api-key'
32
+ end
33
+ ```
34
+
35
+ ## Supported Ruby Versions
36
+
37
+ This library aims to support and is [tested against][travis] the following Ruby
38
+ implementations:
39
+
40
+ * Ruby 1.9.3
41
+ * Ruby 2.0.0
42
+ * Ruby 2.1.0
43
+ * Ruby 2.2.0
44
+ * Ruby 2.3.0
45
+
46
+ [rubygems]: https://rubygems.org/gems/mailerlite
47
+ [travis]: http://travis-ci.org/jpalumickas/mailerlite-ruby
48
+ [gemnasium]: https://gemnasium.com/jpalumickas/mailerlite-ruby
49
+ [coveralls]: https://coveralls.io/r/jpalumickas/mailerlite-ruby
50
+ [codeclimate]: https://codeclimate.com/github/jpalumickas/mailerlite-ruby
51
+
52
+ [mailerlite]: https://www.mailerlite.com
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "mailerlite"
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,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,27 @@
1
+ require 'mailerlite/connection'
2
+ require 'mailerlite/configuration'
3
+
4
+ require 'mailerlite/clients/lists'
5
+
6
+ module MailerLite
7
+ class Client
8
+ include MailerLite::Clients::Lists
9
+
10
+ def initialize(options = {})
11
+ config.api_key = options[:api_key] if options[:api_key]
12
+ end
13
+
14
+ def config
15
+ @config ||= Configuration.new
16
+ end
17
+
18
+ def configure
19
+ yield(config) if block_given?
20
+ true
21
+ end
22
+
23
+ def connection
24
+ @connection ||= Connection.new(self)
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,37 @@
1
+ module MailerLite
2
+ module Clients
3
+ module Lists
4
+ def lists(options = {})
5
+ connection.get('lists/', options)
6
+ end
7
+
8
+ def list(id)
9
+ connection.get("lists/#{id}/")
10
+ end
11
+
12
+ def create_list(name)
13
+ connection.post('lists/', name: name)
14
+ end
15
+
16
+ def update_list(id, name)
17
+ connection.post("lists/#{id}/", name: name)
18
+ end
19
+
20
+ def delete_list(id)
21
+ connection.delete("lists/#{id}/")
22
+ end
23
+
24
+ def list_active_subscribers(id, options = {})
25
+ connection.get("lists/#{id}/active/", options)
26
+ end
27
+
28
+ def list_unsubscribed_subscribers(id, options = {})
29
+ connection.get("lists/#{id}/unsubscribed/", options)
30
+ end
31
+
32
+ def list_bounced_subscribers(id, options = {})
33
+ connection.get("lists/#{id}/bounced/", options)
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,21 @@
1
+ require 'faraday_middleware'
2
+
3
+ module MailerLite
4
+ class Configuration
5
+ # Default API endpoint
6
+ API_ENDPOINT = 'https://app.mailerlite.com/api/v1'.freeze
7
+
8
+ # Default User Agent header string
9
+ USER_AGENT = "MailerLite Ruby v#{MailerLite::VERSION}".freeze
10
+
11
+ attr_accessor :api_key
12
+
13
+ def url
14
+ API_ENDPOINT
15
+ end
16
+
17
+ def user_agent
18
+ USER_AGENT
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,71 @@
1
+ require 'faraday'
2
+ require 'json'
3
+
4
+ require 'mailerlite/middleware/raise_error'
5
+ require 'mailerlite/middleware/underscore_keys'
6
+
7
+ module MailerLite
8
+ class Connection
9
+ attr_reader :client
10
+
11
+ def initialize(client)
12
+ @client = client
13
+ end
14
+
15
+ def get(path, options = {})
16
+ request(:get, path, options).body
17
+ end
18
+
19
+ def delete(path, options = {})
20
+ request(:delete, path, options).body
21
+ end
22
+
23
+ def post(path, options = {})
24
+ options['apiKey'] = client.config.api_key
25
+
26
+ response = connection.post do |req|
27
+ req.url(path)
28
+ req.headers['Content-Type'] = 'application/json'
29
+ req.body = options.to_json
30
+ end
31
+
32
+ response.body
33
+ end
34
+
35
+ private
36
+
37
+ def request(method, path, options = {})
38
+ options['apiKey'] = client.config.api_key
39
+
40
+ response = connection.send(method) do |request|
41
+ request.url(path, options)
42
+ end
43
+
44
+ response
45
+ end
46
+
47
+ def connection
48
+ conn_opts = {
49
+ headers: { user_agent: client.config.user_agent },
50
+ url: client.config.url,
51
+ builder: middleware
52
+ }
53
+
54
+ Faraday.new(conn_opts)
55
+ end
56
+
57
+ def middleware
58
+ @middleware ||= Faraday::RackBuilder.new do |builder|
59
+ builder.request :json
60
+
61
+ builder.use FaradayMiddleware::FollowRedirects
62
+ builder.use FaradayMiddleware::Mashify
63
+ builder.use MailerLite::Middleware::UnderscoreKeys
64
+ builder.use FaradayMiddleware::ParseJson
65
+ builder.use MailerLite::Middleware::RaiseError
66
+
67
+ builder.adapter Faraday.default_adapter
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,9 @@
1
+ class String
2
+ def underscore
3
+ self.gsub(/::/, '/').
4
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
5
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
6
+ tr("-", "_").
7
+ downcase
8
+ end
9
+ end
@@ -0,0 +1,42 @@
1
+ module MailerLite
2
+ class Error < StandardError
3
+ # Returns the appropriate MailerLite::Error sublcass based
4
+ # on status and response message.
5
+ #
6
+ # response - The Hash of HTTP response.
7
+ #
8
+ # Returns the MailerLite::Error.
9
+ def self.from_response(response)
10
+ status = response[:status].to_i
11
+
12
+ klass = case status
13
+ when 400 then MailerLite::BadRequest
14
+ when 401 then MailerLite::Unauthorized
15
+ when 404 then MailerLite::NotFound
16
+ end
17
+
18
+ klass.new if klass
19
+ end
20
+ end
21
+
22
+ # Raised when MailerLite returns a 400 HTTP status code
23
+ class BadRequest < Error
24
+ def to_s
25
+ 'Missing a required parameter or calling invalid method'
26
+ end
27
+ end
28
+
29
+ # Raised when MailerLite returns a 401 HTTP status code
30
+ class Unauthorized < Error
31
+ def to_s
32
+ 'Invalid API key provided'
33
+ end
34
+ end
35
+
36
+ # Raised when MailerLite returns a 404 HTTP status code
37
+ class NotFound < Error
38
+ def to_s
39
+ "Can't find requested items"
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,15 @@
1
+ module MailerLite
2
+ # Faraday response middleware
3
+ module Middleware
4
+ # This class raises an exception based HTTP status codes returned
5
+ # by the API.
6
+ class RaiseError < Faraday::Response::Middleware
7
+ private
8
+
9
+ def on_complete(response)
10
+ error = MailerLite::Error.from_response(response)
11
+ raise error if error
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,26 @@
1
+ module MailerLite
2
+ module Middleware
3
+ # This class will underscore all response keys from CamelCase.
4
+ class UnderscoreKeys < Faraday::Response::Middleware
5
+ private
6
+
7
+ def on_complete(response)
8
+ response[:body] = updated_response(response[:body])
9
+ end
10
+
11
+ def updated_response(response)
12
+ if response.is_a?(Hash)
13
+ underscore_hash_keys(response)
14
+ else
15
+ response
16
+ end
17
+ end
18
+
19
+ def underscore_hash_keys(hash)
20
+ hash.each_with_object({}) do |(k, v), new_hash|
21
+ new_hash[k.underscore] = updated_response(v)
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,3 @@
1
+ module MailerLite
2
+ VERSION = '0.1.0'.freeze
3
+ end
data/lib/mailerlite.rb ADDED
@@ -0,0 +1,22 @@
1
+ require 'mailerlite/core_ext/string'
2
+
3
+ require 'mailerlite/version'
4
+ require 'mailerlite/error'
5
+ require 'mailerlite/client'
6
+
7
+ require 'faraday'
8
+
9
+ module MailerLite
10
+ class << self
11
+ def client
12
+ @client ||= MailerLite::Client.new
13
+ end
14
+
15
+ private
16
+
17
+ def method_missing(method_name, *args, &block)
18
+ return super unless client.respond_to?(method_name)
19
+ client.send(method_name, *args, &block)
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mailerlite/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'mailerlite'
8
+ spec.version = MailerLite::VERSION
9
+ spec.authors = ['Justas Palumickas']
10
+ spec.email = ['jpalumickas@gmail.com']
11
+
12
+ spec.summary = 'Ruby wrapper for MailerLite API'
13
+ spec.description = 'Unofficial ruby gem for MailerLite API'
14
+ spec.homepage = 'https://github.com/jpalumickas/mailerlite-ruby'
15
+
16
+ spec.files = `git ls-files -z`
17
+ .split("\x0")
18
+ .reject { |f| f.match(%r{^(test|spec|features)/}) }
19
+
20
+ spec.bindir = 'exe'
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ['lib']
23
+
24
+ spec.add_dependency 'faraday', '~> 0.9.2'
25
+ spec.add_dependency 'faraday_middleware', '~> 0.10.0'
26
+ spec.add_dependency 'hashie', '~> 3.4'
27
+
28
+ spec.add_development_dependency 'bundler', '~> 1.11'
29
+ spec.add_development_dependency 'rake', '~> 10.0'
30
+ spec.add_development_dependency 'rspec', '~> 3.4'
31
+ end
metadata ADDED
@@ -0,0 +1,146 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mailerlite
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Justas Palumickas
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-04-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.9.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.9.2
27
+ - !ruby/object:Gem::Dependency
28
+ name: faraday_middleware
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.10.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.10.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: hashie
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.4'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.4'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.11'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.11'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.4'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.4'
97
+ description: Unofficial ruby gem for MailerLite API
98
+ email:
99
+ - jpalumickas@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - ".travis.yml"
107
+ - Gemfile
108
+ - README.md
109
+ - Rakefile
110
+ - bin/console
111
+ - bin/setup
112
+ - lib/mailerlite.rb
113
+ - lib/mailerlite/client.rb
114
+ - lib/mailerlite/clients/lists.rb
115
+ - lib/mailerlite/configuration.rb
116
+ - lib/mailerlite/connection.rb
117
+ - lib/mailerlite/core_ext/string.rb
118
+ - lib/mailerlite/error.rb
119
+ - lib/mailerlite/middleware/raise_error.rb
120
+ - lib/mailerlite/middleware/underscore_keys.rb
121
+ - lib/mailerlite/version.rb
122
+ - mailerlite.gemspec
123
+ homepage: https://github.com/jpalumickas/mailerlite-ruby
124
+ licenses: []
125
+ metadata: {}
126
+ post_install_message:
127
+ rdoc_options: []
128
+ require_paths:
129
+ - lib
130
+ required_ruby_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ required_rubygems_version: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - ">="
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ requirements: []
141
+ rubyforge_project:
142
+ rubygems_version: 2.6.3
143
+ signing_key:
144
+ specification_version: 4
145
+ summary: Ruby wrapper for MailerLite API
146
+ test_files: []