macaco 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -18,3 +18,4 @@ tmp
18
18
  .env
19
19
  .ruby-version
20
20
  .ruby-gemset
21
+ fixtures/vcr_cassettes/*
data/.travis.yml ADDED
@@ -0,0 +1,11 @@
1
+ language: ruby
2
+ cache: bundler
3
+ rvm:
4
+ - 1.9.2
5
+ - 1.9.3
6
+ - 2.0.0
7
+ - jruby-19mode
8
+ - rbx-19mode
9
+ env:
10
+ global:
11
+ secure: RRPan9WyIeNkwnDDHlmL+BgwSVP7dLcdUc7ZBQ4MF/XWH6dbkkTu5wDGxRnnI/Rjuwz1GKBpbV74La7X/3QrAQeQIUu5B/jdj2CgZvrTt2ngsbsPcLqLJXd6cCIUqBt125tkyPMDn3n/s1flloVvTLYRWZHwG0jOZiVy5rEdf9Q=
data/Gemfile CHANGED
@@ -4,4 +4,7 @@ gemspec
4
4
 
5
5
  group :test do
6
6
  gem 'minitest'
7
+ gem 'webmock'
8
+ gem 'vcr'
9
+ gem 'rake'
7
10
  end
data/README.md CHANGED
@@ -1,6 +1,7 @@
1
1
  # Macaco
2
+ [![Build Status](https://travis-ci.org/jamesduncombe/macaco.png?branch=master)](https://travis-ci.org/jamesduncombe/macaco)
2
3
 
3
- Tiny wrapper around [Mandrill's API](https://mandrillapp.com/api/docs/)
4
+ Tiny wrapper around (for now) [Mandrill API's send method](https://mandrillapp.com/api/docs/messages.JSON.html#method=send). Later to be expanded to handle Sendgrid's send method too.
4
5
 
5
6
  ## Installation
6
7
 
@@ -18,8 +19,11 @@ Or install it yourself as:
18
19
 
19
20
  ## Usage
20
21
 
21
- You can configure Macaco to use an API by passing a block to the
22
- configure method. Here I've used an environment variable:
22
+ By default Macaco tries to find your mail API key with the `MACACO_API_KEY`
23
+ environment variable.
24
+
25
+ However, you can configure Macaco to use a different key by passing a block to the
26
+ configure method. Here I've used a different environment variable:
23
27
 
24
28
  ```ruby
25
29
  Macaco.configure do |config|
@@ -29,30 +33,29 @@ end
29
33
 
30
34
  At the moment we just have the [send message](https://mandrillapp.com/api/docs/messages.JSON.html#method=send) method. Use it like this:
31
35
 
32
- Form your data hash:
36
+ First create a new mail object:
33
37
 
34
38
  ```ruby
35
- data = {
36
- message: {
37
- text: 'Boom boom',
38
- html: '''
39
- <h1>My email title</h1>
40
- <p>My email body</p>
41
- <small>Monkey Corp Ltd</small>
42
- ''',
43
- subject: 'Test subject',
44
- from_email: 'james@jamesduncombe.com',
45
- to: [{ email: 'james@jamesduncombe.com' }]
46
- }
47
- }
39
+ mail = Macaco::Mandrill.new do
40
+ to 'to@test.com'
41
+ from 'from@test.com'
42
+ subject 'This is my subject'
43
+ body_html '<h1>This is a title</h1>'
44
+ body_text 'This is my text version'
45
+ end
48
46
  ```
49
47
 
50
48
  Then call the method:
51
49
 
52
50
  ```ruby
53
- Macaco::Messages.send_message(data)
51
+ mail.send
54
52
  ```
55
53
 
54
+ ## Todo
55
+
56
+ - Add further reflection methods to be able to inspect the mail object
57
+ - Add support for Sendgrid's send method
58
+
56
59
 
57
60
  ## Contributing
58
61
 
@@ -61,3 +64,27 @@ Macaco::Messages.send_message(data)
61
64
  3. Commit your changes (`git commit -am 'Add some feature'`)
62
65
  4. Push to the branch (`git push origin my-new-feature`)
63
66
  5. Create new Pull Request
67
+
68
+ ## License
69
+
70
+ The MIT License (MIT)
71
+
72
+ Copyright (c) 2013 James Duncombe
73
+
74
+ Permission is hereby granted, free of charge, to any person obtaining a copy
75
+ of this software and associated documentation files (the "Software"), to deal
76
+ in the Software without restriction, including without limitation the rights
77
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
78
+ copies of the Software, and to permit persons to whom the Software is
79
+ furnished to do so, subject to the following conditions:
80
+
81
+ The above copyright notice and this permission notice shall be included in all
82
+ copies or substantial portions of the Software.
83
+
84
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
85
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
86
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
87
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
88
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
89
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
90
+ SOFTWARE.
data/Rakefile CHANGED
@@ -1,2 +1,9 @@
1
1
  require "bundler/gem_tasks"
2
+ require "rake/testtask"
2
3
 
4
+ Rake::TestTask.new do |t|
5
+ t.libs << "spec"
6
+ t.test_files = FileList["spec/**/*_spec.rb"]
7
+ end
8
+
9
+ task :default => [:test]
data/bin/rake ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # This file was generated by Bundler.
4
+ #
5
+ # The application 'rake' is installed as part of a gem, and
6
+ # this file is here to facilitate running it.
7
+ #
8
+
9
+ require 'pathname'
10
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
11
+ Pathname.new(__FILE__).realpath)
12
+
13
+ require 'rubygems'
14
+ require 'bundler/setup'
15
+
16
+ load Gem.bin_path('macaco', 'rake')
data/lib/macaco.rb CHANGED
@@ -3,7 +3,9 @@ require 'json'
3
3
  require 'open-uri'
