lotify 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 862b69f0ef55068945ac454349f9e7d594b00552dd705741a9ea5d0e0b6bf427
4
+ data.tar.gz: f3863eb1a2c725a4ebd256617ba37a029f5874a5b9275620b856f201d5f7ecea
5
+ SHA512:
6
+ metadata.gz: de1574ff8dd7942e53c8c37c4b98e9e44fa08465352464e72c6c735c6bc6b05406a8dd2bde4d542a35d7b64f514db6440019f3b84aec117fed74f7874331709e
7
+ data.tar.gz: a03025de4d17cb4ebf1c7f9ccc44ff61c14c000c82635147dbd6c538063aac676730cc6f76e53a32fb2eb889e0f5723c5076efbf3411efafc59d2ba1cb650329
data/README.md ADDED
@@ -0,0 +1,87 @@
1
+ # Lotify
2
+
3
+ Lotify is a LINE Notify client SDK.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'lotify'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install lotify
20
+
21
+ ## Usage
22
+
23
+ ### initialize lotify instance
24
+
25
+ ```ruby
26
+ lotify = Lotify::Client.new(
27
+ client_id: "your line notify client id",
28
+ client_secret: "your line notify client secret",
29
+ redirect_uri: "your redirect uri"
30
+ )
31
+ ```
32
+
33
+ ### get auth link
34
+
35
+ ```ruby
36
+ auth_link = lotify.get_auth_link("state")
37
+ ```
38
+
39
+ ### get access token
40
+
41
+ ```ruby
42
+ code = "you can get code from redirect uri after user click the auth link"
43
+ token = lotify.get_token(code)
44
+ ```
45
+
46
+ ### get status
47
+
48
+ ```
49
+ response = lotify.status(token)
50
+ ```
51
+
52
+ ### send notification
53
+
54
+ Send a text message.
55
+
56
+ ```
57
+ response = lotify.send(token, message: "Hello lotify.")
58
+ ```
59
+
60
+ Send a text, image and sticker message at same time.
61
+
62
+ ```
63
+ image_url = "https://picsum.photos/240"
64
+
65
+ response = lotify.send(token,
66
+ message: "Hello lotify.",
67
+ imageThumbnail: image_url,
68
+ imageFullsize: image_url,
69
+ stickerPackageId: 1,
70
+ stickerId: 1
71
+ )
72
+ ```
73
+
74
+ ### revoke access token
75
+
76
+ ```
77
+ response = lotify.revoke(token)
78
+ ```
79
+
80
+ ## Contributing
81
+
82
+ Bug reports and pull requests are welcome on GitHub at https://github.com/etrex/lotify.
83
+
84
+ ## License
85
+
86
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
87
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,3 @@
1
+ module Lotify
2
+ VERSION = "0.1.0"
3
+ end
data/lib/lotify.rb ADDED
@@ -0,0 +1,196 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "lotify/version"
4
+ require "net/http"
5
+ require "JSON"
6
+
7
+ module Lotify
8
+ class Error < StandardError; end
9
+
10
+ class Client
11
+ attr_accessor :client_id
12
+ attr_accessor :client_secret
13
+ attr_accessor :redirect_uri
14
+ attr_accessor :bot_origin
15
+ attr_accessor :api_origin
16
+
17
+ def initialize(options = {})
18
+ self.client_id = options[:client_id]
19
+ self.client_secret = options[:client_secret]
20
+ self.redirect_uri = options[:redirect_uri]
21
+
22
+ self.bot_origin = options[:bot_origin] || "https://notify-bot.line.me"
23
+ self.api_origin = options[:api_origin] || "https://notify-api.line.me"
24
+ end
25
+
26
+ # 發送一個get
27
+ # option: :url, :header, :param, :ssl_verify
28
+ def get(option)
29
+ url = URI(option[:url])
30
+ http = Net::HTTP.new(url.host, url.port)
31
+ http.use_ssl = option[:url]["https://"].nil? == false
32
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE unless option[:ssl_verify]
33
+ request = Net::HTTP::Get.new(url)
34
+ option[:header]&.each do |key, value|
35
+ request[key] = value
36
+ end
37
+ http.request(request)
38
+ end
39
+
40
+ # 發送一個post
41
+ # option: :url, :header, :param, :ssl_verify
42
+ def post(option)
43
+ url = URI(option[:url])
44
+ http = Net::HTTP.new(url.host, url.port)
45
+ http.use_ssl = option[:url]["https://"].nil? == false
46
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE unless option[:ssl_verify]
47
+ request = Net::HTTP::Post.new(url)
48
+ option[:header]&.each do |key, value|
49
+ request[key] = value
50
+ end
51
+ request.set_form_data(option[:param] || {})
52
+ http.request(request)
53
+ end
54
+
55
+ # Get Auth Link
56
+ #
57
+ # Get The OAuth2 authorization endpoint URI.
58
+ #
59
+ # @param state Assigns a token that can be used for responding to CSRF attacks
60
+ #
61
+ # CSRF attacks are typically countered by assigning a hash value generated from a user"s session ID, and then verifying the state parameter variable when it attempts to access redirect_uri.
62
+ #
63
+ # LINE Notify is designed with web applications in mind, and requires state parameter variables.
64
+ # @returns The OAuth2 authorization endpoint URI
65
+ def get_auth_link(state)
66
+ data = {
67
+ scope: "notify",
68
+ response_type: "code",
69
+ client_id: self.client_id,
70
+ redirect_uri: self.redirect_uri,
71
+ state: state
72
+ };
73
+
74
+ "#{self.bot_origin}oauth/authorize?#{URI.encode_www_form(data)}"
75
+ end
76
+
77
+
78
+ # Get Token
79
+ #
80
+ # The OAuth2 token endpoint.
81
+ #
82
+ # @param code Assigns a code parameter value generated during redirection
83
+ # @returns An access token for authentication.
84
+ def get_token(code)
85
+ option = {
86
+ url: "#{self.bot_origin}/oauth/token",
87
+ header: {
88
+ "Content-Type": "application/x-www-form-urlencoded",
89
+ },
90
+ param: {
91
+ grant_type: "authorization_code",
92
+ client_id: self.client_id,
93
+ client_secret: self.client_secret,
94
+ redirect_uri: self.redirect_uri,
95
+ code: code
96
+ }
97
+ }
98
+ response = post(option)
99
+ json = JSON.parse(response.body)
100
+ json["access_token"]
101
+ end
102
+
103
+
104
+ # Status
105
+ #
106
+ # An API for checking connection status. You can use this API to check the validity of an access token. Acquires the names of related users or groups if acquiring them is possible.
107
+ #
108
+ # On the connected service side, it"s used to see which groups are configured with a notification and which user the notifications will be sent to. There is no need to check the status with this API before calling /api/notify or /api/revoke.
109
+ #
110
+ # If this API receives a status code 401 when called, the access token will be deactivated on LINE Notify (disabled by the user in most cases). Connected services will also delete the connection information.
111
+ #
112
+ # ## Expected use cases
113
+ # If a connected service wishes to check the connection status of a certain user
114
+ #
115
+ # As LINE Notify also provides the same feature, support for this API is optional.
116
+ #
117
+ # @param accessToken the accessToken you want to revoke
118
+ # @returns
119
+ # - status: Value according to HTTP status code.
120
+ # - message: Message visible to end-user.
121
+ # - targetType: If the notification target is a user: "USER". If the notification target is a group: "GROUP".
122
+ # - target: If the notification target is a user, displays user name. If acquisition fails, displays "null". If the notification target is a group, displays group name. If the target user has already left the group, displays "null".
123
+ def status(access_token)
124
+ option = {
125
+ url: "#{api_origin}/api/status",
126
+ header: {
127
+ Authorization: "Bearer #{access_token}",
128
+ }
129
+ }
130
+ response = get(option)
131
+ JSON.parse(response.body)
132
+ end
133
+
134
+ # Send
135
+ #
136
+ # Sends notifications to users or groups that are related to an access token.
137
+ #
138
+ # If this API receives a status code 401 when called, the access token will be deactivated on LINE Notify (disabled by the user in most cases). Connected services will also delete the connection information.
139
+ #
140
+ # Requests use POST method with application/x-www-form-urlencoded (Identical to the default HTML form transfer type).
141
+ #
142
+ # ## Expected use cases
143
+ # When a connected service has an event that needs to send a notification to LINE
144
+ #
145
+ # @param accessToken An access token related to users or groups
146
+ # @param message The notification content
147
+ # @param options Other optional parameters
148
+ # @returns
149
+ # - status: Value according to HTTP status code
150
+ # - message: Message visible to end-user
151
+ def send(access_token, param)
152
+ option = {
153
+ url: "#{api_origin}/api/notify",
154
+ header: {
155
+ "Content-Type": "application/x-www-form-urlencoded",
156
+ Authorization: "Bearer #{access_token}",
157
+ },
158
+ param: param
159
+ }
160
+ response = post(option)
161
+ JSON.parse(response.body)
162
+ end
163
+
164
+ # Revoke
165
+ #
166
+ # An API used on the connected service side to revoke notification configurations. Using this API will revoke all used access tokens, disabling the access tokens from accessing the API.
167
+ #
168
+ # The revocation process on the connected service side is as follows
169
+ #
170
+ # 1. Call /api/revoke
171
+ # 2. If step 1 returns status code 200, the request is accepted, revoking all access tokens and ending the process
172
+ # 3. If step 1 returns status code 401, the access tokens have already been revoked and the connection will be d
173
+ # 4. If step 1 returns any other status code, the process will end (you can try again at a later time)
174
+ #
175
+ # ### Expected use cases
176
+ # When the connected service wishes to end a connection with a user
177
+ #
178
+ # As LINE Notify also provides the same feature, support for this API is optional.
179
+ #
180
+ # @param accessToken the accessToken you want to revoke
181
+ # @returns
182
+ # - status: Value according to HTTP status code
183
+ # - message: Message visible to end-user
184
+ def revoke(access_token)
185
+ option = {
186
+ url: "#{api_origin}/api/revoke",
187
+ header: {
188
+ "Content-Type": "application/x-www-form-urlencoded",
189
+ Authorization: "Bearer #{access_token}",
190
+ }
191
+ }
192
+ response = post(option)
193
+ JSON.parse(response.body)
194
+ end
195
+ end
196
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lotify
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - etrex kuo
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-04-16 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.17'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.17'
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: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: LINE Notify SDK for bot oauth and api send
56
+ email:
57
+ - et284vu065k3@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - README.md
63
+ - Rakefile
64
+ - lib/lotify.rb
65
+ - lib/lotify/version.rb
66
+ homepage: https://github.com/etrex/lotify
67
+ licenses:
68
+ - MIT
69
+ metadata: {}
70
+ post_install_message:
71
+ rdoc_options: []
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ requirements: []
85
+ rubygems_version: 3.0.6
86
+ signing_key:
87
+ specification_version: 4
88
+ summary: LINE Notify SDK
89
+ test_files: []