mprofi_api_client 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f0353b028a8b6b41a5b9fb50a66cf78e7642b60b
4
+ data.tar.gz: 1f272443acd95a11f83a921656182ab7beab1d1c
5
+ SHA512:
6
+ metadata.gz: d464335a2a506eb682a360b088e553210305bf4904a52b78b9a4ce1464e4d2b0c29bdcf5b26779a94897d6d4799bd9e6e5322d736a61da40748f1c0cfb8aaa10
7
+ data.tar.gz: ff437de57ff0c4963c99c44c0ee6e0d3d2d73194ba180cfc873fb159f31c135f17da44a20875211e0ea7dea3a5b5d147ccef459a652b37a91838c9513843fecb
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'http://rubygems.org'
2
+
3
+ group :test do
4
+ gem 'minitest', '~> 5.5.1'
5
+ gem 'minitest-reporters'
6
+ gem 'webmock', '~> 1.20.4'
7
+ end
data/LICENSE ADDED
@@ -0,0 +1,16 @@
1
+ Copyright (c) 2015, Materna Communications.
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
5
+
6
+ 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
7
+
8
+ 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the
9
+ documentation and/or other materials provided with the distribution.
10
+
11
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
12
+ BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
13
+ SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
14
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
15
+ INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
16
+ OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1,62 @@
1
+
2
+ # Mprofi API connector
3
+
4
+ This is a mProfi library for Ruby, supportig the mProfi.pl HTTP API.
5
+
6
+
7
+ ## Installation
8
+
9
+ ### In Bundler:
10
+
11
+ ```ruby
12
+ gem 'mprofi_api_client'
13
+
14
+ # or
15
+
16
+ gem 'mprofi_api_client', :git => 'https://github.com/materna/mprofi_api_client_ruby.git'
17
+
18
+ ```
19
+
20
+ ### From github.com
21
+
22
+ ```bash
23
+ git clone https://github.com/materna/mprofi_api_client_ruby.git
24
+ cd mprofi_api_client_ruby
25
+ rake build
26
+ gem install mprofi_api_client-0.1.0.gem
27
+ ```
28
+
29
+ # Usage
30
+
31
+ ```ruby
32
+ require 'mprofi_api_client'
33
+
34
+ API_TOKEN = 'token from mprofi.pl website'
35
+
36
+ connector = MprofiApiClient::Connector.new(API_TOKEN)
37
+ # or
38
+ # connector = MprofiApiClient::Connector.new(API_TOKEN, 'http://your_proxy_host_address:8080/')
39
+ begin
40
+ connector.add_message('501002003', 'Test message 1', 'your-msg-id-001')
41
+ connector.add_message('601002003', 'Test message 2', 'your-msg-id-002')
42
+ result = cliend.send
43
+ # => [{"id"=>58}, {"id"=>59}]
44
+
45
+ result.each do |r|
46
+ status = connector.get_status(r['id'])
47
+ # => {"status"=>"delivered", "id"=>11, "reference"=>"your-msg-id-001", "ts"=>"2015-03-26T10:55:06.098Z"}
48
+
49
+ # do sth with status
50
+ end
51
+
52
+ rescue MprofiAuthError
53
+ # invalid token
54
+ rescue MprofiConnectionError
55
+ # communication error
56
+ end
57
+
58
+ ```
59
+
60
+ ## Copyright
61
+
62
+ Copyright (c) 2015 Materna Communications. See LICENSE for details.
@@ -0,0 +1,23 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rdoc/task'
4
+
5
+ GEMSPEC_FILE = 'mprofi_api_client.gemspec'
6
+
7
+
8
+ Rake::TestTask.new do |t|
9
+ t.libs << "test"
10
+ t.test_files = FileList['test/test*.rb']
11
+ #t.verbose = true
12
+ end
13
+
14
+ RDoc::Task.new do |rdoc|
15
+ rdoc.rdoc_dir = 'doc'
16
+ rdoc.main = 'README.md'
17
+ rdoc.rdoc_files.include("README.md", "lib/**/*.rb", "LICENSE")
18
+ end
19
+
20
+ desc 'Build gem'
21
+ task :build do
22
+ sh "bundle exec gem build #{GEMSPEC_FILE}"
23
+ end
@@ -0,0 +1,8 @@
1
+
2
+ require 'mprofi_api_client/connector'
3
+
4
+ module MprofiApiClient
5
+
6
+ VERSION = '0.1.0' #:nodoc:
7
+
8
+ end
@@ -0,0 +1,135 @@
1
+ require 'net/https'
2
+ require 'json'
3
+ require 'mprofi_api_client/connector_exception'
4
+
5
+ # Module with connector managing communication with mProfi API.
6
+ module MprofiApiClient
7
+
8
+ API_BASE_URL = 'https://api.mprofi.pl' #:nodoc:
9
+ API_VERSION = '1.0' #:nodoc:
10
+
11
+ class MprofiConnectionError < ConnectorException ; end
12
+ class MprofiAuthError < ConnectorException ; end
13
+ class MprofiNotFoundError < ConnectorException ; end
14
+
15
+ # Connector class that manages communication with mProfi public API.
16
+ class Connector
17
+
18
+ attr_reader :messages
19
+ attr_accessor :clear_messages #:nodoc:
20
+
21
+ # +api_token+:: api token, as string. If api_token is not specified `MPROFI_API_TOKEN` env variable will be used.
22
+ # +proxy_url+:: proxy URL (optional)
23
+ def initialize(api_token = nil, proxy_url = nil)
24
+ @api_token = api_token || ENV['MPROFI_API_TOKEN']
25
+ raise ArgumentError, "API token not defined!" unless @api_token
26
+
27
+ @proxy_url = proxy_url
28
+ @messages = []
29
+ @clear_messages = true
30
+ end
31
+
32
+ # Add one message to message queue.
33
+ # +recipient+:: Message recipient as string (phone number format: XXXXXXXXX f.e. 664400100).
34
+ # +message+:: Message content as string.
35
+ # +reference+:: Client message ID defined by user for message tracking. (optional)
36
+ def add_message(recipient, message, reference = nil)
37
+ raise ArgumentError, "`recipient` cannot be empty" if recipient.nil? || recipient.empty?
38
+ raise ArgumentError, "`message` cannot be empty" if message.nil? || message.empty?
39
+
40
+ message = { 'recipient' => recipient, 'message' => message }
41
+ message['reference'] = reference if reference
42
+
43
+ @messages << message
44
+ end
45
+
46
+ # Send messages stored in message queue.
47
+ # +raises+:: _MprofiAuthError_, _MprofiNotFoundError_, _MprofiConnectionError_
48
+ def send
49
+ raise StandardError, 'Empty payload. Please use `add_message` first.' unless @messages.size > 0
50
+
51
+ if send_bulk?
52
+ operation = 'sendbulk'
53
+ payload = { 'messages' => @messages }
54
+ else
55
+ operation = 'send'
56
+ payload = @messages.first
57
+ end
58
+
59
+ request = create_request(operation, :post, payload.to_json)
60
+ result = send_request(request)
61
+ @messages.clear if @clear_messages
62
+
63
+ if result.has_key?('result')
64
+ return result['result']
65
+ else
66
+ return [result]
67
+ end
68
+ end
69
+
70
+ # Check status of message with given id
71
+ # +msg_id+:: message id
72
+ #
73
+ # +raises+:: _MprofiNotFound_ - if message id not found
74
+ def get_status(msg_id)
75
+ raise ArgumentError, '`msg_id` cannot be nil' if msg_id.nil?
76
+
77
+ request = create_request('status', :get, "id=#{msg_id}")
78
+ result = send_request(request)
79
+
80
+ return result
81
+ end
82
+
83
+
84
+ private
85
+
86
+ def send_bulk? #:nodoc:
87
+ @messages.size > 1
88
+ end
89
+
90
+ def create_request(operation, req_method = :get, body_or_query_string = nil) #:nodoc:
91
+ uri = URI.parse([API_BASE_URL, API_VERSION, operation, ''].join('/'))
92
+
93
+ case req_method
94
+ when :post
95
+ request = Net::HTTP::Post.new(uri)
96
+ request.content_type = 'application/json'
97
+ request.body = body_or_query_string
98
+ when :get
99
+ uri.query = body_or_query_string
100
+ request = Net::HTTP::Get.new(uri)
101
+ else
102
+ raise ArgumentError, 'Method not supported'
103
+ end
104
+
105
+ request['Authorization'] = "Token #{@api_token}"
106
+
107
+ return request
108
+ end
109
+
110
+ def send_request(request) #:nodoc:
111
+ uri = request.uri
112
+ begin
113
+ http = Net::HTTP.new(uri.host, uri.port)
114
+ http.proxy_address = @proxy_url if @proxy_url
115
+
116
+ if uri.scheme == 'https'
117
+ http.use_ssl = true
118
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
119
+ end
120
+
121
+ result = http.request(request)
122
+ rescue Exception => err
123
+ raise MprofiConnectionError.new(err.message, err)
124
+ end
125
+
126
+ raise MprofiAuthError, 'Invalid API token' if result.is_a?(Net::HTTPUnauthorized)
127
+ raise MprofiNotFoundError, 'Not found' if result.is_a?(Net::HTTPNotFound)
128
+ raise MprofiConnectionError, "HTTP code: #{result.code}, result: #{result.body}" unless result.is_a?(Net::HTTPSuccess)
129
+
130
+ return JSON.parse(result.body)
131
+ end
132
+
133
+ end
134
+
135
+ end
@@ -0,0 +1,13 @@
1
+
2
+ module MprofiApiClient
3
+
4
+ class ConnectorException < StandardError
5
+ attr_reader :original
6
+
7
+ def initialize(msg, original = nil)
8
+ super(msg)
9
+ @original = original
10
+ end
11
+ end
12
+
13
+ end
@@ -0,0 +1,30 @@
1
+ $LOAD_PATH.unshift(File.expand_path('../lib', __FILE__))
2
+
3
+ require 'mprofi_api_client'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'mprofi_api_client'
7
+ spec.version = MprofiApiClient::VERSION
8
+
9
+ spec.summary = 'MProfi API client library'
10
+ spec.date = '2015-03-05'
11
+ spec.authors = ['Materna Communications']
12
+
13
+ spec.email = 'biuro@materna.com.pl'
14
+ spec.homepage = 'https://github.com/materna/mprofi_api_client-ruby'
15
+ spec.description = spec.summary
16
+ spec.license = 'BSD'
17
+
18
+ spec.required_ruby_version = '>= 1.9.3'
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.3'
22
+ spec.add_development_dependency 'rake'
23
+ spec.add_development_dependency 'minitest'
24
+ spec.add_development_dependency 'minitest-reporters'
25
+ spec.add_development_dependency 'webmock'
26
+
27
+
28
+ spec.files = %w(Gemfile LICENSE mprofi_api_client.gemspec Rakefile README.md)
29
+ spec.files += Dir.glob('lib/**/*.rb')
30
+ end
metadata ADDED
@@ -0,0 +1,121 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mprofi_api_client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Materna Communications
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-05 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.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
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: minitest-reporters
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '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: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: MProfi API client library
84
+ email: biuro@materna.com.pl
85
+ executables: []
86
+ extensions: []
87
+ extra_rdoc_files: []
88
+ files:
89
+ - Gemfile
90
+ - LICENSE
91
+ - README.md
92
+ - Rakefile
93
+ - lib/mprofi_api_client.rb
94
+ - lib/mprofi_api_client/connector.rb
95
+ - lib/mprofi_api_client/connector_exception.rb
96
+ - mprofi_api_client.gemspec
97
+ homepage: https://github.com/materna/mprofi_api_client-ruby
98
+ licenses:
99
+ - BSD
100
+ metadata: {}
101
+ post_install_message:
102
+ rdoc_options: []
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: 1.9.3
110
+ required_rubygems_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ requirements: []
116
+ rubyforge_project:
117
+ rubygems_version: 2.4.6
118
+ signing_key:
119
+ specification_version: 4
120
+ summary: MProfi API client library
121
+ test_files: []