twsms2 0.9.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: c364ff8657babc7b1f0a6ffaf9a47fd1e90e427a
4
+ data.tar.gz: 93cde1c61bf791e3076aadf52c26cb1e9f3db347
5
+ SHA512:
6
+ metadata.gz: c9ed81c3fea24c3fa7c267c906fb5240f245fa98a9663e602e08808d9f35d9fd7a245012bdeda5ef872a217154f01f62b30388edc9ca9ff5314a04cefca65bc1
7
+ data.tar.gz: bc2d634dc09216746267526735643629d13b9b3c32d026575c7d806b8de4e2019fa20132d1fccc6cb66e19b30a03826a64d013c60436ab3976a68a037d337fab
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/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in twsms2.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2016 Guanting Chen
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,36 @@
1
+ # Twsms2
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/twsms2`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'twsms2'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install twsms2
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ 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).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/twsms2.
36
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "twsms2"
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,5 @@
1
+ module Twsms2
2
+ class Error < StandardError; end
3
+ class ClientError < Error; end
4
+ class ServerError < Error; end
5
+ end
@@ -0,0 +1,55 @@
1
+ module Twsms2
2
+ module Formatter
3
+ def match_string(rule, string)
4
+ match_data = rule.match(string)
5
+ match_data.nil? ? nil : match_data[1]
6
+ end
7
+
8
+ def format_send_message_info(original_info)
9
+ new_info = {
10
+ access_success: false,
11
+ message_id: nil,
12
+ error: nil
13
+ }
14
+
15
+ code_text = match_string(/<code>(?<code>\w+)<\/code>/, original_info)
16
+ message_id_text = match_string(/<msgid>(?<message_id>\d+)<\/msgid>/, original_info)
17
+
18
+ new_info[:access_success] = !code_text.nil? && !message_id_text.nil? && code_text == '00000'
19
+
20
+ if new_info[:access_success]
21
+ new_info[:message_id] = message_id_text
22
+ else
23
+ new_info[:error] = "TWSMS:CODE_NOT_FOUND"
24
+ new_info[:error] = "TWSMS:#{code_text}" unless code_text.nil?
25
+ new_info[:error].upcase!
26
+ end
27
+
28
+ new_info
29
+ end
30
+
31
+ def format_balance_info(original_info)
32
+ new_info = {
33
+ access_success: false,
34
+ message_quota: 0,
35
+ error: nil
36
+ }
37
+
38
+ code_text = match_string(/<code>(?<code>\w+)<\/code>/, original_info)
39
+ point_text = match_string(/<point>(?<point>\d+)<\/point>/, original_info)
40
+
41
+ new_info[:access_success] = !code_text.nil? && !point_text.nil? && code_text == '00000'
42
+
43
+ if new_info[:access_success]
44
+ new_info[:message_quota] = point_text.to_i
45
+ else
46
+ new_info[:error] = "TWSMS:CODE_NOT_FOUND"
47
+ new_info[:error] = "TWSMS:#{code_text}" unless code_text.nil?
48
+ new_info[:error].upcase!
49
+ end
50
+
51
+ new_info
52
+ end
53
+
54
+ end
55
+ end
@@ -0,0 +1,43 @@
1
+ require 'net/http'
2
+ require 'cgi'
3
+
4
+ module Twsms2
5
+ module Network
6
+ def get(host, request_uri, params = {})
7
+ uri = URI('https://' + host + request_uri)
8
+ uri.query = query_string(params.merge(username: @username, password: @password))
9
+
10
+ message = Net::HTTP::Get.new(uri.request_uri)
11
+ message['User-Agent'] = @user_agent
12
+
13
+ parse(request(uri, message), host)
14
+ end
15
+
16
+ def parse(http_response, host)
17
+ case http_response
18
+ when Net::HTTPSuccess
19
+ http_response.body
20
+ when Net::HTTPClientError
21
+ raise ClientError, "#{http_response.code} response from #{host}"
22
+ when Net::HTTPServerError
23
+ raise ServerError, "#{http_response.code} response from #{host}"
24
+ else
25
+ raise Error, "#{http_response.code} response from #{host}"
26
+ end
27
+ end
28
+
29
+ def request(uri, message)
30
+ http = Net::HTTP.new(uri.host, Net::HTTP.https_default_port)
31
+ http.use_ssl = true
32
+ http.request(message)
33
+ end
34
+
35
+ def query_string(params)
36
+ params.flat_map { |k, vs| Array(vs).map { |v| "#{escape(k)}=#{escape(v)}" } }.join('&')
37
+ end
38
+
39
+ def escape(component)
40
+ CGI.escape(component.to_s)
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,3 @@
1
+ module Twsms2
2
+ VERSION = "0.9.0"
3
+ end
data/lib/twsms2.rb ADDED
@@ -0,0 +1,41 @@
1
+ require 'twsms2/version'
2
+ require 'twsms2/network'
3
+ require 'twsms2/exception'
4
+ require 'twsms2/formatter'
5
+
6
+ module Twsms2
7
+ class Client
8
+ include Twsms2::Network
9
+ include Twsms2::Formatter
10
+
11
+ def initialize(options={})
12
+ @user_agent = "twsms2/#{VERSION}"
13
+ @api_host = options.fetch(:host) { 'api.twsms.com' }
14
+ @username = options.fetch(:username) { ENV.fetch('TWSMS_USERNAME') }
15
+ @password = options.fetch(:password) { ENV.fetch('TWSMS_PASSWORD') }
16
+ end
17
+
18
+ def account_is_available
19
+ balance_info = get_balance
20
+
21
+ ok = balance_info[:message_quota] > 0 && balance_info[:access_success]
22
+ end
23
+
24
+ def send_message(options={})
25
+ options[:to] ||= nil
26
+ options[:content] ||= nil
27
+ options[:long] = options[:long] || options[:long].nil? ? :Y : :N
28
+ options[:popup] = options[:popup] ? :Y : :N
29
+
30
+ response = get(@api_host, '/smsSend.php', popup: options[:popup], mo: :N, longsms: options[:long], mobile: options[:to], message: options[:content], drurl: '')
31
+
32
+ format_send_message_info(response)
33
+ end
34
+
35
+ def get_balance
36
+ response = get(@api_host, '/smsQuery.php', deltime: :N, checkpoint: :Y, mobile: '', msgid: '')
37
+
38
+ format_balance_info(response)
39
+ end
40
+ end
41
+ end
data/twsms2.gemspec ADDED
@@ -0,0 +1,34 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'twsms2/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "twsms2"
8
+ spec.version = Twsms2::VERSION
9
+ spec.authors = ["Guanting Chen"]
10
+ spec.email = ["cgt886@gmail.com"]
11
+ spec.license = "MIT"
12
+ spec.platform = Gem::Platform::RUBY
13
+ spec.summary = %q{2016 台灣簡訊 TwSMS API ( for ruby >= 2.1 )}
14
+ spec.description = %q{2016 台灣簡訊 TwSMS API ( for ruby >= 2.1 )}
15
+ spec.homepage = "https://github.com/guanting112"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
23
+ end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_development_dependency "bundler", "~> 1.12"
31
+ spec.add_development_dependency "rake", "~> 10.0"
32
+ spec.add_development_dependency 'webmock', '~> 1.18'
33
+ spec.add_development_dependency 'minitest', '~> 5.0'
34
+ end
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: twsms2
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.0
5
+ platform: ruby
6
+ authors:
7
+ - Guanting Chen
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-09-02 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.12'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.12'
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: webmock
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.18'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.18'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '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: '5.0'
69
+ description: 2016 台灣簡訊 TwSMS API ( for ruby >= 2.1 )
70
+ email:
71
+ - cgt886@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - Gemfile
78
+ - LICENSE
79
+ - README.md
80
+ - Rakefile
81
+ - bin/console
82
+ - bin/setup
83
+ - lib/twsms2.rb
84
+ - lib/twsms2/exception.rb
85
+ - lib/twsms2/formatter.rb
86
+ - lib/twsms2/network.rb
87
+ - lib/twsms2/version.rb
88
+ - twsms2.gemspec
89
+ homepage: https://github.com/guanting112
90
+ licenses:
91
+ - MIT
92
+ metadata:
93
+ allowed_push_host: https://rubygems.org
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubyforge_project:
110
+ rubygems_version: 2.5.1
111
+ signing_key:
112
+ specification_version: 4
113
+ summary: 2016 台灣簡訊 TwSMS API ( for ruby >= 2.1 )
114
+ test_files: []