smtpcom_api 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.DS_Store ADDED
Binary file
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem "faraday_middleware"
4
+
5
+ group :development do
6
+ gem "rdoc", "~> 3.12"
7
+ gem "bundler", ">= 1.0.0"
8
+ gem "jeweler", "~> 1.8.3"
9
+ gem "simplecov"
10
+ gem "rspec", "~> 2.8.0"
11
+ gem "rspec-mocks"
12
+ gem "webmock"
13
+ gem "vcr"
14
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,52 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ addressable (2.2.8)
5
+ crack (0.3.1)
6
+ diff-lcs (1.1.3)
7
+ faraday (0.8.1)
8
+ multipart-post (~> 1.1)
9
+ faraday_middleware (0.8.7)
10
+ faraday (>= 0.7.4, < 0.9)
11
+ git (1.2.5)
12
+ jeweler (1.8.3)
13
+ bundler (~> 1.0)
14
+ git (>= 1.2.5)
15
+ rake
16
+ rdoc
17
+ json (1.7.3)
18
+ multi_json (1.3.6)
19
+ multipart-post (1.1.5)
20
+ rake (0.9.2.2)
21
+ rdoc (3.12)
22
+ json (~> 1.4)
23
+ rspec (2.8.0)
24
+ rspec-core (~> 2.8.0)
25
+ rspec-expectations (~> 2.8.0)
26
+ rspec-mocks (~> 2.8.0)
27
+ rspec-core (2.8.0)
28
+ rspec-expectations (2.8.0)
29
+ diff-lcs (~> 1.1.2)
30
+ rspec-mocks (2.8.0)
31
+ simplecov (0.6.4)
32
+ multi_json (~> 1.0)
33
+ simplecov-html (~> 0.5.3)
34
+ simplecov-html (0.5.3)
35
+ vcr (2.2.0)
36
+ webmock (1.8.7)
37
+ addressable (>= 2.2.7)
38
+ crack (>= 0.1.7)
39
+
40
+ PLATFORMS
41
+ ruby
42
+
43
+ DEPENDENCIES
44
+ bundler (>= 1.0.0)
45
+ faraday_middleware
46
+ jeweler (~> 1.8.3)
47
+ rdoc (~> 3.12)
48
+ rspec (~> 2.8.0)
49
+ rspec-mocks
50
+ simplecov
51
+ vcr
52
+ webmock
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Vasiliy Sablin
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,110 @@
1
+ # SmtpcomApi
2
+
3
+ SmtpcomApi is a ruby wrapper for the http://smtp.com API (http://api.smtp.com)
4
+
5
+ ## Installation
6
+ [sudo] gem install smtpcom_api
7
+
8
+ ## Usage
9
+
10
+ ### Authentication
11
+
12
+ You need api key and api url for authentication in SmtpcomApi
13
+
14
+ client = SmtpcomApi::Client.new('your_api_key', 'api_url')
15
+
16
+ ### Account settings
17
+
18
+ Getting account
19
+
20
+ account = client.account
21
+
22
+ Getting account attributes as hash
23
+
24
+ account.attributes
25
+ # or
26
+ account.to_hash
27
+
28
+ Updating account attributes:
29
+
30
+ account.company_name = 'My Company'
31
+ account.save
32
+
33
+ ### Senders
34
+
35
+ Getting senders list
36
+
37
+ senders = client.senders
38
+ # or
39
+ senders = account.senders
40
+
41
+ Finding one sender by his label or id
42
+
43
+ sender = client.sender('sender_label_or_id')
44
+ # or
45
+ sender = account.sender('sender_label_or_id')
46
+
47
+ Getting sender attributes as hash
48
+
49
+ sener.attributes
50
+ # or
51
+ sender.to_hash
52
+
53
+ Updating sender attributes
54
+
55
+ sender.login = 'new_login'
56
+ sender.password = 'new_password1'
57
+ sender.save
58
+
59
+ Adding new sender
60
+
61
+ attributes = {
62
+ :label => 'my_label',
63
+ :login => 'my_login',
64
+ :password => 'my_password1',
65
+ :max_quota => 10000
66
+ }
67
+ new_sender = client.add_sender attributes
68
+
69
+ Some senders could be authenticated by IP address instead of login/pass
70
+ To allow sender login that way you should set :is_ip_based attribute to true and add allowed IPs
71
+
72
+ # setting is_ip_based = true
73
+ new_sender.is_ip_based = true
74
+ new_sender.save
75
+
76
+ # adding allowed IPs
77
+ new_sender.add_allowed_ip('222.178.23.97')
78
+
79
+ # or removing some
80
+ new_sender.remove_allowed_ip('222.178.23.97')
81
+
82
+ ### Statistics
83
+
84
+ Examples how to get sender statistics data (summary/detailed delivery/campaigns/complaints/queue):
85
+
86
+ now = Time.now.to_i
87
+ month_ago = now - 30*24*60*60
88
+
89
+ sender.summary :time_start => month_ago, :time_end => now
90
+ sender.delivery :time_start => month_ago, :time_end => now, :limit => 10, :offset => 0
91
+ sender.campaigns :time_start => month_ago, :time_end => now
92
+ sender.complaints :time_start => month_ago, :time_end => now, :limit => 10, :offset => 0
93
+ sender.queue :time_start => month_ago, :time_end => now, :limit => 10, :offset => 0
94
+
95
+
96
+ ## Contributing to smtpcom_api
97
+
98
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
99
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
100
+ * Fork the project.
101
+ * Start a feature/bugfix branch.
102
+ * Commit and push until you are happy with your contribution.
103
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
104
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
105
+
106
+ ## Copyright
107
+
108
+ Copyright (c) 2012 Vasiliy Sablin. See LICENSE.txt for
109
+ further details.
110
+
data/Rakefile ADDED
@@ -0,0 +1,43 @@
1
+ # encoding: utf-8
2
+ $LOAD_PATH << "./lib/"
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ require 'smtpcom_api'
16
+
17
+ Jeweler::Tasks.new do |gem|
18
+ gem.name = "smtpcom_api"
19
+ gem.homepage = "http://github.com/smtpcom/smtpcom_api"
20
+ gem.license = "MIT"
21
+ gem.summary = %Q{Ruby wrapper for the http://smtp.com API (http://api.smtp.com)}
22
+ gem.description = gem.description
23
+ gem.email = "vasiliy.sablin@gmail.com"
24
+ gem.authors = ["Vasiliy Sablin"]
25
+ gem.version = SmtpcomApi::Version::STRING
26
+ end
27
+ Jeweler::RubygemsDotOrgTasks.new
28
+
29
+ require 'rspec/core'
30
+ require 'rspec/core/rake_task'
31
+ RSpec::Core::RakeTask.new(:spec) do |spec|
32
+ spec.pattern = FileList['spec/**/*_spec.rb']
33
+ end
34
+ task :default => :spec
35
+
36
+ require 'rdoc/task'
37
+ Rake::RDocTask.new do |rdoc|
38
+ version = SmtpcomApi::Version::STRING
39
+
40
+ rdoc.rdoc_dir = 'rdoc'
41
+ rdoc.title = "smtpcom_api #{version}"
42
+ rdoc.rdoc_files.include('lib/**/*.rb')
43
+ end
data/examples/usage.rb ADDED
@@ -0,0 +1,61 @@
1
+ require 'smtpcom_api'
2
+
3
+ client = SmtpcomApi::Client.new('your_api_key', 'api_url')
4
+
5
+ # Getting account
6
+ account = client.account
7
+
8
+ # Getting account attributes as hash
9
+ account.attributes # or account.to_hash
10
+
11
+ #updating account attributes:
12
+ account.company_name = 'My Company'
13
+ account.save
14
+
15
+ # Getting senders
16
+ senders = client.senders # OR senders = account.senders
17
+
18
+ # Find one sender by label or id
19
+ sender = client.sender('sender_label') # OR sender = account.sender('sender_label')
20
+
21
+ # Get sender attributes as hash
22
+ sener.attributes # OR sender.to_hash
23
+
24
+ #update sender
25
+ sender.login = 'new_login'
26
+ sender.password = 'new_password1'
27
+ sender.save
28
+
29
+ #creating sender
30
+ attributes = {
31
+ :label => 'my_label',
32
+ :login => 'my_login',
33
+ :password => 'my_password1',
34
+ :max_quota => 10000
35
+ }
36
+ new_sender = client.add_sender attributes
37
+
38
+ # Auth by IP
39
+ # You could allow sender to login from some IP address instead of using login/pass each time
40
+ # To do that you shold first set :is_ip_based attribute to true
41
+ new_sender.is_ip_based = true
42
+ new_sender.save
43
+ # Then, add allowed IP addresse
44
+ new_sender.add_allowed_ip('222.178.23.97')
45
+ # or remove some
46
+ new_sender.remove_allowed_ip('222.178.23.97')
47
+
48
+
49
+ # Examples how to get sender statistics data (summary/detailed delivery/campaigns/complaints/queue)
50
+ now = Time.now.to_i
51
+ month_ago = now - 30*24*60*60
52
+
53
+ sender.summary :time_start => month_ago, :time_end => now
54
+ sender.delivery :time_start => month_ago, :time_end => now, :limit => 10, :offset => 0
55
+ sender.campaigns :time_start => month_ago, :time_end => now
56
+ sender.complaints :time_start => month_ago, :time_end => now, :limit => 10, :offset => 0
57
+ sender.queue :time_start => month_ago, :time_end => now, :limit => 10, :offset => 0
58
+
59
+
60
+
61
+
@@ -0,0 +1,4 @@
1
+ require 'smtpcom_api/client'
2
+ require 'smtpcom_api/account'
3
+ require 'smtpcom_api/sender'
4
+ require 'smtpcom_api/version'
@@ -0,0 +1,34 @@
1
+ module SmtpcomApi
2
+ class Account < Client
3
+ ACCESSORS = [:company_name, :phone, :last_name, :first_name]
4
+ READERS = [:email, :prepaid_quota, :reset_quota_at, :ip_pool, :usage]
5
+ ATTRIBUTES = READERS + ACCESSORS
6
+
7
+ include ::SmtpcomApi::Helpers
8
+
9
+ attr_accessor *ACCESSORS
10
+ attr_reader *READERS
11
+
12
+ def initialize(api_key, api_host)
13
+ super
14
+ reload
15
+ end
16
+
17
+ def reload
18
+ set_attributes get(:account)['account']
19
+ self
20
+ end
21
+
22
+ def dashboard
23
+ st = @reset_quota_at.to_i>0 ? @reset_quota_at - 30*24*24 : now.to_i - 30*24*60*60
24
+ get(:dashboard, :time_end => now.to_i, :time_start => st)['dashboard']
25
+ end
26
+
27
+ def save
28
+ update = attributes.select{|k| ACCESSORS.include? k}
29
+ put :account, :account => update
30
+ self
31
+ end
32
+
33
+ end
34
+ end
@@ -0,0 +1,33 @@
1
+ require 'smtpcom_api/request'
2
+ require 'smtpcom_api/helpers'
3
+
4
+ module SmtpcomApi
5
+ class Client
6
+ include Request
7
+
8
+ attr_reader :api_key, :host
9
+
10
+ def initialize(api_key, api_host='http://api.smtp.com')
11
+ @api_key = api_key
12
+ @api_host = api_host
13
+ end
14
+
15
+ def account
16
+ SmtpcomApi::Account.new(@api_key, @api_host)
17
+ end
18
+
19
+ def sender(label)
20
+ SmtpcomApi::Sender.new(label, @api_key, @api_host)
21
+ end
22
+
23
+ def add_sender(attributes)
24
+ SmtpcomApi::Sender.new(attributes[:label], @api_key, @api_host, post(:senders, :sender => attributes)['sender'])
25
+ end
26
+ alias_method :create_sender, :add_sender
27
+
28
+ def senders
29
+ get(:senders)['senders'].map{|s| SmtpcomApi::Sender.new(s['label'], @api_key, @api_host, s) }
30
+ end
31
+
32
+ end
33
+ end
@@ -0,0 +1,21 @@
1
+ module SmtpcomApi
2
+ module Helpers
3
+
4
+ def to_hash
5
+ self.class.const_get(:ATTRIBUTES).inject({}) do |r, k|
6
+ r.merge k => instance_variable_get("@#{k}")
7
+ end
8
+ end
9
+ alias_method :attributes, :to_hash
10
+
11
+ def set_attributes(attributes)
12
+ self.class.const_get(:ATTRIBUTES).each do |k|
13
+ instance_variable_set("@#{k}", attributes[k.to_s])
14
+ end
15
+ end
16
+
17
+ def now
18
+ Time.now
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,65 @@
1
+ require 'faraday_middleware'
2
+ require 'smtpcom_api/request/urls'
3
+
4
+ module SmtpcomApi
5
+ module Request
6
+
7
+ include Urls
8
+
9
+ API_PATH = '/v1'
10
+ FORMAT = 'json'
11
+ HTTP_METHODS = [:get, :put, :post, :delete]
12
+
13
+ def method_missing(name, *args)
14
+ if HTTP_METHODS.include? name.to_sym
15
+ key = args[0]
16
+ params = args[1] || {}
17
+ pl = params.delete(:pl)
18
+ _call(name, get_url(key, pl), process_params(params))
19
+ else
20
+ super
21
+ end
22
+ end
23
+
24
+ def respond_to?(name)
25
+ if HTTP_METHODS.include? name.to_sym
26
+ true
27
+ else
28
+ super
29
+ end
30
+ end
31
+
32
+ private
33
+
34
+ def process_params(params)
35
+ params[:time_start] = params[:time_start].to_i if params[:time_start]
36
+ params[:time_end] = params[:time_end].to_i if params[:time_end]
37
+ params
38
+ end
39
+
40
+ def get_url(key, pl=[])
41
+ [@api_host, API_PATH, get_path(key, pl), ".#{FORMAT}"].join
42
+ end
43
+
44
+ def get_path(key, pl)
45
+ pl = [pl] unless pl.instance_of?(Array)
46
+ pl.map!{|s| CGI.escape(s.to_s).gsub(/\./,'%2E')}
47
+ Urls.const_get(key.to_s.upcase) % pl
48
+ end
49
+
50
+ def _call(method, path, params)
51
+ conn = Faraday.new do |c|
52
+ c.request :url_encoded
53
+ #c.response :logger
54
+ c.response :json
55
+ c.response :raise_error
56
+ c.adapter :net_http
57
+ end
58
+
59
+ res = conn.send(method, path, params.merge({:api_key => @api_key})).body
60
+ raise "Error #{res["code"]}: #{res["message"]}" unless res['code'] == 0
61
+ res
62
+ end
63
+
64
+ end
65
+ end