primotexto 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: f253be2828781bb54cbe4afb000a630a0c66251a
4
+ data.tar.gz: e3af78a14752af77c3c7d60e153a2eec83c3b476
5
+ SHA512:
6
+ metadata.gz: bc4fbfc056f9399cd3cb63b3be25a7e6eb8d2616783945d94b6b59d96e412ecae4feac6b45476dbd0ceb564af5926ad71dcaa4d8b9a00229a9ade751a2d266b1
7
+ data.tar.gz: c67ca0dedc222fd732d00803f04f54fe9ae70806438ff71dd544898997c7c5d5fb4f53461c05365b9c637392a11ffa83e6b407b8ff46142a4e631f76d5b4ea8b
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,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ before_install: gem install bundler -v 1.11.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in primotexto.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 syntaxTerr0r
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,168 @@
1
+ # Primotexto
2
+
3
+ This is an unofficial Ruby client for the [Primotexto API](https://www.primotexto.com/api/).
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'primotexto'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install primotexto
20
+
21
+ ## Usage
22
+
23
+ All API methods are not implemented yet. Here is an exhaustive list of what is currently supported.
24
+
25
+ ### Initializing a new client
26
+
27
+ ```ruby
28
+ pt = Primotexto.client(key: 'MY_SHINY_API_KEY')
29
+ ```
30
+
31
+ Alternatively, you can define the key in the `PRIMOTEXTO_API_KEY` `ENV` variable:
32
+
33
+ ```ruby
34
+ pt = Primotexto.client
35
+ ```
36
+
37
+ ### Sending SMS notification message
38
+
39
+ [API reference](https://www.primotexto.com/api/messages/send-notification.asp)
40
+
41
+ ```ruby
42
+ message = pt.post_notification_message(number: '+33612345678', sender: 'Spiderman', message: 'Your new code is "W0WE".', category: 'codeConfirmation')
43
+ ```
44
+
45
+ ### Sending SMS marketing message
46
+
47
+ [API reference](https://www.primotexto.com/api/messages/send-marketing.asp)
48
+
49
+ ```ruby
50
+ message = pt.post_marketing_message(number: '+33612345678', sender: 'Carwash', message: 'Get 30% off with this coupon: "W4SH" until tomorrow.')
51
+ ```
52
+
53
+ See more about [E.164](https://en.wikipedia.org/wiki/E.164) phone formatting.
54
+
55
+ ### Getting a message's status
56
+
57
+ [API reference](https://www.primotexto.com/api/messages/status.asp)
58
+
59
+ By phone number
60
+
61
+ ```ruby
62
+ pt.get_messages_status(identifier: '+33612345678')
63
+ ```
64
+
65
+ Or by snapshotId
66
+
67
+ ```ruby
68
+ pt.get_messages_status(snapshotId: message[:snapshotId])
69
+ ```
70
+
71
+ ### Getting messages' stats
72
+
73
+ [API reference](https://www.primotexto.com/api/messages/stats.asp)
74
+
75
+ ```ruby
76
+ pt.get_messages_stats(category: 'codeConfirmation')
77
+ ```
78
+
79
+ ### Getting a messages' replies
80
+
81
+ [API reference](https://www.primotexto.com/api/messages/callbacks.asp)
82
+
83
+ ```ruby
84
+ pt.get_messages_replies(category: 'codeConfirmation')
85
+ ```
86
+
87
+ ### Getting messages' blacklists (Bounces + Unsubscribers)
88
+
89
+ [API reference](https://www.primotexto.com/api/account/stats.asp)
90
+
91
+ ```ruby
92
+ pt.get_messages_blacklists(category: 'codeConfirmation')
93
+ ```
94
+
95
+ ### Unsubscribers
96
+
97
+ [API reference](https://www.primotexto.com/api/account/unsubscribers.asp)
98
+
99
+ #### Getting unsubscribers list
100
+
101
+ ```ruby
102
+ pt.get_unsubscribers_contacts
103
+ ```
104
+
105
+ #### Adding a phone number to unsubscribers list
106
+
107
+ ```ruby
108
+ pt.post_unsubscribers_contacts(identifier: '+33612345678')
109
+ ```
110
+
111
+ ##### Removing a phone number from unsubscribers list
112
+
113
+ [API reference](https://www.primotexto.com/api/account/stats.asp)
114
+
115
+ ```ruby
116
+ pt.delete_unsubscribers_contacts(identifier: '+33612345678')
117
+ ```
118
+
119
+ ### Bounces
120
+
121
+ [API reference](https://www.primotexto.com/api/account/bounces.asp)
122
+
123
+ #### Getting bounces list
124
+
125
+ ```ruby
126
+ pt.get_bounces_contacts
127
+ ```
128
+
129
+ #### Adding a phone number to bounces list
130
+
131
+ ```ruby
132
+ pt.post_bounces_contacts(identifier: '+33612345678')
133
+ ```
134
+
135
+ ##### Removing a phone number from bounces list
136
+
137
+ [API reference](https://www.primotexto.com/api/account/stats.asp)
138
+
139
+ ```ruby
140
+ pt.delete_bounces_contacts(identifier: '+33612345678')
141
+ ```
142
+
143
+ ### Getting you account stats
144
+
145
+ [API reference](https://www.primotexto.com/api/account/stats.asp)
146
+
147
+ ```ruby
148
+ pt.get_account_stats
149
+ ```
150
+
151
+ ## Testing
152
+
153
+ This gem has no specs yet.
154
+
155
+ ## Development
156
+
157
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
158
+
159
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
160
+
161
+ ## Contributing
162
+
163
+ Bug reports and pull requests are welcome on GitHub at https://github.com/syntaxTerr0r/primotexto.
164
+
165
+
166
+ ## License
167
+
168
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
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 "primotexto"
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,52 @@
1
+ module Primotexto
2
+ class Client
3
+ ERROR_CODES = {
4
+ 10 => 'no phone number provided',
5
+ 11 => 'invalid phone number syntax',
6
+ 12 => 'number blacklisted unsubscribed',
7
+ 13 => 'number blacklisted user bounce',
8
+ 14 => 'number blacklisted global bounce',
9
+ 15 => 'number already exists',
10
+ 20 => 'invalid characters in sender',
11
+ 21 => 'sender too short',
12
+ 22 => 'sender too long',
13
+ 23 => 'full numeric sender not allowed',
14
+ 30 => 'no message content provided',
15
+ 31 => 'invalid characters in message',
16
+ 32 => 'message content too long',
17
+ 40 => 'invalid campaign name',
18
+ 41 => 'invalid campaign programming time',
19
+ 42 => 'campaign cannot be deleted',
20
+ 43 => 'campaign cannot be cancelled',
21
+ 44 => 'free mode campaign limit reached',
22
+ 45 => 'invalid campaign tag',
23
+ 46 => 'tag already exists',
24
+ 47 => 'campaign not found',
25
+ 48 => 'invalid campaign send list',
26
+ 60 => 'list not found',
27
+ 61 => 'invalid date format',
28
+ 70 => 'api access disabled',
29
+ 71 => 'insufficient credits',
30
+ 72 => 'authentication failed',
31
+ 91 => 'international mode needed'
32
+ }.freeze
33
+
34
+ API_HOST = 'api.primotexto.com'.freeze
35
+
36
+ API_METHODS_MAP = [
37
+ ['post_notification_message', 'post_notification_messages_send_params'],
38
+ ['post_marketing_message', 'post_marketing_messages_send_params'],
39
+ 'get_messages_status_params',
40
+ 'get_account_stats',
41
+ 'get_messages_stats_params',
42
+ 'get_messages_replies_params',
43
+ 'get_messages_blacklists_params',
44
+ ['get_bounces_contacts', 'get_bounces_default_contacts'],
45
+ ['post_bounces_contacts', 'post_bounces_default_contacts_params'],
46
+ ['delete_bounces_contacts', 'delete_bounces_default_contacts_params'],
47
+ ['get_unsubscribers_contacts', 'get_unsubscribers_default_contacts'],
48
+ ['post_unsubscribers_contacts', 'post_unsubscribers_default_contacts_params'],
49
+ ['delete_unsubscribers_contacts', 'delete_unsubscribers_default_contacts_params'],
50
+ ].freeze
51
+ end
52
+ end
@@ -0,0 +1,70 @@
1
+ require 'net/http'
2
+ require 'json'
3
+ require 'cgi'
4
+
5
+ module Primotexto
6
+ class Client
7
+ private
8
+
9
+ def get(path, params = {})
10
+ uri = full_url_with(path)
11
+ uri.query = query_string(params)
12
+ get_request = Net::HTTP::Get.new(uri.request_uri)
13
+ get_request['X-Primotexto-ApiKey'] = @key
14
+ get_request['Content-Type'] = 'application/json'
15
+ parse query(uri.host, get_request)
16
+ end
17
+
18
+ def post(path, params)
19
+ uri = full_url_with(path)
20
+ post_request = Net::HTTP::Post.new(uri.request_uri)
21
+ post_request['X-Primotexto-ApiKey'] = @key
22
+ post_request['Content-Type'] = 'application/json'
23
+ post_request.body = params.to_json
24
+ parse query(uri.host, post_request)
25
+ end
26
+
27
+ def delete(path, params)
28
+ uri = full_url_with(path)
29
+ uri.query = query_string(params)
30
+ delete_request = Net::HTTP::Delete.new(uri.request_uri)
31
+ delete_request['X-Primotexto-ApiKey'] = @key
32
+ delete_request['Content-Type'] = 'application/json'
33
+ parse query(uri.host, delete_request)
34
+ end
35
+
36
+ def query(host, http_request)
37
+ http = Net::HTTP.new(host, Net::HTTP.https_default_port)
38
+ http.use_ssl = true
39
+ http.request(http_request)
40
+ end
41
+
42
+ def full_url_with(path)
43
+ URI.join("https://#{API_HOST}", "/v2#{path}")
44
+ end
45
+
46
+ def parse(http_response)
47
+ case http_response
48
+ when Net::HTTPSuccess
49
+ if http_response['Content-Type'].split(';').first == 'application/json'
50
+ JSON.parse!(http_response.body, symbolize_names: true)
51
+ else
52
+ http_response.body
53
+ end
54
+ when Net::HTTPUnauthorized
55
+ raise AuthenticationError
56
+ else
57
+ error_code = JSON.parse(http_response.body)['code']
58
+ raise Error, "#{ERROR_CODES[error_code]} (code: #{error_code})"
59
+ end
60
+ end
61
+
62
+ def query_string(params)
63
+ params.flat_map { |k, vs| Array(vs).map { |v| "#{escape(k)}=#{escape(v)}" } }.join('&')
64
+ end
65
+
66
+ def escape(component)
67
+ CGI.escape(component.to_s)
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,34 @@
1
+ require "primotexto/client/constants"
2
+ require "primotexto/client/http"
3
+
4
+ module Primotexto
5
+ class Client
6
+ attr_reader :key
7
+
8
+ def initialize(options = {})
9
+ @key = options.fetch(:key) { ENV.fetch('PRIMOTEXTO_API_KEY') }
10
+ end
11
+
12
+ API_METHODS_MAP.each do |api_method|
13
+ if api_method.is_a?(Array)
14
+ friendly_method = api_method.first
15
+ api_method = api_method.last
16
+ end
17
+
18
+ method = api_method.split('_')
19
+ with_params = api_method.include?('params')
20
+ method.pop if with_params
21
+ friendly_method = method.join('_') if friendly_method.nil?
22
+ verb = method.shift
23
+ uri = '/'
24
+ uri += method.join('/')
25
+
26
+ if with_params
27
+ behaviour = -> (params) { send(verb, uri, params) }
28
+ else
29
+ behaviour = -> { send(verb, uri) }
30
+ end
31
+ define_method(friendly_method.to_sym, &behaviour)
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,3 @@
1
+ module Primotexto
2
+ VERSION = "0.1.0"
3
+ end
data/lib/primotexto.rb ADDED
@@ -0,0 +1,11 @@
1
+ require "primotexto/client"
2
+ require "primotexto/version"
3
+
4
+ module Primotexto
5
+ def self.client(options = {})
6
+ Client.new(options)
7
+ end
8
+
9
+ class Error < StandardError; end
10
+ class AuthenticationError < Error; end
11
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'primotexto/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "primotexto"
8
+ spec.version = Primotexto::VERSION
9
+ spec.authors = ["Sébastien Poudat"]
10
+ spec.email = ["synthax.terror@gmail.com"]
11
+
12
+ spec.summary = %q{Ruby client for the Primotexto API}
13
+ spec.description = %q{Send SMS easily}
14
+ spec.homepage = "https://github.com/syntaxTerr0r/primotexto"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.11"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec", "~> 3.0"
25
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: primotexto
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Sébastien Poudat
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-03-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: Send SMS easily
56
+ email:
57
+ - synthax.terror@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - bin/console
70
+ - bin/setup
71
+ - lib/primotexto.rb
72
+ - lib/primotexto/client.rb
73
+ - lib/primotexto/client/constants.rb
74
+ - lib/primotexto/client/http.rb
75
+ - lib/primotexto/version.rb
76
+ - primotexto.gemspec
77
+ homepage: https://github.com/syntaxTerr0r/primotexto
78
+ licenses:
79
+ - MIT
80
+ metadata: {}
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubyforge_project:
97
+ rubygems_version: 2.4.5
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: Ruby client for the Primotexto API
101
+ test_files: []
102
+ has_rdoc: