send_with_us 0.0.6

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.
data/.gitignore ADDED
@@ -0,0 +1,23 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .DS_Store
6
+ coverage
7
+ InstalledFiles
8
+ lib/bundler/man
9
+ pkg
10
+ rdoc
11
+ spec/reports
12
+ test/tmp
13
+ test/version_tmp
14
+ tmp
15
+
16
+ # vim stuff
17
+ *.swp
18
+ *.swo
19
+
20
+ # YARD artifacts
21
+ .yardoc
22
+ _yardoc
23
+ doc/
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - "1.9.3"
data/CHANGELOG.md ADDED
@@ -0,0 +1,7 @@
1
+ 0.0.4 - Rewritten to be more gem-like
2
+ - Accepts configuration via a Rails-style initializer
3
+ - Test coverage
4
+ 0.0.3 - No idea
5
+ 0.0.2 - Full json post body
6
+ 0.0.1 - First Version
7
+
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in send_with_us.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,35 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ send_with_us (0.0.4)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ activesupport (3.2.11)
10
+ i18n (~> 0.6)
11
+ multi_json (~> 1.0)
12
+ bourne (1.1.2)
13
+ mocha (= 0.10.5)
14
+ i18n (0.6.1)
15
+ metaclass (0.0.1)
16
+ mocha (0.10.5)
17
+ metaclass (~> 0.0.1)
18
+ multi_json (1.5.0)
19
+ rake (10.0.3)
20
+ shoulda (3.3.2)
21
+ shoulda-context (~> 1.0.1)
22
+ shoulda-matchers (~> 1.4.1)
23
+ shoulda-context (1.0.2)
24
+ shoulda-matchers (1.4.2)
25
+ activesupport (>= 3.0.0)
26
+ bourne (~> 1.1.2)
27
+
28
+ PLATFORMS
29
+ ruby
30
+
31
+ DEPENDENCIES
32
+ mocha
33
+ rake
34
+ send_with_us!
35
+ shoulda
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2013 Matt Harris
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,60 @@
1
+ # sendwithus_ruby
2
+
3
+ Ruby bindings for sending email via the sendwithus API.
4
+
5
+ [sendwithus.com](http://sendwithus.com)
6
+
7
+ [![Build Status](https://api.travis-ci.org/sendwithus/sendwithus_ruby.png)](https://travis-ci.org/sendwithus/sendwithus_ruby)
8
+
9
+ ## Installation
10
+
11
+ gem install send_with_us
12
+
13
+ or with Bundler:
14
+
15
+ gem 'send_with_us'
16
+ bundle install
17
+
18
+ ## Usage
19
+
20
+ ### General
21
+
22
+ For any Ruby project:
23
+
24
+ require 'rubygems'
25
+ require 'send_with_us'
26
+
27
+ begin
28
+ obj = SendWithUs::Api.new( api_key: 'YOUR API KEY', debug: true )
29
+ result = obj.send_with('email_id', 'recipient@testco.com', { company_name: 'TestCo' })
30
+ puts result
31
+ rescue Exception => e
32
+ puts "Error - #{e.class.name}: #{e.message}"
33
+ end
34
+
35
+ ### Rails
36
+
37
+ For a Rails app, create `send_with_us.rb` in `/config/initializers/`
38
+ with the following:
39
+
40
+ SendWithUs::Api.configure do |config|
41
+ config.api_key = 'YOUR API KEY'
42
+ config.debug = true
43
+ end
44
+
45
+ In your application code where you want to send an email:
46
+
47
+ begin
48
+ result = SendWithUs::Api.new.send_with('email_id', 'recipient@testco.com', { company_name: 'TestCo' })
49
+ puts result
50
+ rescue Exception => e
51
+ puts "Error - #{e.class.name}: #{e.message}"
52
+ end
53
+
54
+ ## Errors
55
+
56
+ The following errors may be generated:
57
+
58
+ SendWithUs::ApiInvalidEndpoint - the target URI is probably incorrect
59
+ SendWithUs::ApiConnectionRefused - the target URI is probably incorrect
60
+ SendWithUs::ApiUnknownError - an unhandled HTTP error occurred
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require 'rake/testtask'
4
+
5
+ Rake::TestTask.new do |t|
6
+ t.libs << 'lib/send_with_us'
7
+ t.test_files = FileList['test/lib/send_with_us/*_test.rb']
8
+ t.verbose = true
9
+ end
10
+ task default: :test
@@ -0,0 +1,13 @@
1
+ module SendWithUs
2
+ end
3
+
4
+ require 'rubygems'
5
+ require 'net/http'
6
+ require 'net/https'
7
+ require 'uri'
8
+ require 'json'
9
+
10
+ require 'send_with_us/api'
11
+ require 'send_with_us/api_request'
12
+ require 'send_with_us/config'
13
+ require 'send_with_us/version'
@@ -0,0 +1,30 @@
1
+ module SendWithUs
2
+
3
+ class Api
4
+ attr_reader :configuration
5
+
6
+ # ------------------------------ Class Methods ------------------------------
7
+
8
+ def self.configuration
9
+ @configuration ||= SendWithUs::Config.new
10
+ end
11
+
12
+ def self.configure
13
+ yield self.configuration if block_given?
14
+ end
15
+
16
+ # ------------------------------ Instance Methods ------------------------------
17
+
18
+ def initialize(options = {})
19
+ settings = SendWithUs::Api.configuration.settings.merge(options)
20
+ @configuration = SendWithUs::Config.new(settings)
21
+ end
22
+
23
+ def send_with(email_id, to, data = {})
24
+ payload = { email_id: email_id, email_to: to, email_data: data }.to_json
25
+ SendWithUs::ApiRequest.new(@configuration).send_with(payload)
26
+ end
27
+
28
+ end
29
+
30
+ end
@@ -0,0 +1,48 @@
1
+ module SendWithUs
2
+ class ApiInvalidEndpoint < StandardError; end
3
+ class ApiConnectionRefused < StandardError; end
4
+ class ApiUnknownError < StandardError; end
5
+
6
+ class ApiRequest
7
+ attr_reader :response
8
+
9
+ def initialize(configuration)
10
+ @configuration = configuration
11
+ end
12
+
13
+ def send_with(payload)
14
+ path = request_path(:send)
15
+ request = Net::HTTP::Post.new(path, initheader = {'Content-Type' =>'application/json'})
16
+ request.add_field('X-SWU-API-KEY', @configuration.api_key)
17
+
18
+ http = Net::HTTP.new(@configuration.host, @configuration.port)
19
+ http.use_ssl = use_ssl?
20
+ http.set_debug_output($stdout) if @configuration.debug
21
+
22
+ @response = http.request(request, payload)
23
+
24
+ case @response
25
+ when Net::HTTPNotFound then
26
+ raise SendWithUs::ApiInvalidEndpoint, path
27
+ when Net::HTTPSuccess
28
+ puts @response.body if @configuration.debug
29
+ @response
30
+ else
31
+ raise SendWithUs::ApiUnknownError
32
+ end
33
+ rescue Errno::ECONNREFUSED
34
+ raise SendWithUs::ApiConnectionRefused
35
+ end
36
+
37
+ private
38
+
39
+ def request_path(end_point)
40
+ "/api/v#{@configuration.api_version}/#{end_point}"
41
+ end
42
+
43
+ def use_ssl?
44
+ @configuration.protocol == 'https'
45
+ end
46
+
47
+ end
48
+ end
@@ -0,0 +1,49 @@
1
+ module SendWithUs
2
+ class Config
3
+ attr_accessor :settings
4
+ #attr_writer :url, :api_key, :protocol, :host, :port, :api_verstion, :debug
5
+
6
+ DEFAULT_URL = 'https://beta.sendwithus.com'
7
+
8
+ def self.defaults
9
+ source = URI.parse(DEFAULT_URL)
10
+
11
+ {
12
+ url: DEFAULT_URL,
13
+ api_key: nil,
14
+ protocol: source.scheme,
15
+ host: source.host,
16
+ port: source.port,
17
+ api_version: 0,
18
+ debug: true
19
+ }
20
+ end
21
+
22
+ def initialize(options={})
23
+ @settings = SendWithUs::Config.defaults.merge(options)
24
+ end
25
+
26
+ def method_missing(meth, *args, &block)
27
+ meth_str = meth.to_s
28
+
29
+ if meth_str.include?('=')
30
+ # If this is a write attempt, see if we can write to that key
31
+ meth_sym = meth_str.gsub('=', '').to_sym
32
+ has?(meth_sym) ? @settings[meth_sym] = args[0] : super
33
+ else
34
+ # It's a read attempt, see if that key exists
35
+ has?(meth) ? @settings[meth] : super
36
+ end
37
+ end
38
+
39
+ def respond_to_missing?(meth, include_private = false)
40
+ has?(meth) || super
41
+ end
42
+
43
+ private
44
+
45
+ def has?(key)
46
+ @settings.has_key?(key)
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,3 @@
1
+ module SendWithUs
2
+ VERSION = '0.0.6'
3
+ end
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ require 'send_with_us/version'
6
+
7
+ Gem::Specification.new do |gem|
8
+ gem.name = "send_with_us"
9
+ gem.version = SendWithUs::VERSION
10
+ gem.authors = ["Matt Harris", "Chris Cummer"]
11
+ gem.email = ["us@sendwithus.com"]
12
+ gem.description = %q{SendWithUs.com Ruby Client}
13
+ gem.summary = %q{SendWithUs.com Ruby Client}
14
+ gem.homepage = "http://www.sendwithus.com"
15
+
16
+ gem.files = `git ls-files`.split($/)
17
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
18
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
19
+ gem.require_paths = ["lib"]
20
+
21
+ gem.add_development_dependency 'rake'
22
+ gem.add_development_dependency 'shoulda'
23
+ gem.add_development_dependency 'mocha'
24
+ end
@@ -0,0 +1,40 @@
1
+ require_relative '../../test_helper'
2
+
3
+ class TestApiRequest < MiniTest::Unit::TestCase
4
+
5
+ def build_objects
6
+ @payload = {}
7
+ @config = SendWithUs::Config.new( api_version: 3, api_key: 'TEST_KEY', debug: false )
8
+ @request = SendWithUs::ApiRequest.new(@config)
9
+ end
10
+
11
+ def test_payload
12
+ build_objects
13
+ Net::HTTP.any_instance.stubs(:request).returns(Net::HTTPSuccess.new(1.0, 200, "OK"))
14
+ assert_instance_of( Net::HTTPSuccess, @request.send_with(@payload) )
15
+ end
16
+
17
+ def test_send_with_not_found_exception
18
+ build_objects
19
+ Net::HTTP.any_instance.stubs(:request).returns(Net::HTTPNotFound.new(1.0, 404, "OK"))
20
+ assert_raises( SendWithUs::ApiInvalidEndpoint ) { @request.send_with(@payload) }
21
+ end
22
+
23
+ def test_send_with_unknown_exception
24
+ build_objects
25
+ Net::HTTP.any_instance.stubs(:request).returns(Net::HTTPNotAcceptable.new(1.0, 406, "OK"))
26
+ assert_raises( SendWithUs::ApiUnknownError ) { @request.send_with(@payload) }
27
+ end
28
+
29
+ def test_send_with_connection_refused
30
+ build_objects
31
+ Net::HTTP.any_instance.stubs(:request).raises(Errno::ECONNREFUSED.new)
32
+ assert_raises( SendWithUs::ApiConnectionRefused ) { @request.send_with(@payload) }
33
+ end
34
+
35
+ def test_request_path
36
+ build_objects
37
+ assert_equal( true, @request.send(:request_path, :send) == '/api/v3/send' )
38
+ end
39
+
40
+ end
@@ -0,0 +1,24 @@
1
+ require_relative '../../test_helper'
2
+
3
+ describe SendWithUs::Api do
4
+
5
+ describe '.configuration with initializer' do
6
+ before do
7
+ @initializer_api_key = 'CONFIG_TEST'
8
+ SendWithUs::Api.configure { |config| config.api_key = @initializer_api_key }
9
+ end
10
+
11
+ it('configs') { SendWithUs::Api.new.configuration.api_key.must_equal @initializer_api_key }
12
+ end
13
+
14
+ describe '.configuration with custom' do
15
+ before do
16
+ @initializer_api_key = 'CONFIG_TEST'
17
+ @custom_api_key = 'STUFF_AND_THINGS'
18
+ SendWithUs::Api.configure { |config| config.api_key = @initializer_api_key }
19
+ end
20
+
21
+ it('configs') { SendWithUs::Api.new( api_key: @custom_api_key ).configuration.api_key.must_equal @custom_api_key }
22
+ end
23
+
24
+ end
@@ -0,0 +1,22 @@
1
+ require_relative '../../test_helper'
2
+
3
+ class TestConfig < MiniTest::Unit::TestCase
4
+
5
+ def test_class_defaults
6
+ assert_equal( false, SendWithUs::Config.defaults.empty? )
7
+ assert_equal( 'https', SendWithUs::Config.defaults[:protocol] )
8
+ end
9
+
10
+ def test_config_unmerged
11
+ assert_equal( 'https', SendWithUs::Config.new.protocol )
12
+ assert_equal( true, SendWithUs::Config.new.respond_to?(:protocol) )
13
+ assert_raises( NoMethodError ) { SendWithUs::Config.new.widget }
14
+ end
15
+
16
+ def test_config_merged
17
+ assert_equal( 'proto', SendWithUs::Config.new({ protocol: 'proto'}).protocol )
18
+ assert_equal( true, SendWithUs::Config.new.respond_to?(:protocol) )
19
+ assert_raises( NoMethodError ) { SendWithUs::Config.new({ protocol: 'proto'}).widget }
20
+ end
21
+
22
+ end
@@ -0,0 +1,9 @@
1
+ require_relative '../../test_helper'
2
+
3
+ class TestVersion < MiniTest::Unit::TestCase
4
+
5
+ def test_version
6
+ assert_equal( false, SendWithUs::VERSION.nil? )
7
+ end
8
+
9
+ end
@@ -0,0 +1,21 @@
1
+ require 'minitest/autorun'
2
+ require 'minitest/pride'
3
+
4
+ require File.expand_path('../../lib/send_with_us.rb', __FILE__)
5
+
6
+ require 'rubygems'
7
+ require 'bundler'
8
+ require 'mocha'
9
+
10
+ begin
11
+ Bundler.setup(:default, :development)
12
+ rescue Bundler::BundlerError => e
13
+ $stderr.puts e.message
14
+ $stderr.puts "Run `bundle install` to install missing gems"
15
+ exit e.status_code
16
+ end
17
+
18
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
19
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
20
+
21
+ require 'send_with_us'
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: send_with_us
3
+ version: !ruby/object:Gem::Version
4
+ hash: 19
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 6
10
+ version: 0.0.6
11
+ platform: ruby
12
+ authors:
13
+ - Matt Harris
14
+ - Chris Cummer
15
+ autorequire:
16
+ bindir: bin
17
+ cert_chain: []
18
+
19
+ date: 2013-03-08 00:00:00 Z
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rake
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :development
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: shoulda
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
46
+ version: "0"
47
+ type: :development
48
+ version_requirements: *id002
49
+ - !ruby/object:Gem::Dependency
50
+ name: mocha
51
+ prerelease: false
52
+ requirement: &id003 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ hash: 3
58
+ segments:
59
+ - 0
60
+ version: "0"
61
+ type: :development
62
+ version_requirements: *id003
63
+ description: SendWithUs.com Ruby Client
64
+ email:
65
+ - us@sendwithus.com
66
+ executables: []
67
+
68
+ extensions: []
69
+
70
+ extra_rdoc_files: []
71
+
72
+ files:
73
+ - .gitignore
74
+ - .travis.yml
75
+ - CHANGELOG.md
76
+ - Gemfile
77
+ - Gemfile.lock
78
+ - LICENSE
79
+ - README.md
80
+ - Rakefile
81
+ - lib/send_with_us.rb
82
+ - lib/send_with_us/api.rb
83
+ - lib/send_with_us/api_request.rb
84
+ - lib/send_with_us/config.rb
85
+ - lib/send_with_us/version.rb
86
+ - send_with_us.gemspec
87
+ - test/lib/send_with_us/api_request_test.rb
88
+ - test/lib/send_with_us/api_test.rb
89
+ - test/lib/send_with_us/config_test.rb
90
+ - test/lib/send_with_us/version_test.rb
91
+ - test/test_helper.rb
92
+ homepage: http://www.sendwithus.com
93
+ licenses: []
94
+
95
+ post_install_message:
96
+ rdoc_options: []
97
+
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ none: false
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ hash: 3
106
+ segments:
107
+ - 0
108
+ version: "0"
109
+ required_rubygems_version: !ruby/object:Gem::Requirement
110
+ none: false
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ hash: 3
115
+ segments:
116
+ - 0
117
+ version: "0"
118
+ requirements: []
119
+
120
+ rubyforge_project:
121
+ rubygems_version: 1.8.24
122
+ signing_key:
123
+ specification_version: 3
124
+ summary: SendWithUs.com Ruby Client
125
+ test_files:
126
+ - test/lib/send_with_us/api_request_test.rb
127
+ - test/lib/send_with_us/api_test.rb
128
+ - test/lib/send_with_us/config_test.rb
129
+ - test/lib/send_with_us/version_test.rb
130
+ - test/test_helper.rb