clickatell_api_client 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 27d57e8f04227a482256abb32670b04302de7682
4
+ data.tar.gz: e9977ec3948d55f54185708902960c33cc5c26b2
5
+ SHA512:
6
+ metadata.gz: 0b1699333e04593768b1e78545f65a66593d66fbd2b97b35e9df189b203c55f98cf4c0838c0e01ee1254c6c45227a29965e7e7f2b445fecc2a7eea6c097aad65
7
+ data.tar.gz: f6a91e976fa82d7266c9bc722d953d8bd5fae6ca15af77375a6bddf5a91dfe1f725e116dcbe63290312944596f3b97eb4fb055a1e0f8ed62933bb49af8d302c1
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
13
+
14
+ *.gem
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.1
5
+ before_install: gem install bundler -v 1.15.4
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in clickatell_api_client.gemspec
6
+ gemspec
data/README.md ADDED
@@ -0,0 +1,74 @@
1
+ # ClickatellApiClient
2
+
3
+ Ruby gem which is a wrapper for [Clickatell](https://www.clickatell.com/) API.
4
+ It allows to send a message and supports sending messages to the US (two way message) and to the rest of the World.
5
+ Gem automatically check if a number is from the USA and which type of a message should be used (two way or not).
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'clickatell_api_client'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install clickatell_api_client
22
+
23
+ ## Configuration
24
+
25
+ First of all, configure your client.
26
+
27
+ ```ruby
28
+ ClickatellApiClient.configure do |config|
29
+ config.api_key = '123'
30
+ config.us_api_key = '234'
31
+ config.phone_number '+1324324231'
32
+ end
33
+ ```
34
+
35
+ - `api_key` - your API key for non-two way messages
36
+ - `us_api_key` - your API key for two way messages (US only)
37
+ - `phone_number` - your phone number from Clickatell which is used to send a two way message
38
+
39
+ ## Usage
40
+
41
+ In order to send a message, just use:
42
+
43
+ ```ruby
44
+ ClickatellApiClient::API::SendMessage.new(
45
+ telephone: '+1213123213132',
46
+ message: 'Your message'
47
+ ).call
48
+ ```
49
+
50
+ Example response after sending a message:
51
+ ```ruby
52
+ {
53
+ message_id: '32eef0se02ed0fsdasd',
54
+ to: '+1213123213132',
55
+ send: 'true'
56
+ }
57
+ ```
58
+
59
+ There is a possiblity, that Clickatell API returns error after trying to send a message. Here is the list of exceptions:
60
+ - `ClickatellApiClient::API::TopUpAccountError` - you don't have enought funds to send a message.
61
+ - `ClickatellApiClient::API::MissingFromNumberError` - missing sender number (used in two way messages)
62
+ - `ClickatellApiClient::API::InvalidAPIResponseError` - other API error
63
+
64
+ That's all!
65
+
66
+ ## Development
67
+
68
+ 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.
69
+
70
+ 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).
71
+
72
+ ## Contributing
73
+
74
+ Bug reports and pull requests are welcome on GitHub at https://github.com/paladinsoftware/clickatell_api_client. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
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 "clickatell_api_client"
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(__FILE__)
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,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "clickatell_api_client/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "clickatell_api_client"
8
+ spec.version = ClickatellApiClient::VERSION
9
+ spec.authors = ["Piotr Jaworski"]
10
+ spec.email = ["piotrek.jaw@gmail.com"]
11
+
12
+ spec.summary = %q{Clickatell API Ruby wrapper.}
13
+ spec.description = %q{Gem which is a wrapper written in Ruby for Clickatell API.}
14
+ spec.homepage = "https://github.com/paladinsoftware/clickatell_api_client"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_runtime_dependency "faraday", "~> 0.15"
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.15"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "rspec", "~> 3.0"
28
+ end
@@ -0,0 +1,63 @@
1
+ module ClickatellApiClient
2
+ module API
3
+ class ClickatellAPIError < StandardError; end
4
+ class TopUpAccountError < ClickatellAPIError; end
5
+ class MissingFromNumberError < ClickatellAPIError; end
6
+ class InvalidAPIResponseError < ClickatellAPIError; end
7
+
8
+ class Client
9
+ NO_ERROR = 200
10
+ ERROR_MISSING_FROM_NUMBER = 106
11
+ ERROR_TOP_UP_ACCOUNT = 301
12
+
13
+ PROVIDER_URL = 'https://platform.clickatell.com'.freeze
14
+ MESSAGES_ENDPOINT = '/messages'.freeze
15
+
16
+ attr_reader :api_key
17
+
18
+ def initialize(api_key)
19
+ @api_key = api_key
20
+ end
21
+
22
+ def send_message(params)
23
+ request(
24
+ http_method: :post,
25
+ endpoint: MESSAGES_ENDPOINT,
26
+ params: params
27
+ )
28
+ end
29
+
30
+ private
31
+
32
+ def connection
33
+ @_connection ||= Faraday.new(url: PROVIDER_URL) do |client|
34
+ client.request :url_encoded
35
+ client.adapter Faraday.default_adapter
36
+ client.headers['Authorization'] = api_key
37
+ end
38
+ end
39
+
40
+ def request(http_method:, endpoint:, params:)
41
+ response = connection.public_send(http_method, endpoint, **params)
42
+ error_code = JSON.parse(response.body)['errorCode']
43
+
44
+ return response if successful_response?(error_code)
45
+
46
+ error_class = case error_code
47
+ when ERROR_TOP_UP_ACCOUNT
48
+ TopUpAccountError
49
+ when ERROR_MISSING_FROM_NUMBER
50
+ MissingFromNumberError
51
+ else
52
+ InvalidAPIResponseError
53
+ end
54
+
55
+ raise Object.const_get("::ClickatellApiClient::API::#{error_class}"), "Code: #{error_code}, response: #{response.body}"
56
+ end
57
+
58
+ def successful_response?(code)
59
+ code.to_i == NO_ERROR || code.nil?
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,60 @@
1
+ module ClickatellApiClient
2
+ module API
3
+ class SendMessage
4
+ attr_reader :telephone, :message
5
+
6
+ def initialize(telephone:, message:)
7
+ @telephone = telephone
8
+ @message = message
9
+ end
10
+
11
+ def call
12
+ response = client.send_message(sms_attributes)
13
+ response_body = JSON.parse(response.body)
14
+
15
+ response_data(response_body)
16
+ end
17
+
18
+ private
19
+
20
+ def client
21
+ @_client = Client.new(api_key)
22
+ end
23
+
24
+ def api_key
25
+ if us_phone_number?
26
+ ClickatellApiClient.configuration.us_api_key
27
+ else
28
+ ClickatellApiClient.configuration.api_key
29
+ end
30
+ end
31
+
32
+ def us_phone_number?
33
+ @_us_phone_number ||= TelephoneNumber::UsNumberValidator.new(telephone).call
34
+ end
35
+
36
+ def sms_attributes
37
+ if us_phone_number?
38
+ default_attributes.merge(from: ClickatellApiClient.configuration.phone_number)
39
+ else
40
+ default_attributes
41
+ end
42
+ end
43
+
44
+ def default_attributes
45
+ {
46
+ content: message,
47
+ to: telephone
48
+ }
49
+ end
50
+
51
+ def response_data(response_body)
52
+ {
53
+ message_id: response_body['messages'].first['apiMessageId'],
54
+ to: response_body['messages'].first['to'],
55
+ send: response_body['messages'].first['accepted']
56
+ }
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,17 @@
1
+ module ClickatellApiClient
2
+ module TelephoneNumber
3
+ class UsNumberValidator
4
+ US_PHONE_NUMBER_REGEXP = /^\+1\d{10}$/
5
+
6
+ attr_reader :telephone
7
+
8
+ def initialize(telephone)
9
+ @telephone = telephone
10
+ end
11
+
12
+ def call
13
+ !!US_PHONE_NUMBER_REGEXP.match(telephone)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ module ClickatellApiClient
2
+ VERSION = '0.1.0'.freeze
3
+ end
@@ -0,0 +1,21 @@
1
+ require 'json'
2
+ require 'faraday'
3
+ require 'clickatell_api_client/version'
4
+ require 'clickatell_api_client/telephone_number/us_number_validator'
5
+ require 'clickatell_api_client/api/client'
6
+ require 'clickatell_api_client/api/send_message'
7
+
8
+ module ClickatellApiClient
9
+ class << self
10
+ attr_accessor :configuration
11
+ end
12
+
13
+ def self.configure
14
+ self.configuration ||= Configuration.new
15
+ yield(configuration)
16
+ end
17
+
18
+ class Configuration
19
+ attr_accessor :phone_number, :us_api_key, :api_key
20
+ end
21
+ end
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: clickatell_api_client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Piotr Jaworski
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-05-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.15'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.15'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.15'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.15'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description: Gem which is a wrapper written in Ruby for Clickatell API.
70
+ email:
71
+ - piotrek.jaw@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - Gemfile
80
+ - README.md
81
+ - Rakefile
82
+ - bin/console
83
+ - bin/setup
84
+ - clickatell_api_client.gemspec
85
+ - lib/clickatell_api_client.rb
86
+ - lib/clickatell_api_client/api/client.rb
87
+ - lib/clickatell_api_client/api/send_message.rb
88
+ - lib/clickatell_api_client/telephone_number/us_number_validator.rb
89
+ - lib/clickatell_api_client/version.rb
90
+ homepage: https://github.com/paladinsoftware/clickatell_api_client
91
+ licenses: []
92
+ metadata: {}
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubyforge_project:
109
+ rubygems_version: 2.6.12
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: Clickatell API Ruby wrapper.
113
+ test_files: []