4
4
 
5
5
  require 'macaco/version'
6
- require 'macaco/messages'
6
+ require 'macaco/api'
7
+ require 'macaco/senders/sender'
8
+ require 'macaco/senders/mandrill'
7
9
 
8
10
  module Macaco
9
11
  class << self
@@ -16,12 +18,11 @@ module Macaco
16
18
  end
17
19
 
18
20
  class Configuration
19
- attr_accessor :api_key, :api_root, :api_port
21
+ attr_accessor :api_key, :sender
20
22
 
21
23
  def initialize
22
- @api_key = ENV['MANDRILL_API_KEY']
23
- @api_root = 'mandrillapp.com'
24
- @api_port = 443
24
+ @api_key = ENV['MACACO_API_KEY']
25
+ @sender = :mandrill
25
26
  end
26
27
  end
27
- end
28
+ end
data/lib/macaco/api.rb ADDED
@@ -0,0 +1,30 @@
1
+ module Macaco
2
+ class Api
3
+
4
+ def self.post(args = {})
5
+
6
+ request = request_instance(args)
7
+ request.body = args[:data].to_json
8
+
9
+ JSON.parse(http_response(request, args).body)
10
+
11
+ end
12
+
13
+ private
14
+
15
+ def self.http_response(request, args)
16
+ http_instance(args).start { |http| http.request(request) }
17
+ end
18
+
19
+ def self.request_instance(args)
20
+ Net::HTTP::Post.new(args[:mail].api_path, initheader = { 'Content-Type' => 'application/json' })
21
+ end
22
+
23
+ def self.http_instance(args)
24
+ http = Net::HTTP.new(args[:mail].api_root, 443)
25
+ http.use_ssl = true
26
+ http
27
+ end
28
+
29
+ end
30
+ end
@@ -0,0 +1,34 @@
1
+ module Macaco
2
+ class Mandrill < Sender
3
+
4
+ def api_key
5
+ Macaco.config.api_key || ENV['MACACO_API_KEY']
6
+ end
7
+
8
+ def api_root
9
+ 'mandrillapp.com'
10
+ end
11
+
12
+ def api_path
13
+ '/api/1.0/messages/send.json'
14
+ end
15
+
16
+ def to_hash
17
+ {
18
+ message: {
19
+ from_email: @from,
20
+ to: @to,
21
+ subject: @subject,
22
+ html: @body_html,
23
+ text: @body_text
24
+ }
25
+ }
26
+ end
27
+
28
+ def send
29
+ data = to_hash.merge!({ key: api_key })
30
+ Macaco::Api.post({ mail: self, data: data })
31
+ end
32
+
33
+ end
34
+ end
@@ -0,0 +1,64 @@
1
+ module Macaco
2
+ class Sender
3
+
4
+ def initialize(*args, &block)
5
+ @body_html = nil
6
+ @body_text = nil
7
+ @text = nil
8
+ @to = []
9
+ @from = nil
10
+ @subject = nil
11
+
12
+ if args.first.is_a? Hash
13
+ hash_attributes(args.first)
14
+ end
15
+
16
+ if block_given?
17
+ instance_eval(&block)
18
+ end
19
+
20
+ self
21
+ end
22
+
23
+ def hash_attributes(args)
24
+ to args[:to]
25
+ from args[:from]
26
+ subject args[:subject]
27
+ body_html args[:body_html]
28
+ body_text args[:body_text]
29
+ end
30
+
31
+ def to(val = nil)
32
+ return @to unless val
33
+ @to << { email: val }
34
+ end
35
+
36
+ def from(val = nil)
37
+ return @from unless val
38
+ @from ||= val
39
+ end
40
+
41
+ def subject(val = nil)
42
+ return @subject unless val
43
+ @subject ||= val
44
+ end
45
+
46
+ def body_html(val = nil)
47
+ return @body_html unless val
48
+ @body_html ||= val
49
+ end
50
+ alias_method :html, :body_html
51
+
52
+ def body_text(val = nil)
53
+ return @body_text unless val
54
+ @body_text ||= val
55
+ end
56
+ alias_method :text, :body_text
57
+
58
+ # helpers - could be split out?
59
+
60
+ def to_json
61
+ to_hash.to_json
62
+ end
63
+ end
64
+ end
@@ -1,3 +1,3 @@
1
1
  module Macaco
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/macaco.gemspec CHANGED
@@ -8,9 +8,10 @@ Gem::Specification.new do |gem|
8
8
  gem.version = Macaco::VERSION
9
9
  gem.authors = ["James Duncombe"]
10
10
  gem.email = ["james@jamesduncombe.com"]
11
- gem.summary = %q{A tiny gem to handle Mandrill's API}
12
- gem.description = %q{Tiny gem to handle Mandrill's API without other gem dependencies}
11
+ gem.summary = %q{Tiny wrapper around Mandrill API's send method}
12
+ gem.description = %q{Tiny gem to wrap Mandrill API's send method without other gem dependencies}
13
13
  gem.homepage = "https://github.com/jamesduncombe/macaco"
14
+ gem.license = 'MIT'
14
15
 
15
16
  gem.files = `git ls-files`.split($/)
16
17
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
@@ -0,0 +1,43 @@
1
+ require 'spec_helper'
2
+
3
+ describe Macaco::Mandrill do
4
+
5
+ let(:mail) do
6
+ Macaco::Mandrill.new do
7
+ to 'to@test.com'
8
+ from 'from@test.com'
9
+ subject 'Subject for my email'
10
+ body_html '<h1>This is a header for the HTML version</h1>'
11
+ body_text 'This is the Text version'
12
+ end
13
+ end
14
+
15
+ describe '#api_root' do
16
+ it { Macaco::Mandrill.new.api_root.must_equal 'mandrillapp.com' }
17
+ end
18
+
19
+ describe '#api_path' do
20
+ it { Macaco::Mandrill.new.api_path.must_equal '/api/1.0/messages/send.json' }
21
+ end
22
+
23
+ describe '#to_hash' do
24
+ subject { mail.to_hash }
25
+ it { subject.must_be_kind_of Hash }
26
+ end
27
+
28
+ describe '#to_json' do
29
+ it 'converts the mandrill hash into a JSON string' do
30
+ mail.to_json.must_be_kind_of String
31
+ end
32
+ end
33
+
34
+ describe '#send' do
35
+ subject do
36
+ VCR.use_cassette('send') do
37
+ mail.send
38
+ end
39
+ end
40
+ it { subject.must_be_kind_of Array }
41
+ it { subject.first['status'].must_equal 'sent' }
42
+ end
43
+ end
@@ -0,0 +1,56 @@
1
+ require 'spec_helper'
2
+
3
+ describe Macaco::Sender do
4
+
5
+ let(:mail) do
6
+ Macaco::Sender.new do
7
+ to 'to@test.com'
8
+ from 'from@test.com'
9
+ subject 'Subject for my email'
10
+ body_html '<h1>This is a header for the HTML version</h1>'
11
+ body_text 'This is the Text version'
12
+ end
13
+ end
14
+
15
+ describe '#hash_attributes' do
16
+ it 'creates a new mail record via hash' do
17
+ m = Macaco::Sender.new( from: 'from@test.com', to: 'to@test.com', subject: 'Test' )
18
+ m.from.must_equal 'from@test.com'
19
+ end
20
+ end
21
+
22
+ describe '#to' do
23
+ it 'sets the recipient if theres a string provided' do
24
+ mail.to.must_equal [ { email: 'to@test.com' } ]
25
+ end
26
+ end
27
+
28
+ describe '#from' do
29
+ it 'sets the from address' do
30
+ mail.from.must_equal 'from@test.com'
31
+ end
32
+ end
33
+
34
+ describe '#subject' do
35
+ it 'sets the subject address' do
36
+ mail.subject.must_equal 'Subject for my email'
37
+ end
38
+ end
39
+
40
+ describe '#body_html' do
41
+ it 'sets the body html' do
42
+ n = Macaco::Sender.new
43
+ n.body_html '<h1>This is a test</h1>'
44
+ n.body_html.must_equal '<h1>This is a test</h1>'
45
+ end
46
+ end
47
+
48
+ describe '#body_text' do
49
+ it 'sets the body text' do
50
+ n = Macaco::Sender.new
51
+ n.body_text 'This is a test'
52
+ n.body_text.must_equal 'This is a test'
53
+ end
54
+ end
55
+
56
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,7 +1,18 @@
1
- ENV['RACK_ENV'] = 'test'
1
+ require 'rubygems'
2
2
  require 'bundler'
3
3
  Bundler.require
4
4
  require 'minitest/autorun'
5
5
  require 'minitest/pride'
6
6
 
7
- require './lib/macaco'
7
+ require 'vcr'
8
+
9
+ require 'macaco'
10
+
11
+ VCR.configure do |c|
12
+ c.cassette_library_dir = 'fixtures/vcr_cassettes'
13
+ c.hook_into :webmock
14
+ end
15
+
16
+ Macaco.configure do |config|
17
+ config.api_key = ENV['MANDRILL_API_KEY']
18
+ end
metadata CHANGED
@@ -1,58 +1,68 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: macaco
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
+ prerelease:
5
6
  platform: ruby
6
7
  authors:
7
8
  - James Duncombe
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2013-10-27 00:00:00.000000000 Z
12
+ date: 2013-11-10 00:00:00.000000000 Z
12
13
  dependencies: []
13
- description: Tiny gem to handle Mandrill's API without other gem dependencies
14
+ description: Tiny gem to wrap Mandrill API's send method without other gem dependencies
14
15
  email:
15
16
  - james@jamesduncombe.com
16
- executables: []
17
+ executables:
18
+ - rake
17
19
  extensions: []
18
20
  extra_rdoc_files: []
19
21
  files:
20
22
  - .gitignore
23
+ - .travis.yml
21
24
  - Gemfile
22
25
  - LICENSE.txt
23
26
  - README.md
24
27
  - Rakefile
28
+ - bin/rake
25
29
  - lib/macaco.rb
26
- - lib/macaco/messages.rb
30
+ - lib/macaco/api.rb
31
+ - lib/macaco/senders/mandrill.rb
32
+ - lib/macaco/senders/sender.rb
27
33
  - lib/macaco/version.rb
28
34
  - macaco.gemspec
29
- - spec/lib/macaco/messages_spec.rb
35
+ - spec/lib/macaco/senders/mandrill_spec.rb
36
+ - spec/lib/macaco/senders/sender_spec.rb
30
37
  - spec/lib/macaco_spec.rb
31
38
  - spec/spec_helper.rb
32
39
  homepage: https://github.com/jamesduncombe/macaco
33
- licenses: []
34
- metadata: {}
40
+ licenses:
41
+ - MIT
35
42
  post_install_message:
36
43
  rdoc_options: []
37
44
  require_paths:
38
45
  - lib
39
46
  required_ruby_version: !ruby/object:Gem::Requirement
47
+ none: false
40
48
  requirements:
41
- - - '>='
49
+ - - ! '>='
42
50
  - !ruby/object:Gem::Version
43
51
  version: '0'
44
52
  required_rubygems_version: !ruby/object:Gem::Requirement
53
+ none: false
45
54
  requirements:
46
- - - '>='
55
+ - - ! '>='
47
56
  - !ruby/object:Gem::Version
48
57
  version: '0'
49
58
  requirements: []
50
59
  rubyforge_project:
51
- rubygems_version: 2.0.3
60
+ rubygems_version: 1.8.25
52
61
  signing_key:
53
- specification_version: 4
54
- summary: A tiny gem to handle Mandrill's API
62
+ specification_version: 3
63
+ summary: Tiny wrapper around Mandrill API's send method
55
64
  test_files:
56
- - spec/lib/macaco/messages_spec.rb
65
+ - spec/lib/macaco/senders/mandrill_spec.rb
66
+ - spec/lib/macaco/senders/sender_spec.rb
57
67
  - spec/lib/macaco_spec.rb
58
68
  - spec/spec_helper.rb
checksums.yaml DELETED
@@ -1,7 +0,0 @@
1
- ---
2
- SHA1:
3
- metadata.gz: cfd7cbd8435ea89f75b2cd6cadfdcad5b36b3a6d
4
- data.tar.gz: 55f493eb3e10f825f35fdfbb588926dd3165c13c
5
- SHA512:
6
- metadata.gz: 535d25ad21a393b6568396a329909026a9cb3f244f1e8f688c9391d5cfa1701b8bc00bf31cc8a8c5fc299faac086e817f0e1eb1dceb836601a423c2f8770bb06
7
- data.tar.gz: f1eaf76c759b8f9883c2cc9a27e72eb5d32457bcb75ba32b7dbd1dbaabca86565147a2997c2148511c3ded7ea0dc6e6a84ae4d3b04ede20e6ae910a9c1967036
@@ -1,22 +0,0 @@
1
- module Macaco
2
- class Messages
3
-
4
- def self.send_message(data)
5
-
6
- data.merge!({ key: Macaco.config.api_key })
7
-
8
- request = Net::HTTP::Post.new('/api/1.0/messages/send.json',
9
- initheader = { 'Content-Type'=> 'application/json' })
10
- request.body = data.to_json
11
- http = Net::HTTP.new(Macaco.config.api_root, Macaco.config.api_port)
12
- http.use_ssl = true
13
-
14
- response = http.start do |http|
15
- http.request(request)
16
- end
17
-
18
- JSON.parse(response.body)
19
-
20
- end
21
- end
22
- end
@@ -1,28 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Macaco::Messages do
4
-
5
- describe '#send_message' do
6
- subject do
7
- data = {
8
- message: {
9
- text: 'Boom boom',
10
- html: '''
11
- <h1>My email title</h1>
12
- <p>My email body</p>
13
- <small>Monkey Corp Ltd</small>
14
- ''',
15
- subject: 'Test subject',
16
- from_email: 'james@jamesduncombe.com',
17
- to: [{ email: 'james@jamesduncombe.com' }]
18
- }
19
- }
20
- Macaco.configure do |config|
21
- config.api_key = ENV['MANDRILL_API_KEY']
22
- end
23
- Macaco::Messages.send_message(data)
24
- end
25
- it { subject.must_be_kind_of Array }
26
- it { subject.first['status'].must_equal 'sent' }
27
- end
28
- end