mitake 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f243f92bac9f2f716c696e47841e65b41833c66f6c79c6811dff250c7ad32405
4
+ data.tar.gz: e5ff6e87671fe045676a71069a661b39884a44790370136ddba13a3951ab0fac
5
+ SHA512:
6
+ metadata.gz: 844dfdc83c9010bcf1d8b02eb5859fcbd6b1afdce324a34f2f8211058b8c022a4aa3bb9984cad43ee9d2774eb661913a28a75f4ca12d82076718b2ca5e9ef0a0
7
+ data.tar.gz: f016c37ce3909bc05af6a08385d88a300402d42f172c187c6c92ebb3f87daed076b1591f77a6ef5c9bf7282ff438ac44df084332e93aee1c82a96c76a842f803
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
@@ -0,0 +1,34 @@
1
+ # Use this file to configure the Overcommit hooks you wish to use. This will
2
+ # extend the default configuration defined in:
3
+ # https://github.com/sds/overcommit/blob/master/config/default.yml
4
+ #
5
+ # At the topmost level of this YAML file is a key representing type of hook
6
+ # being run (e.g. pre-commit, commit-msg, etc.). Within each type you can
7
+ # customize each hook, such as whether to only run it on certain files (via
8
+ # `include`), whether to only display output if it fails (via `quiet`), etc.
9
+ #
10
+ # For a complete list of hooks, see:
11
+ # https://github.com/sds/overcommit/tree/master/lib/overcommit/hook
12
+ #
13
+ # For a complete list of options that you can use to customize hooks, see:
14
+ # https://github.com/sds/overcommit#configuration
15
+ #
16
+ # Uncomment the following lines to make the configuration take effect.
17
+
18
+ PreCommit:
19
+ AuthorName:
20
+ enabled: false
21
+
22
+ RuboCop:
23
+ enabled: true
24
+ on_warn: fail # Treat all warnings as failures
25
+
26
+ TrailingWhitespace:
27
+ enabled: true
28
+
29
+ PostCheckout:
30
+ ALL: # Special hook name that customizes all hooks of this type
31
+ quiet: true # Change all post-checkout hooks to only display output on failure
32
+
33
+ IndexTags:
34
+ enabled: true # Generate a tags file with `ctags` each time HEAD changes
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.3
7
+ before_install: gem install bundler -v 2.0.2
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at elct9620@frost.tw. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in mitake.gemspec
6
+ gemspec
@@ -0,0 +1,62 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ mitake (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.0)
10
+ bundler-audit (0.6.1)
11
+ bundler (>= 1.2.0, < 3)
12
+ thor (~> 0.18)
13
+ childprocess (3.0.0)
14
+ diff-lcs (1.3)
15
+ iniparse (1.4.4)
16
+ jaro_winkler (1.5.4)
17
+ overcommit (0.51.0)
18
+ childprocess (>= 0.6.3, < 4)
19
+ iniparse (~> 1.4)
20
+ parallel (1.19.1)
21
+ parser (2.6.5.0)
22
+ ast (~> 2.4.0)
23
+ rainbow (3.0.0)
24
+ rake (10.5.0)
25
+ rspec (3.9.0)
26
+ rspec-core (~> 3.9.0)
27
+ rspec-expectations (~> 3.9.0)
28
+ rspec-mocks (~> 3.9.0)
29
+ rspec-core (3.9.0)
30
+ rspec-support (~> 3.9.0)
31
+ rspec-expectations (3.9.0)
32
+ diff-lcs (>= 1.2.0, < 2.0)
33
+ rspec-support (~> 3.9.0)
34
+ rspec-mocks (3.9.0)
35
+ diff-lcs (>= 1.2.0, < 2.0)
36
+ rspec-support (~> 3.9.0)
37
+ rspec-support (3.9.0)
38
+ rubocop (0.76.0)
39
+ jaro_winkler (~> 1.5.1)
40
+ parallel (~> 1.10)
41
+ parser (>= 2.6)
42
+ rainbow (>= 2.2.2, < 4.0)
43
+ ruby-progressbar (~> 1.7)
44
+ unicode-display_width (>= 1.4.0, < 1.7)
45
+ ruby-progressbar (1.10.1)
46
+ thor (0.20.3)
47
+ unicode-display_width (1.6.0)
48
+
49
+ PLATFORMS
50
+ ruby
51
+
52
+ DEPENDENCIES
53
+ bundler (~> 2.0)
54
+ bundler-audit (~> 0.6.1)
55
+ mitake!
56
+ overcommit (~> 0.51.0)
57
+ rake (~> 10.0)
58
+ rspec (~> 3.0)
59
+ rubocop (~> 0.76.0)
60
+
61
+ BUNDLED WITH
62
+ 2.0.2
@@ -0,0 +1,96 @@
1
+ # Mitake (三竹簡訊)
2
+
3
+ This is a Ruby implement to help user send SMS via 三竹簡訊 easier.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'mitake'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install mitake
20
+
21
+ ## Usage
22
+
23
+ To send the SMS, you need to specify the username and password.
24
+
25
+ ```ruby
26
+ Mitake.credential = Mitake::Credential.new('YOUR_USERNAME', 'YOUR_PASSWORD')
27
+ ```
28
+
29
+ If you prefer to config it by the environment variable, please setup `MITAKE_USERNAME` and `MITAKE_PASSWORD`, the gem will automatic create credential.
30
+
31
+ > The default server is `https://smsapi.mitake.com.tw` if you use a different server, please specify it manual.
32
+
33
+ ```ruby
34
+ # Create recipient and give phone number
35
+ recipient = Mitake::Recipient.new(phone_number: '09xxxxxxxx', name: 'John')
36
+
37
+ # Create message with body
38
+ message = Mitake::Message.new(recipient: recipient, body: 'Hello World!')
39
+
40
+ # Delivery message
41
+ message.delivery
42
+
43
+ # Check status
44
+ puts message.status unless message.sent?
45
+ ```
46
+
47
+ ### Switch Credential
48
+
49
+ If you have multiple credentials, you can switch it in the runtime.
50
+
51
+ ```ruby
52
+ external = Mitake::Credential.new('xxx', 'xxx')
53
+
54
+ Mitake.use(external) do
55
+ # Replace default credential with external
56
+ end
57
+
58
+ # Switch back to use default credential
59
+ ```
60
+
61
+ ### Message Attributes
62
+
63
+ |Name|Type|Description
64
+ |----|----|-----------
65
+ |id|String| `Readonly` The message ID from Mitake
66
+ |source_id|String| The customize identity, if same `source_id` send to Mitake, it will response duplicate flag
67
+ |recipient|Mitake::Recipient| The recipient of message
68
+ |body|String| The message body
69
+ |schedule_at|Time| The schedule time to send message
70
+ |expired_at|Time| The message expire time, max value is `+ 24h`
71
+ |duplicate|TrueClass|FalseClass| `Readonly` The message is duplicate (already sent)
72
+ |status_code|Integer| `Readonly` The message status
73
+
74
+ ## Roadmap
75
+
76
+ * [ ] Rspec tests
77
+ * [ ] Message
78
+ * [x] Delivery
79
+ * [ ] Batch Delivery
80
+ * [ ] Webhook
81
+ * [ ] Query Status
82
+ * [ ] Cancel
83
+
84
+ ## Development
85
+
86
+ 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.
87
+
88
+ 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).
89
+
90
+ ## Contributing
91
+
92
+ Bug reports and pull requests are welcome on GitHub at https://github.com/elct9620/mitake. 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.
93
+
94
+ ## Code of Conduct
95
+
96
+ Everyone interacting in the Mitake project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/elct9620/mitake/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'mitake'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
@@ -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,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'mitake/version'
4
+ require 'mitake/credential'
5
+ require 'mitake/balance'
6
+ require 'mitake/message'
7
+
8
+ # The Mitake API Client
9
+ #
10
+ # @since 0.1.0
11
+ module Mitake
12
+ # @since 0.1.0
13
+ # @api private
14
+ LOCK = Mutex.new
15
+
16
+ # Switch credential
17
+ #
18
+ # @param credential [Mitake::Credential] the api credential
19
+ # @param _block [Proc] the actions use specify credential
20
+ #
21
+ # @since 0.1.0
22
+ def self.use(credential, &_block)
23
+ temp = credential
24
+ LOCK.synchronize do
25
+ self.credential = credential
26
+ yield
27
+ self.credential = temp
28
+ end
29
+ end
30
+
31
+ # The credential
32
+ #
33
+ # @return [Mitake::Credential] the current credential
34
+ #
35
+ # @since 0.1.0
36
+ def self.credential
37
+ @credential ||= Credential.new
38
+ end
39
+
40
+ # Set credential
41
+ #
42
+ # @param credential [Mitake::Credential] the new credential
43
+ #
44
+ # @since 0.1.0
45
+ def self.credential=(credential)
46
+ @credential = credential
47
+ end
48
+ end
@@ -0,0 +1,110 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'mitake/parser'
4
+ require 'mitake/api/get'
5
+ require 'mitake/api/post'
6
+
7
+ module Mitake
8
+ # Provide API Interface
9
+ #
10
+ # @since 0.1.0
11
+ # @api private
12
+ module API
13
+ class Error < RuntimeError; end
14
+
15
+ # @since 0.1.0
16
+ # @api private
17
+ def self.included(base)
18
+ base.class_eval do
19
+ @method = 'Get'
20
+
21
+ extend ClassMethods
22
+ end
23
+ end
24
+
25
+ # @since 0.1.0
26
+ # @api private
27
+ module ClassMethods
28
+ # Set/Get the api path
29
+ #
30
+ # @param path [String] the api endpoint path
31
+ # @return [String] if path not given, return previous value
32
+ #
33
+ # @since 0.1.0
34
+ # @api private
35
+ def path(path = nil)
36
+ return @path if path.nil?
37
+
38
+ @path = path
39
+ end
40
+
41
+ # Set/Get the api method
42
+ #
43
+ # @param method [String] the api request method
44
+ # @return [String] if method not given, return previous value
45
+ #
46
+ # @since 0.1.0
47
+ # @api private
48
+ def method(method = nil)
49
+ return @method if method.nil?
50
+
51
+ @method = method.to_s.capitalize
52
+ end
53
+
54
+ # Set response field mapping
55
+ #
56
+ # @param from [String] the response field
57
+ # @param to [String] the target field
58
+ #
59
+ # @since 0.1.0
60
+ # @api private
61
+ def map(from, to)
62
+ @mapping ||= {}
63
+ @mapping[from.to_s] = to.to_s
64
+ end
65
+
66
+ # Execute API
67
+ #
68
+ # @param params [Hash] the API params
69
+ # @param _block [Proc] the customize process
70
+ # @return [Mitake::API] the api response
71
+ #
72
+ # @since 0.1.0
73
+ # @api private
74
+ def execute(params = {}, &_block)
75
+ res = request(params)
76
+ raise Mitake::API::Error, res.code unless res.is_a?(Net::HTTPOK)
77
+
78
+ items = Parser.new(res).parse.map { |item| rename_attribute(item) }
79
+ return items.map { |item| new(item) } unless block_given?
80
+
81
+ yield items
82
+ end
83
+
84
+ private
85
+
86
+ # Send HTTP Request
87
+ #
88
+ # @return [Net::HTTPResponse] the http response
89
+ #
90
+ # @since 0.1.0
91
+ # @api private
92
+ def request(params = {})
93
+ klass = API.const_get(method)
94
+ klass.new(path, params).execute
95
+ end
96
+
97
+ # Rename attributes
98
+ #
99
+ # @param item [Hash] the source hash
100
+ #
101
+ # @since 0.1.0
102
+ # @api private
103
+ def rename_attribute(item)
104
+ item.map do |key, value|
105
+ [@mapping[key] || key, value]
106
+ end.to_h
107
+ end
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,69 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'net/http'
4
+ require 'mitake/credential'
5
+
6
+ module Mitake
7
+ module API
8
+ # @since 0.1.0
9
+ # @api private
10
+ class Base
11
+ # @param path [String] the api endpoint
12
+ # @param params [Hash] the request body
13
+ #
14
+ # @since 0.1.0
15
+ # @api private
16
+ def initialize(path, params = {})
17
+ @path = path
18
+ @params = params
19
+ end
20
+
21
+ # @since 0.1.0
22
+ # @api private
23
+ def request
24
+ raise NotImplementedError, 'Request not defined!'
25
+ end
26
+
27
+ # Execute HTTP Request
28
+ #
29
+ # @return [Net::HTTPResponse] the request result
30
+ #
31
+ # @since 0.1.0
32
+ # @api private
33
+ def execute
34
+ Net::HTTP.start(uri.host, uri.port, use_ssl: ssl?) do |http|
35
+ http.request request
36
+ end
37
+ end
38
+
39
+ # @return [URI] the request URI
40
+ #
41
+ # @since 0.1.0
42
+ # @api private
43
+ def uri
44
+ @uri ||= URI("#{Mitake.credential.server}#{@path}")
45
+ end
46
+
47
+ # @return [TrueClass|FalseClass] is the SSL request
48
+ #
49
+ # @since 0.1.0
50
+ # @api private
51
+ def ssl?
52
+ @uri.scheme == 'https'
53
+ end
54
+
55
+ # Return the request params
56
+ #
57
+ # @return [Hash] the query params
58
+ #
59
+ # @since 0.1.0
60
+ # @api private
61
+ def params
62
+ @params.merge(
63
+ username: Mitake.credential.username,
64
+ password: Mitake.credential.password
65
+ )
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'mitake/api/base'
4
+
5
+ module Mitake
6
+ module API
7
+ # Create HTTP Get Request
8
+ #
9
+ # @since 0.1.0
10
+ # @api private
11
+ class Get < Base
12
+ # Create HTTP Get Request
13
+ #
14
+ # @since 0.1.0
15
+ # @api private
16
+ def request
17
+ return @request unless @request.nil?
18
+
19
+ @request ||= Net::HTTP::Get.new(uri)
20
+ end
21
+
22
+ # @see Mitake::API::Base#uri
23
+ #
24
+ # @since 0.1.0
25
+ # @api private
26
+ def uri
27
+ @uri ||=
28
+ URI("#{Mitake.credential.server}" \
29
+ "#{@path}?#{URI.encode_www_form(params)}")
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'mitake/api/base'
4
+
5
+ module Mitake
6
+ module API
7
+ # Create HTTP Get Request
8
+ #
9
+ # @since 0.1.0
10
+ # @api private
11
+ class Post < Base
12
+ # Create HTTP Post Request
13
+ #
14
+ # @since 0.1.0
15
+ # @api private
16
+ def request
17
+ return @request unless @request.nil?
18
+
19
+ @request ||= Net::HTTP::Post.new(uri)
20
+ @request.body = URI.encode_www_form(params)
21
+ @request
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'mitake/api'
4
+ require 'mitake/model'
5
+
6
+ module Mitake
7
+ # Get the current balance
8
+ #
9
+ # @since 0.1.0
10
+ class Balance
11
+ class << self
12
+ # Get account point
13
+ #
14
+ # @see Mitake::Balance#amount
15
+ # @return [Integer]
16
+ #
17
+ # @since 0.1.0
18
+ def amount
19
+ execute&.first&.amount
20
+ end
21
+ end
22
+
23
+ include API
24
+ include Model
25
+
26
+ path '/api/mtk/SmQuery'
27
+ map 'AccountPoint', 'amount'
28
+
29
+ # @!attribute [r] amount
30
+ # @return [Integer] the amount of account point
31
+ #
32
+ # @since 0.1.0
33
+ attribute :amount, Integer
34
+ end
35
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mitake
4
+ # The helper object to define boolean value
5
+ #
6
+ # @since 0.1.0
7
+ # @api private
8
+ class Boolean
9
+ TRUE_VALUES = %w[Y 1 yes true].freeze
10
+
11
+ class << self
12
+ # Parse boolean value
13
+ #
14
+ # @since 0.1.0
15
+ # @api private
16
+ def parse(value)
17
+ return true if value == true
18
+ return true if TRUE_VALUES.include?(value)
19
+
20
+ false
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mitake
4
+ # The Mitake SMS Client
5
+ #
6
+ # @since 0.1.
7
+ class Credential
8
+ # @since 0.1.0
9
+ # @api private
10
+ DEFAULT_SERVER = 'https://smsapi.mitake.com.tw'
11
+
12
+ # @since 0.1.0
13
+ attr_reader :username, :password, :server
14
+
15
+ # Return Mitake::Client instance
16
+ #
17
+ # @param username [String] the username, default is `MITAKE_USERNAME`
18
+ # @param password [String] the password, default is `MITAKE_PASSWORD`
19
+ # @param server [String] the API server url
20
+ # @return [Mitake::Client] the api instance
21
+ #
22
+ # @since 0.1.0
23
+ def initialize(username = nil, password = nil, server = nil)
24
+ @username = username || ENV['MITAKE_USERNAME']
25
+ @password = password || ENV['MITAKE_PASSWORD']
26
+ @server = server || ENV['MITAKE_SERVER'] || DEFAULT_SERVER
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,114 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'mitake/model'
4
+ require 'mitake/recipient'
5
+ require 'mitake/response'
6
+ require 'mitake/boolean'
7
+ require 'mitake/status'
8
+
9
+ module Mitake
10
+ # Create Sort Message
11
+ #
12
+ # @since 0.1.0
13
+ class Message
14
+ include API
15
+ include Model
16
+
17
+ method 'Post'
18
+ path '/api/mtk/SmSend?CharsetURL=UTF8'
19
+ map 'msgid', 'id'
20
+ map 'Duplicate', 'duplicate'
21
+ map 'statuscode', 'status_code'
22
+
23
+ # @!attribute [r] id
24
+ # @return [String] the message id
25
+ attribute :id, String, readonly: true
26
+
27
+ # @!attribute source_id
28
+ # @return [String] the customize identity
29
+ attribute :source_id, String
30
+
31
+ # @!attribute receipient
32
+ # @return [Mitake::Recipient] the message recipient
33
+ attribute :recipient, Recipient
34
+
35
+ # @!attribute body
36
+ # @return [String] the message body
37
+ attribute :body, String
38
+
39
+ # @!attribute schedule_at
40
+ # @return [Time|NilClass] the schedule time to send message
41
+ attribute :schedule_at, Time
42
+
43
+ # @!attribute expired_at
44
+ # @return [Time|NilClass] the valid time for this message
45
+ attribute :expired_at, Time
46
+
47
+ # @!attribute [r] duplicate
48
+ # @return [TrueClass|FalseClass] is the message duplicate
49
+ attribute :duplicate, Boolean, readonly: true
50
+
51
+ # @!attribute [r] status_code
52
+ # @return [Integer] the status code
53
+ attribute :status_code, Integer, readonly: true
54
+
55
+ # Send message
56
+ #
57
+ # @since 0.1.0
58
+ # @api private
59
+ def delivery
60
+ return self if sent?
61
+
62
+ self.class.execute(params) do |items|
63
+ attrs = items&.first&.slice(*self.class.attribute_names)
64
+ assign_attributes(attrs)
65
+ end
66
+
67
+ self
68
+ end
69
+
70
+ # Does message is sent
71
+ #
72
+ # @return [TrueClass|FalseClass] is the message sent
73
+ #
74
+ # @since 0.1.0
75
+ def sent?
76
+ !@id.nil?
77
+ end
78
+
79
+ # Does message is duplicate
80
+ #
81
+ # @return [TrueClass|FalseClass] is the message duplicate
82
+ #
83
+ # @since 0.1.0
84
+ def duplicate?
85
+ @duplicate == true
86
+ end
87
+
88
+ # Readable status code
89
+ #
90
+ # @return [String] the status code description
91
+ #
92
+ # @since 0.1.0
93
+ def status
94
+ Status::CODES[@status_code]
95
+ end
96
+
97
+ private
98
+
99
+ # The request params
100
+ #
101
+ # @since 0.1.0
102
+ # @api private
103
+ def params
104
+ {
105
+ clientid: @source_id,
106
+ smbody: @body,
107
+ dlvtime: @schedule_at&.strftime('%Y%m%d%H%M%S'),
108
+ vldtime: @expired_at&.strftime('%Y%m%d%H%M%S'),
109
+ dstaddr: @recipient.phone_number,
110
+ destname: @recipient.name
111
+ }.reject { |_, v| v.nil? }.to_h
112
+ end
113
+ end
114
+ end
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'mitake/model/attributes'
4
+ require 'mitake/model/accessor'
5
+
6
+ module Mitake
7
+ # Provide attributes accessor
8
+ #
9
+ # @since 0.1.0
10
+ # @api private
11
+ module Model
12
+ # @since 0.1.0
13
+ # @api private
14
+ def self.included(base)
15
+ base.class_eval do
16
+ include Attributes
17
+ extend Accessor
18
+ end
19
+ end
20
+
21
+ # Assign attribute by hash
22
+ #
23
+ # @see Mitake::Model::Attributes#assign_attributes
24
+ #
25
+ # @param attributes [Hash] the attributes to assignment
26
+ #
27
+ # @since 0.1.0
28
+ def assign_attributes(attributes = {})
29
+ attributes.each do |key, value|
30
+ next unless self.class.attribute_names.include?(key.to_s)
31
+ next send("#{key}=", value) if respond_to?("#{key}=")
32
+
33
+ type = self.class.attributes[key.to_s]
34
+ instance_variable_set("@#{key}", self.class.cast(value, type))
35
+ end
36
+ end
37
+
38
+ # Get attributes as hash
39
+ #
40
+ # @return [Hash] the object attributes
41
+ #
42
+ # @since 0.1.0
43
+ def attributes
44
+ self
45
+ .class
46
+ .attribute_names
47
+ .map { |attr| [attr, send(attr)] }
48
+ .reject { |_, value| value.nil? }
49
+ .map do |attr, value|
50
+ [attr, value.respond_to?(:attributes) ? value.attributes : value]
51
+ end
52
+ .to_h
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,72 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'time'
4
+ require 'date'
5
+
6
+ module Mitake
7
+ module Model
8
+ # @since 0.1.0
9
+ # @api private
10
+ module Accessor
11
+ # Get attributes
12
+ #
13
+ # @return [Hash] the list of attributes and type
14
+ #
15
+ # @since 0.1.0
16
+ def attributes
17
+ @attributes ||= {}
18
+ end
19
+
20
+ # Get attribute names
21
+ #
22
+ # @return [Array] the list of attribute names
23
+ #
24
+ # @since 0.1.0
25
+ def attribute_names
26
+ attributes.keys
27
+ end
28
+
29
+ # Define attribute
30
+ #
31
+ # @param name [String|Symbol] the attribute name
32
+ # @param type [Class] the attribute type
33
+ # @param readonly [TrueClass|FalseClass] is attribute readonly
34
+ #
35
+ # @since 0.1.0
36
+ def attribute(name, type = 'String', readonly: false)
37
+ @attributes ||= {}
38
+ @attributes[name.to_s] = type.to_s
39
+
40
+ define_method name do
41
+ instance_variable_get("@#{name}")
42
+ end
43
+ return if readonly
44
+
45
+ define_method "#{name}=" do |value|
46
+ instance_variable_set("@#{name}", self.class.cast(value, type.to_s))
47
+ end
48
+ end
49
+
50
+ # Casting type
51
+ #
52
+ # @param value [Object] the source value
53
+ # @param type [Class] the cast type
54
+ #
55
+ # @since 0.1.0
56
+ # @api private
57
+ def cast(value, type = 'String')
58
+ case type.to_s
59
+ when 'String', 'Integer', 'Float'
60
+ Kernel.method(type).call(value)
61
+ when 'Time', 'DateTime', 'Date'
62
+ Kernel.const_get(type).parse("#{value}+8")
63
+ else
64
+ klass = Kernel.const_get(type)
65
+ return klass.parse(value) if klass.respond_to?(:parse)
66
+
67
+ value
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mitake
4
+ module Model
5
+ # @since 0.1.0
6
+ # @api private
7
+ module Attributes
8
+ # @since 0.1.0
9
+ # @api private
10
+ def initialize(attributes = {})
11
+ assign_attributes(attributes)
12
+ end
13
+
14
+ # Assign attribute by hash
15
+ #
16
+ # @param attributes [Hash] the attributes to assignment
17
+ #
18
+ # @since 0.1.0
19
+ def assign_attributes(attributes = {})
20
+ attributes.each do |key, value|
21
+ send("#{key}=", value)
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mitake
4
+ # Parse API response
5
+ #
6
+ # @since 0.1.0
7
+ # @api private
8
+ class Parser
9
+ # @since 0.1.0
10
+ ID_MATCHER = /\[(\d+)\]/.freeze
11
+
12
+ # @since 0.1.0
13
+ attr_reader :items
14
+
15
+ # @since 0.1.0
16
+ # @api private
17
+ def initialize(response)
18
+ @response = response
19
+ @body = response.body
20
+ @items = []
21
+ end
22
+
23
+ # TODO: Improve response parser
24
+ #
25
+ # @since 0.1.0
26
+ # @api private
27
+ def parse
28
+ @body.each_line do |line|
29
+ next new_item(Regexp.last_match(1)) if line =~ ID_MATCHER
30
+
31
+ key, value = line.strip.split('=')
32
+ next if key.nil?
33
+
34
+ current[key] = value
35
+ end
36
+ @items << current
37
+ end
38
+
39
+ private
40
+
41
+ # The current response object
42
+ #
43
+ # @since 0.1.0
44
+ # @api private
45
+ def current
46
+ @current ||= {}
47
+ end
48
+
49
+ # Create new response object
50
+ #
51
+ # @param id [String] the clientID
52
+ #
53
+ # @since 0.1.0
54
+ # @api private
55
+ def new_item(id = nil)
56
+ @items << @current unless @current.nil?
57
+ @current = id ? { 'source_id': id } : {}
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'mitake/model'
4
+
5
+ module Mitake
6
+ # The recipient
7
+ #
8
+ # @since 0.1.0
9
+ class Recipient
10
+ include Model
11
+
12
+ # @since 0.1.0
13
+ attribute :name, String
14
+ attribute :phone_number, String
15
+ end
16
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'mitake/api'
4
+
5
+ module Mitake
6
+ # The API response
7
+ #
8
+ # @since 0.1.0
9
+ class Response
10
+ include API
11
+
12
+ method 'Post'
13
+ path '/api/mtk/SmSend?CharsetURL=UTF8'
14
+
15
+ # @since 0.1.0
16
+ # @api private
17
+ def initialize(_response)
18
+ # TODO: Process API response
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mitake
4
+ # The message status
5
+ #
6
+ # @since 0.1.0
7
+ module Status
8
+ # @since 0.1.0
9
+ CODES = {
10
+ 0 => '預約傳送中',
11
+ 1 => '已送達業者',
12
+ 2 => '已送達業者',
13
+ 4 => '已送達手機',
14
+ 5 => '內容有錯誤',
15
+ 6 => '門號有錯誤',
16
+ 7 => '簡訊已停用',
17
+ 8 => '逾時無送達',
18
+ 9 => '預約已取消'
19
+ }.freeze
20
+ end
21
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mitake
4
+ # The message status code
5
+ #
6
+ # @since 0.1.0
7
+ class StatusCode
8
+ end
9
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mitake
4
+ VERSION = '0.1.0'
5
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'mitake/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'mitake'
9
+ spec.version = Mitake::VERSION
10
+ spec.authors = ['蒼時弦也']
11
+ spec.email = ['contact@frost.tw']
12
+
13
+ spec.summary = 'The Mitake SMS API Client'
14
+ spec.description = 'The Mitake SMS API Client'
15
+ spec.homepage = 'https://github.com/elct9620/mitake'
16
+
17
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
18
+
19
+ spec.metadata['homepage_uri'] = spec.homepage
20
+ spec.metadata['source_code_uri'] = 'https://github.com/elct9620/mitake'
21
+ # spec.metadata["changelog_uri"] = "TODO"
22
+
23
+ # Specify which files should be added to the gem when it is released.
24
+ # The `git ls-files -z` loads the files in
25
+ # the RubyGem that have been added into git.
26
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
27
+ `git ls-files -z`
28
+ .split("\x0")
29
+ .reject { |f| f.match(%r{^(test|spec|features)/}) }
30
+ end
31
+ spec.bindir = 'exe'
32
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
33
+ spec.require_paths = ['lib']
34
+
35
+ spec.add_development_dependency 'bundler', '~> 2.0'
36
+ spec.add_development_dependency 'bundler-audit', '~> 0.6.1'
37
+ spec.add_development_dependency 'overcommit', '~> 0.51.0'
38
+ spec.add_development_dependency 'rake', '~> 10.0'
39
+ spec.add_development_dependency 'rspec', '~> 3.0'
40
+ spec.add_development_dependency 'rubocop', '~> 0.76.0'
41
+ end
metadata ADDED
@@ -0,0 +1,158 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mitake
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - 蒼時弦也
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-12-24 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: '2.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler-audit
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.6.1
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.6.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: overcommit
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.51.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.51.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 0.76.0
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 0.76.0
97
+ description: The Mitake SMS API Client
98
+ email:
99
+ - contact@frost.tw
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".overcommit.yml"
106
+ - ".rspec"
107
+ - ".travis.yml"
108
+ - CODE_OF_CONDUCT.md
109
+ - Gemfile
110
+ - Gemfile.lock
111
+ - README.md
112
+ - Rakefile
113
+ - bin/console
114
+ - bin/setup
115
+ - lib/mitake.rb
116
+ - lib/mitake/api.rb
117
+ - lib/mitake/api/base.rb
118
+ - lib/mitake/api/get.rb
119
+ - lib/mitake/api/post.rb
120
+ - lib/mitake/balance.rb
121
+ - lib/mitake/boolean.rb
122
+ - lib/mitake/credential.rb
123
+ - lib/mitake/message.rb
124
+ - lib/mitake/model.rb
125
+ - lib/mitake/model/accessor.rb
126
+ - lib/mitake/model/attributes.rb
127
+ - lib/mitake/parser.rb
128
+ - lib/mitake/recipient.rb
129
+ - lib/mitake/response.rb
130
+ - lib/mitake/status.rb
131
+ - lib/mitake/status_code.rb
132
+ - lib/mitake/version.rb
133
+ - mitake.gemspec
134
+ homepage: https://github.com/elct9620/mitake
135
+ licenses: []
136
+ metadata:
137
+ homepage_uri: https://github.com/elct9620/mitake
138
+ source_code_uri: https://github.com/elct9620/mitake
139
+ post_install_message:
140
+ rdoc_options: []
141
+ require_paths:
142
+ - lib
143
+ required_ruby_version: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - ">="
146
+ - !ruby/object:Gem::Version
147
+ version: '0'
148
+ required_rubygems_version: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ requirements: []
154
+ rubygems_version: 3.0.3
155
+ signing_key:
156
+ specification_version: 4
157
+ summary: The Mitake SMS API Client
158
+ test_files: []