playsms 0.0.1

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,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e54d6f2f4d0527ac6cb56f7641b489a73471b5a2
4
+ data.tar.gz: b589235ed725c8c7c83e75bcd13571c0a76c3279
5
+ SHA512:
6
+ metadata.gz: 0f3980d62cde72e54c9f687ef509337968fc6531cde0d9c61594799686901ef773e7a88438922af0373773f3f7abaf6a54c6ad141fc6ce1bbcbf747e8b7789ac
7
+ data.tar.gz: 03386ecf6aa33fee2bf49f2cde1fa4dbc362a66325e1a31100dee94ef826cf942dd8aa0cabbaceed10a265b97732d899c12bba4bd76605a42be75f135a970718
@@ -0,0 +1,18 @@
1
+ Copyright (c) 2016 Matt Mencel
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
4
+ this software and associated documentation files (the "Software"), to deal in
5
+ the Software without restriction, including without limitation the rights to
6
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
7
+ the Software, and to permit persons to whom the Software is furnished to do so,
8
+ subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
15
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
16
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
17
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,81 @@
1
+ Playsms Client Library for Ruby
2
+ =============================
3
+
4
+ Borrowed heavily from Nexmo Ruby Library: https://github.com/Nexmo/nexmo-ruby
5
+
6
+ [![Gem Version](https://badge.fury.io/rb/playsms.svg)](https://badge.fury.io/rb/playsms) [![Build Status](https://api.travis-ci.org/Playsms/playsms-ruby.svg?branch=master)](https://travis-ci.org/Playsms/playsms-ruby)
7
+
8
+ This is the Ruby client library for PlaySMS's API. To use it you'll
9
+ need a PlaySMS system setup.
10
+
11
+ * [Installation](#installation)
12
+ * [Usage](#usage)
13
+ * [SMS API](#sms-api)
14
+ * [Coverage](#api-coverage)
15
+ * [License](#license)
16
+
17
+ Installation
18
+ ------------
19
+
20
+ To install the Ruby client library using Rubygems:
21
+
22
+ gem install playsms
23
+
24
+ Alternatively you can clone the repository:
25
+
26
+ git clone git@github.com:WIU/playsms-ruby.git
27
+
28
+
29
+ Usage
30
+ -----
31
+
32
+ Begin by requiring the playsms library:
33
+
34
+ ```ruby
35
+ require 'playsms'
36
+ ```
37
+
38
+ Then construct a client object with your user and secret:
39
+
40
+ ```ruby
41
+ client = Playsms::Client.new(user: 'YOUR-API-USER', secret: 'YOUR-API-SECRET')
42
+ ```
43
+
44
+ For production you can specify the `PLAYSMS_USER` and `PLAYSMS_SECRET`
45
+ environment variables instead of specifying the key and secret explicitly.
46
+
47
+ ## SMS API
48
+
49
+ ### Send a text message
50
+
51
+ ```ruby
52
+ response = client.send_message(to: 'YOUR NUMBER', msg: 'Hello world')
53
+
54
+ unless response['data'].nil? || response['data'][0]['status'] != 'OK'
55
+ puts "Sent message #{response['data'][0]['smslog_id']}"
56
+ else
57
+ puts "Error: #{response['error_string']}"
58
+ end
59
+ ```
60
+
61
+ Docs: [https://github.com/antonraharja/playSMS/blob/master/documents/development/WEBSERVICES.md](https://github.com/antonraharja/playSMS/blob/master/documents/development/WEBSERVICES.md)
62
+
63
+ API Coverage
64
+ ------------
65
+
66
+ * Messaging
67
+ * [X] Send
68
+ * [ ] Delivery Receipt
69
+ * [ ] Inbound Messages
70
+ * [ ] Search
71
+ * [ ] Message
72
+ * [ ] Messages
73
+ * [ ] Rejections
74
+
75
+
76
+ License
77
+ -------
78
+
79
+ This library is released under the [MIT License][license]
80
+
81
+ [license]: LICENSE.txt
@@ -0,0 +1,86 @@
1
+ require 'playsms/version'
2
+ require 'playsms/params'
3
+ require 'playsms/errors/error'
4
+ require 'playsms/errors/client_error'
5
+ require 'playsms/errors/server_error'
6
+ require 'playsms/errors/authentication_error'
7
+ require 'net/http'
8
+ require 'json'
9
+
10
+ module Playsms
11
+ class Client
12
+ attr_accessor :user, :secret
13
+
14
+ def initialize(options = {})
15
+ @user = options.fetch(:user) { ENV.fetch('PLAYSMS_USER') }
16
+ @secret = options.fetch(:secret) { ENV.fetch('PLAYSMS_SECRET') }
17
+ @host = options.fetch(:host) { 'http://playsms.example.com' }
18
+ @user_agent = "playsms-ruby/#{VERSION} ruby/#{RUBY_VERSION}"
19
+ end
20
+
21
+ def send_message(params)
22
+ post(@host, '/index.php?app=ws&op=pv&', params)
23
+ end
24
+
25
+ private
26
+
27
+ def get(host, request_uri, params = {})
28
+ uri = URI(host + request_uri + Params.encode(params.merge(u: @user, h: @secret)))
29
+
30
+ message = Net::HTTP::Get.new(uri.request_uri)
31
+
32
+ request(uri, message)
33
+ end
34
+
35
+ def post(host, request_uri, params)
36
+ uri = URI(host + request_uri + Params.encode(params.merge(u: @user, h: @secret)))
37
+
38
+ message = Net::HTTP::Post.new(uri.request_uri)
39
+
40
+ request(uri, message)
41
+ end
42
+
43
+ def put(host, request_uri, params)
44
+ uri = URI(host + request_uri + Params.encode(params.merge(u: @user, h: @secret)))
45
+
46
+ message = Net::HTTP::Put.new(uri.request_uri)
47
+
48
+ request(uri, message)
49
+ end
50
+
51
+ def delete(host, request_uri)
52
+ uri = URI(host + request_uri + Params.encode(params.merge(u: @user, h: @secret)))
53
+
54
+ message = Net::HTTP::Delete.new(uri.request_uri)
55
+
56
+ request(uri, message)
57
+ end
58
+
59
+ def request(uri, message)
60
+ http = Net::HTTP.new(uri.host, Net::HTTP.http_default_port)
61
+
62
+ message['User-Agent'] = @user_agent
63
+
64
+ http_response = http.request(message)
65
+
66
+ case http_response
67
+ when Net::HTTPNoContent
68
+ :no_content
69
+ when Net::HTTPSuccess
70
+ if http_response['Content-Type'].split(';').first == 'application/json'
71
+ JSON.parse(http_response.body)
72
+ else
73
+ http_response.body
74
+ end
75
+ when Net::HTTPUnauthorized
76
+ fail AuthenticationError, "#{http_response.code} response from #{uri.host}"
77
+ when Net::HTTPClientError
78
+ fail ClientError, "#{http_response.code} response from #{uri.host}"
79
+ when Net::HTTPServerError
80
+ fail ServerError, "#{http_response.code} response from #{uri.host}"
81
+ else
82
+ fail Error, "#{http_response.code} response from #{uri.host}"
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,4 @@
1
+ module Playsms
2
+ class AuthenticationError < ClientError
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module Playsms
2
+ class ClientError < Error
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module Playsms
2
+ class Error < StandardError
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module Playsms
2
+ class ServerError < Error
3
+ end
4
+ end
@@ -0,0 +1,13 @@
1
+ require 'cgi'
2
+
3
+ module Playsms
4
+ module Params
5
+ def self.encode(params)
6
+ params.flat_map { |k, vs| Array(vs).map { |v| "#{escape(k)}=#{escape(v)}" } }.join('&')
7
+ end
8
+
9
+ def self.escape(component)
10
+ CGI.escape(component.to_s)
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,3 @@
1
+ module Playsms
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,28 @@
1
+ require File.expand_path('lib/playsms/version', File.dirname(__FILE__))
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = 'playsms'
5
+ s.version = Playsms::VERSION
6
+ s.license = 'MIT'
7
+ s.platform = Gem::Platform::RUBY
8
+ s.date = '2016-12-13'
9
+ s.summary = 'This is the Ruby client library for PlaySMS\'s API.'
10
+ s.description = 'PlaySMS Client Library for Ruby'
11
+ s.files = Dir.glob('{lib,spec}/**/*') + %w(LICENSE.txt README.md playsms.gemspec)
12
+ s.authors = ['Matt Mencel']
13
+ s.email = 'mr-mencel@wiu.edu'
14
+ s.homepage = 'https://github.com/WIU/playsms-ruby'
15
+ s.required_ruby_version = '>= 1.9.3'
16
+ s.add_development_dependency('rake', '~> 11.0')
17
+ s.add_development_dependency('minitest', '~> 5.0')
18
+ s.add_development_dependency "rspec"
19
+
20
+ if RUBY_VERSION == '1.9.3'
21
+ s.add_development_dependency('addressable', '< 2.5.0')
22
+ s.add_development_dependency('webmock', '~> 1.0')
23
+ else
24
+ s.add_development_dependency('webmock', '~> 2.0')
25
+ end
26
+
27
+ s.require_path = 'lib'
28
+ end
@@ -0,0 +1,100 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Playsms::Client' do
4
+ before do
5
+ @user = 'user_xxx'
6
+ @secret = 'secret_xxx'
7
+ @base_url = 'http://playsms.example.com'
8
+ @response_body = { body: '{"key":"value"}', headers: { 'Content-Type' => 'application/json;charset=utf-8' } }
9
+ @response_object = { 'key' => 'value' }
10
+ @client = Playsms::Client.new(key: @user, secret: @secret)
11
+ end
12
+
13
+ # describe 'send_message method' do
14
+ # it 'posts to the sms resource and returns the response object' do
15
+ # expect_post "#{@base_url}/index.php?app=ws&op=pv&u=#{@user}&h=#{@secret}&to=number&msg=Hey!"
16
+ #
17
+ # @client.send_message(to: 'number', msg: 'Hey!').must_equal(@response_object)
18
+ # end
19
+ # end
20
+ #
21
+ # it 'raises an authentication error exception if the response code is 401' do
22
+ # stub_request(:get, /#{@base_url}/).to_return(status: 401)
23
+ #
24
+ # proc { @client.send_message(to: 'number', msg: 'Hey!') }.must_raise(Playsms::AuthenticationError)
25
+ # end
26
+ #
27
+ # it 'raises a client error exception if the response code is 4xx' do
28
+ # stub_request(:get, /#{@base_url}/).to_return(status: 400)
29
+ #
30
+ # proc { @client.send_message(to: 'number', msg: 'Hey!') }.must_raise(Playsms::ClientError)
31
+ # end
32
+ #
33
+ # it 'raises a server error exception if the response code is 5xx' do
34
+ # stub_request(:get, /#{@base_url}/).to_return(status: 500)
35
+ #
36
+ # proc { @client.send_message(to: 'number', msg: 'Hey!') }.must_raise(Playsms::ServerError)
37
+ # end
38
+ #
39
+ # it 'includes a user-agent header with the library version number and ruby version number' do
40
+ # headers = { 'User-Agent' => "playsms-ruby/#{Playsms::VERSION} ruby/#{RUBY_VERSION}" }
41
+ #
42
+ # stub_request(:get, /#{@base_url}/).with(headers: headers).to_return(@response_body)
43
+ #
44
+ # @client.send_message(to: 'number', msg: 'Hey!')
45
+ # end
46
+ #
47
+ # it 'provides an option for specifying a different hostname to connect to' do
48
+ # expect_get "http://playsms-sandbox.example.com/index.php?app=ws&op=pv&u=#{@user}&h=#{@secret}&to=number&msg=Hey!"
49
+ #
50
+ # @client = Playsms::Client.new(key: @user, secret: @secret, host: 'playsms-sandbox.example.com')
51
+ #
52
+ # @client.send_message(to: 'number', msg: 'Hey!')
53
+ # end
54
+ #
55
+ # private
56
+ #
57
+ # def expect(method_symbol, url, body = nil)
58
+ # headers = { 'Authorization' => /\ABearer (.+)\.(.+)\.(.+)\z/ }
59
+ #
60
+ # @request = stub_request(method_symbol, url)
61
+ #
62
+ # if method_symbol == :delete
63
+ # @request.with(headers: headers).to_return(status: 204)
64
+ # elsif body.nil?
65
+ # @request.with(headers: headers).to_return(@response_body)
66
+ # else
67
+ # headers['Content-Type'] = 'application/json'
68
+ #
69
+ # @request.with(headers: headers, body: body).to_return(@response_body)
70
+ # end
71
+ # end
72
+ #
73
+ # def expect_get(url)
74
+ # @request = stub_request(:get, url).to_return(@response_body)
75
+ # end
76
+ #
77
+ # def expect_post(url)
78
+ # # body = WebMock::Util::QueryMapper.query_to_values(data)
79
+ #
80
+ # #headers = { 'Content-Type' => 'application/x-www-form-urlencoded' }
81
+ #
82
+ # @request = stub_request(:post, url).to_return(@response_body)
83
+ # end
84
+ #
85
+ # def expect_put(url, data)
86
+ # body = WebMock::Util::QueryMapper.query_to_values(data)
87
+ #
88
+ # headers = { 'Content-Type' => 'application/x-www-form-urlencoded' }
89
+ #
90
+ # @request = stub_request(:put, url).with(body: body, headers: headers).to_return(@response_body)
91
+ # end
92
+ #
93
+ # def expect_delete(url)
94
+ # @request = stub_request(:delete, url).to_return(status: 204)
95
+ # end
96
+ #
97
+ # after do
98
+ # assert_requested(@request) if defined?(@request)
99
+ # end
100
+ end
@@ -0,0 +1,11 @@
1
+ require 'bundler/setup'
2
+ Bundler.setup
3
+
4
+ require 'minitest/autorun'
5
+ require 'webmock/minitest'
6
+ require 'webmock/rspec'
7
+ require 'playsms'
8
+
9
+ RSpec.configure do |config|
10
+ # some (optional) config here
11
+ end
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: playsms
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Matt Mencel
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-12-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '11.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '11.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '5.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '5.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: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: webmock
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '2.0'
69
+ description: PlaySMS Client Library for Ruby
70
+ email: mr-mencel@wiu.edu
71
+ executables: []
72
+ extensions: []
73
+ extra_rdoc_files: []
74
+ files:
75
+ - LICENSE.txt
76
+ - README.md
77
+ - lib/playsms.rb
78
+ - lib/playsms/errors/authentication_error.rb
79
+ - lib/playsms/errors/client_error.rb
80
+ - lib/playsms/errors/error.rb
81
+ - lib/playsms/errors/server_error.rb
82
+ - lib/playsms/params.rb
83
+ - lib/playsms/version.rb
84
+ - playsms.gemspec
85
+ - spec/playsms_spec.rb
86
+ - spec/spec_helper.rb
87
+ homepage: https://github.com/WIU/playsms-ruby
88
+ licenses:
89
+ - MIT
90
+ metadata: {}
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: 1.9.3
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ requirements: []
106
+ rubyforge_project:
107
+ rubygems_version: 2.6.7
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: This is the Ruby client library for PlaySMS's API.
111
+ test_files: []