socketlabs 0.1.0

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.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ Rakefile
3
+ lib/socketlabs.rb
4
+ test/test.rb
5
+ Manifest
@@ -0,0 +1,55 @@
1
+ = SocketLabs Gem
2
+
3
+ Ruby gem for sending email through SocketLabs Email On-Demand. SocketLabs Email On-Demand is a managed email delivery service featuring high deliverability, message tracking and rich statistics and reporting through a web control panel and API.
4
+
5
+ A SocketLabs Email On-Demand account is required to use this API. Basic SocketLabs Email On-Demand accounts are available for free, with paid plans available for more features and messages per month.
6
+
7
+ For more information visit: www.socketlabs.com
8
+
9
+ == Installation
10
+
11
+ sudo gem install socketlabs
12
+
13
+ == Bare Bones Example
14
+
15
+ # You can get the URL and serverToken from your control panel
16
+ require 'socketlabs'
17
+ SocketLabs.url="https://api.socketlabs.com/v1/email/send"
18
+ SocketLabs.serverToken = "13451345"
19
+
20
+ # You can instantiate the EmailMessage object and work with its properties and methods
21
+ msg = SocketLabs::EmailMessage.new
22
+ msg.from = 'sender@domain.com'
23
+ msg.to = 'recipient@domain.com'
24
+ msg.subject = 'Subject of message'
25
+ msg.textBody = 'This is the text message body.'
26
+ msg.send
27
+
28
+ == Other cool stuff you can do
29
+
30
+ # You can specify an HTML body in addition to or instead of a text body
31
+ msg.htmlBody = '<html><body>This is the text message body.</html></body>'
32
+
33
+ # You can merge data to a template you create online in the SocketLabs Email On-Demand control panel
34
+ msg.templateId="23498723"
35
+ msg.templateData={:firstName=>'John', :accountNumber=>'12345'}
36
+
37
+ # Templates even support data collections such as lines on an invoice
38
+ msg.templateData={:invoiceNumber=>'1234', :items=>{'Blue Widget'=>15, 'Red Widget'=>10}}
39
+
40
+ # When using the template and merge engine, you can even request a copy of the merged html and/or text body in the response.
41
+ msg.showme=true
42
+
43
+ # You can tag your messages with a campaign identifier and a message identifier which will show up in our online delivery reports and stats
44
+ msg.messageId='123456'
45
+ msg.mailingId='Campaign 9'
46
+
47
+ # You can specify custom headers
48
+ msg.headers = {:X-MyHeader=>'Value', :List-Unsubscribe=>'Value'}
49
+
50
+ # Or just do a quick and dirty one liner to construct and send an email
51
+ SocketLabs::SendQuickMessage("recipient@domain.com", "sender@domain.com", "subject", "body")
52
+
53
+ == Copyright
54
+
55
+ Copyright (c)2010 SocketLabs, Inc.
@@ -0,0 +1,14 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'echoe'
4
+
5
+ Echoe.new('socketlabs', '0.1.0') do |p|
6
+ p.description = "Send email through SocketLabs."
7
+ p.url = "http://github.com/socketlabs/socketlabs"
8
+ p.author = "John Alessi"
9
+ p.email = "john@socketlabs.com "
10
+ p.ignore_pattern = ["tmp/*", "script/*"]
11
+ p.development_dependencies = []
12
+ end
13
+
14
+ Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
@@ -0,0 +1,91 @@
1
+ require 'rubygems'
2
+ require 'json'
3
+ require 'net/http'
4
+ require 'net/https'
5
+
6
+ module SocketLabs
7
+
8
+ class << self
9
+ attr_accessor :max_attempts, :url, :serverToken, :ssl
10
+ def max_attempts
11
+ @max_attempts ||= 3
12
+ end
13
+ def ssl
14
+ @ssl || (true)
15
+ end
16
+ def port
17
+ @port || (ssl ? 443 : 80)
18
+ end
19
+
20
+ end
21
+
22
+ class EmailMessage
23
+
24
+ attr_accessor :to, :from, :cc, :bcc, :subject, :template, :textBody, :htmlBody, :messageId, :mailingId, :replyTo, :headers
25
+
26
+ def send
27
+ msg = Hash.new
28
+ msg['From']=from
29
+ msg['To']=to
30
+ msg['Cc']=cc
31
+ msg['Bcc']=bcc
32
+ msg['Subject']=subject
33
+ msg['Template']=template
34
+ msg['TextBody']=textBody
35
+ msg['HtmlBody']=htmlBody
36
+ msg['ReplyTo']=replyTo
37
+ msg['Headers'] = headers
38
+ SocketLabs::SendRawHashMessage(msg);
39
+ end
40
+
41
+ end
42
+
43
+ def self.SendQuickMessage(from, to, subject, textBody, options={})
44
+ msg = Hash.new
45
+ msg['From']=from
46
+ msg['To']=to
47
+ msg['Subject']=subject
48
+ msg['TextBody']=textBody
49
+ options.each do | k, v |
50
+ msg['Bcc']=v if k=="bcc"
51
+ msg['Cc']=cc if k=="cc"
52
+ msg['Template']=template if k=="template"
53
+ msg['HtmlBody']=htmlBody if k=="htmlBody"
54
+ msg['ReplyTo']=replyTo if k=="replyTo"
55
+ msg['Headers'] = headers if k=="headers"
56
+ end
57
+ SocketLabs::SendRawHashMessage(msg);
58
+ end
59
+
60
+ def self.SendRawHashMessage(msgHash)
61
+ SocketLabs::SendRawJsonMessage(msgHash.to_json)
62
+ end
63
+
64
+ def self.SendRawJsonMessage(msgJson)
65
+ @attempt = 1
66
+ begin
67
+ api_url = URI.parse(url)
68
+ req = Net::HTTP::Post.new(api_url.path)
69
+ #req.basic_auth user, pw
70
+ req.body=msgJson
71
+ req.set_content_type('application/x-www-form-urlencoded')
72
+ req.delete("Accept")
73
+ req.add_field("Accept", "application/json")
74
+ req.add_field("Content-Type", "application/json; charset=utf-8")
75
+ req.add_field("User-Agent", "SocketLabs Ruby Gem")
76
+ req.add_field("X-Server-Token", serverToken)
77
+ http = Net::HTTP.new(api_url.host, @port)
78
+ http.use_ssl= @ssl
79
+ res = http.start {|http| http.request(req) }
80
+ rescue Exception => e
81
+ if @attempt < max_attempts
82
+ @attempt += 1
83
+ retry
84
+ else
85
+ raise
86
+ end
87
+ end
88
+ res.body
89
+ end
90
+
91
+ end
@@ -0,0 +1,30 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{socketlabs}
5
+ s.version = "0.1.0"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["John Alessi"]
9
+ s.date = %q{2010-10-14}
10
+ s.description = %q{Send email through SocketLabs.}
11
+ s.email = %q{john@socketlabs.com }
12
+ s.extra_rdoc_files = ["README.rdoc", "lib/socketlabs.rb"]
13
+ s.files = ["README.rdoc", "Rakefile", "lib/socketlabs.rb", "test/test.rb", "Manifest", "socketlabs.gemspec"]
14
+ s.homepage = %q{http://github.com/socketlabs/socketlabs}
15
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Socketlabs", "--main", "README.rdoc"]
16
+ s.require_paths = ["lib"]
17
+ s.rubyforge_project = %q{socketlabs}
18
+ s.rubygems_version = %q{1.3.7}
19
+ s.summary = %q{Send email through SocketLabs.}
20
+
21
+ if s.respond_to? :specification_version then
22
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
23
+ s.specification_version = 3
24
+
25
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
26
+ else
27
+ end
28
+ else
29
+ end
30
+ end
@@ -0,0 +1,23 @@
1
+ require 'socketlabs'
2
+
3
+ SocketLabs.max_attempts=1
4
+ SocketLabs.url="http://api.test.socketlabs.com/v1/email/send"
5
+ SocketLabs.ssl=false
6
+ SocketLabs.serverToken='API_TEST_TOKEN'
7
+
8
+ msg = SocketLabs::EmailMessage.new
9
+ msg.from = 'john.alessi@gmail.com'
10
+ msg.to = 'john@socketlabs.com'
11
+ msg.subject = 'test message subject'
12
+ msg.textBody = 'test message body'
13
+ #msg.send
14
+ begin
15
+ jsonResult=SocketLabs::SendQuickMessage("john@socketlabs.com", "john@socketlabs.com", "subject", "body")
16
+ hashResult = JSON.parse(jsonResult)
17
+ raise if hashResult['error']!=nil
18
+ puts jsonResult
19
+ rescue
20
+ puts 'There was an error processing your request.'
21
+
22
+ end
23
+
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: socketlabs
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - John Alessi
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-10-14 00:00:00 -04:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: Send email through SocketLabs.
23
+ email: "john@socketlabs.com "
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files:
29
+ - README.rdoc
30
+ - lib/socketlabs.rb
31
+ files:
32
+ - README.rdoc
33
+ - Rakefile
34
+ - lib/socketlabs.rb
35
+ - test/test.rb
36
+ - Manifest
37
+ - socketlabs.gemspec
38
+ has_rdoc: true
39
+ homepage: http://github.com/socketlabs/socketlabs
40
+ licenses: []
41
+
42
+ post_install_message:
43
+ rdoc_options:
44
+ - --line-numbers
45
+ - --inline-source
46
+ - --title
47
+ - Socketlabs
48
+ - --main
49
+ - README.rdoc
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !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
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ hash: 11
67
+ segments:
68
+ - 1
69
+ - 2
70
+ version: "1.2"
71
+ requirements: []
72
+
73
+ rubyforge_project: socketlabs
74
+ rubygems_version: 1.3.7
75
+ signing_key:
76
+ specification_version: 3
77
+ summary: Send email through SocketLabs.
78
+ test_files: []
79
+