serensic 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: fda5f9640e30be7a4a459a677bbd2bd146f26dfb
4
+ data.tar.gz: f8ffc45bba3f35a9da191b17368deaae12e1ada7
5
+ SHA512:
6
+ metadata.gz: b00ba5ae0242a5a8fee6ab857ef746e5f0d829d4caee6ef7d3917587b684f80fddaf8b9e86893fedbbe219e3d3a1d942b7fcf5f17c467ffc0c357020e1e336d2
7
+ data.tar.gz: f2cb950413579d816416001c4c4030c837ffda4c67db6afa6e9a0388afdcc86c461738a351e44bca667eade16d52f323f211aa0d4557dd85e4d752195e1e6e7d
@@ -0,0 +1,18 @@
1
+ Copyright (c) 2016 Mark Francis
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,76 @@
1
+ Serensic Client Library for Ruby
2
+ =============================
3
+
4
+ This is the Ruby client library for serensic's API. To use it you'll
5
+
6
+
7
+ * [Installation](#installation)
8
+ * [Usage](#usage)
9
+ * [SMS API](#sms-api)
10
+ * [Coverage](#api-coverage)
11
+ * [License](#license)
12
+
13
+ Installation
14
+ ------------
15
+
16
+ To install the Ruby client library using Rubygems:
17
+
18
+ gem install serensic
19
+
20
+ Alternatively you can clone the repository:
21
+
22
+ git clone git@github.com:pesaply/serensic-ruby.git
23
+
24
+
25
+ Usage
26
+ -----
27
+
28
+ Begin by requiring the serensic library:
29
+
30
+ ```ruby
31
+ require 'serensic'
32
+ ```
33
+
34
+ Then construct a client object with your user and secret:
35
+
36
+ ```ruby
37
+ client = serensic::Client.new(user: 'YOUR-API-USER', secret: 'YOUR-API-SECRET')
38
+ ```
39
+
40
+ For production you can specify the `SERERNSIC_USER` and `SERENSIC_SECRET`
41
+ environment variables instead of specifying the key and secret explicitly.
42
+
43
+ ## SMS API
44
+
45
+ ### Send a text message
46
+
47
+ ```ruby
48
+ response = client.send_message(to: 'YOUR NUMBER', msg: 'Hello world')
49
+
50
+ unless response['data'].nil? || response['data'][0]['status'] != 'OK'
51
+ puts "Sent message #{response['data'][0]['smslog_id']}"
52
+ else
53
+ puts "Error: #{response['error_string']}"
54
+ end
55
+ ```
56
+
57
+
58
+ API Coverage
59
+ ------------
60
+
61
+ * Messaging
62
+ * [X] Send
63
+ * [ ] Delivery Receipt
64
+ * [ ] Inbound Messages
65
+ * [ ] Search
66
+ * [ ] Message
67
+ * [ ] Messages
68
+ * [ ] Rejections
69
+
70
+
71
+ License
72
+ -------
73
+
74
+ This library is released under the [MIT License][license]
75
+
76
+ [license]: LICENSE.txt
@@ -0,0 +1,86 @@
1
+ require 'serensic/version'
2
+ require 'serensic/params'
3
+ require 'serensic/errors/error'
4
+ require 'serensic/errors/client_error'
5
+ require 'serensic/errors/server_error'
6
+ require 'serensic/errors/authentication_error'
7
+ require 'net/http'
8
+ require 'json'
9
+
10
+ module Serensic
11
+ class Client
12
+ attr_accessor :user, :secret
13
+
14
+ def initialize(options = {})
15
+ @user = options.fetch(:user) { ENV.fetch('SERERNSIC_USER') }
16
+ @secret = options.fetch(:secret) { ENV.fetch('SERENSIC_SECRET') }
17
+ @host = options.fetch(:host) { 'http://app.serensic.com' }
18
+ @user_agent = "serensic-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 Serensic
2
+ class AuthenticationError < ClientError
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module Serensic
2
+ class ClientError < Error
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module Serensic
2
+ class Error < StandardError
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module Serensic
2
+ class ServerError < Error
3
+ end
4
+ end
@@ -0,0 +1,13 @@
1
+ require 'cgi'
2
+
3
+ module Serensic
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 Serensic
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,28 @@
1
+ require File.expand_path('lib/serensic/version', File.dirname(__FILE__))
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = 'serensic'
5
+ s.version = Serensic::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 Serensic \'s API.'
10
+ s.description = 'Serensic Client Library for Ruby'
11
+ s.files = Dir.glob('{lib,spec}/**/*') + %w(LICENSE.txt README.md serensic.gemspec)
12
+ s.authors = ['Marks Francis']
13
+ s.email = 'marks@serensic.com'
14
+ s.homepage = 'https://github.com/pesaply/serensic-ruby'
15
+ s.required_ruby_version = '>= 2.2.6'
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 == '2.2.6'
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,97 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Serensic::Client' do
4
+ before do
5
+ @user = 'user_xxx'
6
+ @secret = 'secret_xxx'
7
+ @base_url = 'http://app.serensic.com'
8
+ @response_body = { body: '{"key":"value"}', headers: { 'Content-Type' => 'application/json;charset=utf-8' } }
9
+ @response_object = { 'key' => 'value' }
10
+ @client = serensic::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(serensic::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(Serensic::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(serensic::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' => "serensic-ruby/#{Serensic::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
+ # it 'provides an option for specifying a different hostname to connect to' do
47
+ expect_get "http://app.serensic.com/index.php?app=ws&op=pv&u=#{@user}&h=#{@secret}&to=number&msg=Hey!"
48
+ @client = Serensic::Client.new(key: @user, secret: @secret, host: 'app.serensic.com')
49
+ @client.send_message(to: 'number', msg: 'Hey!')
50
+ end
51
+ #
52
+ # private
53
+ #
54
+ def expect(method_symbol, url, body = nil)
55
+ headers = { 'Authorization' => /\ABearer (.+)\.(.+)\.(.+)\z/ }
56
+
57
+ @request = stub_request(method_symbol, url)
58
+
59
+ if method_symbol == :delete
60
+ @request.with(headers: headers).to_return(status: 204)
61
+ elsif body.nil?
62
+ @request.with(headers: headers).to_return(@response_body)
63
+ else
64
+ headers['Content-Type'] = 'application/json'
65
+
66
+ @request.with(headers: headers, body: body).to_return(@response_body)
67
+ end
68
+ end
69
+
70
+ def expect_get(url)
71
+ @request = stub_request(:get, url).to_return(@response_body)
72
+ end
73
+
74
+ def expect_post(url)
75
+ body = WebMock::Util::QueryMapper.query_to_values(data)
76
+
77
+ headers = { 'Content-Type' => 'application/x-www-form-urlencoded' }
78
+
79
+ @request = stub_request(:post, url).to_return(@response_body)
80
+ end
81
+
82
+ def expect_put(url, data)
83
+ body = WebMock::Util::QueryMapper.query_to_values(data)
84
+
85
+ headers = { 'Content-Type' => 'application/x-www-form-urlencoded' }
86
+
87
+ @request = stub_request(:put, url).with(body: body, headers: headers).to_return(@response_body)
88
+ end
89
+
90
+ def expect_delete(url)
91
+ @request = stub_request(:delete, url).to_return(status: 204)
92
+ end
93
+ #
94
+ after do
95
+ assert_requested(@request) if defined?(@request)
96
+ end
97
+ 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 'serensic'
8
+
9
+ RSpec.configure do |config|
10
+ # some (optional) config here
11
+ end
metadata ADDED
@@ -0,0 +1,125 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: serensic
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Marks Francis
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: addressable
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "<"
60
+ - !ruby/object:Gem::Version
61
+ version: 2.5.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.5.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: webmock
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.0'
83
+ description: Serensic Client Library for Ruby
84
+ email: marks@serensic.com
85
+ executables: []
86
+ extensions: []
87
+ extra_rdoc_files: []
88
+ files:
89
+ - LICENSE.txt
90
+ - README.md
91
+ - lib/serensic.rb
92
+ - lib/serensic/errors/authentication_error.rb
93
+ - lib/serensic/errors/client_error.rb
94
+ - lib/serensic/errors/error.rb
95
+ - lib/serensic/errors/server_error.rb
96
+ - lib/serensic/params.rb
97
+ - lib/serensic/version.rb
98
+ - serensic.gemspec
99
+ - spec/serensic_spec.rb
100
+ - spec/spec_helper.rb
101
+ homepage: https://github.com/pesaply/serensic-ruby
102
+ licenses:
103
+ - MIT
104
+ metadata: {}
105
+ post_install_message:
106
+ rdoc_options: []
107
+ require_paths:
108
+ - lib
109
+ required_ruby_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: 2.2.6
114
+ required_rubygems_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ requirements: []
120
+ rubyforge_project:
121
+ rubygems_version: 2.4.5.2
122
+ signing_key:
123
+ specification_version: 4
124
+ summary: This is the Ruby client library for Serensic 's API.
125
+ test_files: []