line_notify_client 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 33b7a3c966ce4988bca357dcaaf18e08efe82f4065da2e8a737b9ecdff5daf0c
4
+ data.tar.gz: d9b12267a2265cf8263001e4c2c002e1ac843f3312669700e9d574f22e08b566
5
+ SHA512:
6
+ metadata.gz: b29f912a3cdcf243c49ce14af1fd0f0286fd66a2112f90b28691b0b2a2b74becbc80ab03e9a91c41deaa39b014bd12fe6635d18335f6bbc7b429f50a5e8c06c4
7
+ data.tar.gz: c00b02f04756fdad7061ad426382ea3e261f1b29b8592fd628f54f3487a5dd8ff83cce2bf18a97622a320844ae673aa8ef91745cff1c4312a189e4978eb03f3e
@@ -0,0 +1,59 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /spec/examples.txt
9
+ /test/tmp/
10
+ /test/version_tmp/
11
+ /tmp/
12
+
13
+ # Used by dotenv library to load environment variables.
14
+ # .env
15
+
16
+ # Ignore Byebug command history file.
17
+ .byebug_history
18
+
19
+ ## Specific to RubyMotion:
20
+ .dat*
21
+ .repl_history
22
+ build/
23
+ *.bridgesupport
24
+ build-iPhoneOS/
25
+ build-iPhoneSimulator/
26
+
27
+ ## Specific to RubyMotion (use of CocoaPods):
28
+ #
29
+ # We recommend against adding the Pods directory to your .gitignore. However
30
+ # you should judge for yourself, the pros and cons are mentioned at:
31
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
32
+ #
33
+ # vendor/Pods/
34
+
35
+ ## Documentation cache and generated files:
36
+ /.yardoc/
37
+ /_yardoc/
38
+ /doc/
39
+ /rdoc/
40
+
41
+ ## Environment normalization:
42
+ /.bundle/
43
+ /vendor/bundle
44
+ /lib/bundler/man/
45
+
46
+ # for a library or gem, you might want to ignore these files since the code is
47
+ # intended to run in multiple environments; otherwise, check them in:
48
+ # Gemfile.lock
49
+ # .ruby-version
50
+ # .ruby-gemset
51
+
52
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
53
+ .rvmrc
54
+
55
+ # Used by RuboCop. Remote config files pulled in from inherit_from directive.
56
+ # .rubocop-https?--*
57
+
58
+ # rspec failure tracking
59
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+
6
+ group :development, :test do
7
+ gem 'byebug', '~> 10.0'
8
+ gem 'pry-byebug'
9
+ end
@@ -0,0 +1,64 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ line_notify_client (1.0.0)
5
+ faraday (~> 0.13)
6
+ faraday_middleware (~> 0.12)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ addressable (2.7.0)
12
+ public_suffix (>= 2.0.2, < 5.0)
13
+ byebug (10.0.2)
14
+ coderay (1.1.2)
15
+ crack (0.4.3)
16
+ safe_yaml (~> 1.0.0)
17
+ diff-lcs (1.3)
18
+ faraday (0.17.3)
19
+ multipart-post (>= 1.2, < 3)
20
+ faraday_middleware (0.13.1)
21
+ faraday (>= 0.7.4, < 1.0)
22
+ hashdiff (1.0.0)
23
+ method_source (0.9.2)
24
+ multipart-post (2.1.1)
25
+ pry (0.12.2)
26
+ coderay (~> 1.1.0)
27
+ method_source (~> 0.9.0)
28
+ pry-byebug (3.6.0)
29
+ byebug (~> 10.0)
30
+ pry (~> 0.10)
31
+ public_suffix (4.0.2)
32
+ rake (10.5.0)
33
+ rspec (3.9.0)
34
+ rspec-core (~> 3.9.0)
35
+ rspec-expectations (~> 3.9.0)
36
+ rspec-mocks (~> 3.9.0)
37
+ rspec-core (3.9.0)
38
+ rspec-support (~> 3.9.0)
39
+ rspec-expectations (3.9.0)
40
+ diff-lcs (>= 1.2.0, < 2.0)
41
+ rspec-support (~> 3.9.0)
42
+ rspec-mocks (3.9.0)
43
+ diff-lcs (>= 1.2.0, < 2.0)
44
+ rspec-support (~> 3.9.0)
45
+ rspec-support (3.9.0)
46
+ safe_yaml (1.0.5)
47
+ webmock (3.7.6)
48
+ addressable (>= 2.3.6)
49
+ crack (>= 0.3.2)
50
+ hashdiff (>= 0.4.0, < 2.0.0)
51
+
52
+ PLATFORMS
53
+ ruby
54
+
55
+ DEPENDENCIES
56
+ byebug (~> 10.0)
57
+ line_notify_client!
58
+ pry-byebug
59
+ rake (~> 10.0)
60
+ rspec (~> 3.0)
61
+ webmock
62
+
63
+ BUNDLED WITH
64
+ 2.0.2
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2019 Findy Inc. (ファインディ)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,45 @@
1
+ # line_notify_client
2
+
3
+ LINE Notify API client library, written in Ruby https://notify-bot.line.me/doc/en/
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ gem install line_notify_client
9
+ ```
10
+
11
+ or
12
+
13
+ ```
14
+ # Gemfile
15
+ gem 'line_notify_client'
16
+ ```
17
+
18
+ ## Library
19
+
20
+ ```ruby
21
+ require 'line_notify_client'
22
+
23
+ # OAuth2 Auhentication Documentation: https://notify-bot.line.me/doc/en/
24
+ access_token = 'access token from LINE Notify oauth2 authentication'
25
+
26
+ # new client
27
+ client = LineNotifyClient.new(access_token)
28
+
29
+ # Sends notifications to users or groups are related to an access token.
30
+ client.notify('message')
31
+
32
+ # Check the validity of an access token.
33
+ client.status
34
+
35
+ # Disable an access token
36
+ client.revoke
37
+ ```
38
+
39
+ ## Development
40
+
41
+ * Run rspec
42
+
43
+ ```ruby
44
+ bundle exec rake
45
+ ```
@@ -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
+ # frozen_string_literal: true
2
+
3
+ require 'faraday'
4
+ require 'faraday_middleware'
5
+
6
+ require 'line_notify_client/error'
7
+ require 'line_notify_client/http_client'
8
+ require 'line_notify_client/response'
9
+ require 'line_notify_client/version'
10
+
11
+ module LineNotifyClient
12
+ def self.new(access_token)
13
+ LineNotifyClient::HttpClient.new(access_token)
14
+ end
15
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LineNotify
4
+ # Custom error class for rescuing from all LINE Notify errors
5
+ class Error < StandardError
6
+ end
7
+
8
+ # Raised when HTTP request timeout
9
+ class TimeoutError < Error; end
10
+ end
@@ -0,0 +1,83 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LineNotifyClient
4
+ # ## LineNotify::Client
5
+ # A class for LINE Notify API client.
6
+ #
7
+ class HttpClient
8
+ HOST = 'notify-api.line.me'
9
+ ACCEPT = 'application/json'
10
+ USER_AGENT = 'LineNotify Ruby'
11
+ HEADERS = { 'Accept' => ACCEPT, 'User-Agent' => USER_AGENT }.freeze
12
+
13
+ def initialize(access_token)
14
+ @access_token = access_token
15
+ end
16
+
17
+ # ### LineNotify::Client#notify
18
+ # Sends notifications to users or groups that are related to an access token.
19
+ #
20
+ # POST https://notify-api.line.me/api/notify
21
+ def notify(message, options = {})
22
+ params = { message: message }.merge(options)
23
+ post('/api/notify', params, options)
24
+ end
25
+
26
+ # ### LineNotify::Client#status
27
+ # Check the validity of an access token.
28
+ #
29
+ # GET https://notify-api.line.me/api/status
30
+ def status
31
+ get('/api/status')
32
+ end
33
+
34
+ # ### LineNotify::Client#revoke
35
+ # Disable an access token.
36
+ #
37
+ # POST https://notify-api.line.me/api/revoke
38
+ def revoke
39
+ post('/api/revoke')
40
+ end
41
+
42
+ private
43
+
44
+ def get(path, params = nil, headers = nil)
45
+ request(:get, path, params, headers)
46
+ end
47
+
48
+ def post(path, params = nil, headers = nil)
49
+ request(:post, path, params, headers)
50
+ end
51
+
52
+ def request(http_method, path, params, headers)
53
+ faraday_response = connection.send(http_method, path, params, headers)
54
+ LineNotify::Response.new(faraday_response)
55
+ rescue Faraday::Error::TimeoutError => e
56
+ raise LineNotify::TimeoutError, e.message
57
+ rescue Faraday::Error => e
58
+ raise LineNotify::Error, e.message
59
+ end
60
+
61
+ def connection
62
+ @connection ||= Faraday.new(faraday_client_options) do |builder|
63
+ builder.options.timeout = 5 # 5 seconds
64
+ builder.request :url_encoded
65
+ builder.request :json
66
+ builder.response :json
67
+ builder.adapter Faraday.default_adapter
68
+ end
69
+ end
70
+
71
+ def faraday_client_options
72
+ {
73
+ url: "https://#{HOST}",
74
+ ssl: { verify: true },
75
+ headers: faraday_headers
76
+ }
77
+ end
78
+
79
+ def faraday_headers
80
+ HEADERS.merge('Authorization' => "Bearer #{@access_token}")
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,99 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LineNotify
4
+ # ## LineNotify::Response
5
+ # A class for response data returned from API.
6
+ #
7
+ class Response
8
+ HTTP_STATUS_SUCCESS = 200
9
+ HTTP_STATUS_BAD_REQUEST = 400
10
+ HTTP_STATUS_TOKEN_EXPIRED = 401
11
+ HTTP_STATUS_INTERNAL_ERROR = 500
12
+
13
+ def initialize(faraday_response)
14
+ @raw_body = faraday_response.body
15
+ @raw_headers = faraday_response.headers
16
+ @raw_status = faraday_response.status
17
+ end
18
+
19
+ # ### LineNotify::Response#body
20
+ # Returns response body returned from API as a `Hash`.
21
+ #
22
+ # ```rb
23
+ # response.body #=> { ... }
24
+ # ```
25
+ #
26
+ def body
27
+ @raw_body
28
+ end
29
+
30
+ # ### LineNotify::Response#headers
31
+ # Returns response headers returned from API as a `Hash`.
32
+ #
33
+ # ```rb
34
+ # response.headers #=> { "Content-Type" => "application/json" }
35
+ # ```
36
+ #
37
+ def headers
38
+ @headers ||= @raw_headers.inject({}) do |result, (key, value)|
39
+ result.merge(key.split("-").map(&:capitalize).join("-") => value)
40
+ end
41
+ end
42
+
43
+ # ### LineNotify::Response#status
44
+ # Returns response status code returned from API as a `Integer`.
45
+ #
46
+ # ```rb
47
+ # response.status #=> 200
48
+ # ```
49
+ #
50
+ def status
51
+ @raw_status
52
+ end
53
+
54
+ # ### LineNotify::Response#status_message
55
+ # Returns response status message returned from API as a `String`.
56
+ #
57
+ # ```rb
58
+ # response.status_message #=> "OK"
59
+ # ```
60
+ #
61
+ def status_message
62
+ Rack::Utils::HTTP_STATUS_CODES[status]
63
+ end
64
+
65
+ # ### LineNotify::Response#success?
66
+ # Returns success boolean value from API as a `TrueClass` or `FalseClass`.
67
+ #
68
+ # ```rb
69
+ # response.success? #=> true
70
+ # ```
71
+ #
72
+ def success?
73
+ status == HTTP_STATUS_SUCCESS
74
+ end
75
+
76
+ # ### LineNotify::Response#status
77
+ # Returns response status from API as a `TrueClass` or `FalseClass`.
78
+ #
79
+ # ```rb
80
+ # response.success? #=> true
81
+ # ```
82
+ #
83
+ def error?
84
+ !success?
85
+ end
86
+
87
+ def bad_request?
88
+ status == HTTP_STATUS_BAD_REQUEST
89
+ end
90
+
91
+ def token_expired?
92
+ status == HTTP_STATUS_TOKEN_EXPIRED
93
+ end
94
+
95
+ def internal_error?
96
+ status == HTTP_STATUS_INTERNAL_ERROR
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LineNotify
4
+ VERSION = '1.0.0'
5
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ $:.push File.expand_path('../lib', __FILE__)
4
+
5
+ require 'line_notify_client/version'
6
+
7
+ Gem::Specification.new do |s|
8
+ s.name = 'line_notify_client'
9
+ s.version = LineNotify::VERSION
10
+ s.authors = ['Shojiro Yanagisawa']
11
+ s.email = ['nipe0324@gmail.com']
12
+ s.homepage = 'https://github.com/Findy/line_notify_client'
13
+ s.summary = 'LINE Notify API client library'
14
+ s.description = 'LINE Notify API client library'
15
+ s.licenses = ['MIT']
16
+
17
+ s.files = Dir.chdir(File.expand_path('..', __FILE__)) do
18
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
19
+ end
20
+ s.require_paths = ["lib"]
21
+
22
+ s.add_dependency 'faraday', '~> 0.13'
23
+ s.add_dependency 'faraday_middleware', '~> 0.12'
24
+
25
+ s.add_development_dependency 'rake', '~> 10.0'
26
+ s.add_development_dependency 'rspec', '~> 3.0'
27
+ s.add_development_dependency 'webmock'
28
+ end
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: line_notify_client
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Shojiro Yanagisawa
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-01-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.13'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.13'
27
+ - !ruby/object:Gem::Dependency
28
+ name: faraday_middleware
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.12'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.12'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.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: LINE Notify API client library
84
+ email:
85
+ - nipe0324@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - Gemfile
93
+ - Gemfile.lock
94
+ - LICENSE
95
+ - README.md
96
+ - Rakefile
97
+ - lib/line_notify_client.rb
98
+ - lib/line_notify_client/error.rb
99
+ - lib/line_notify_client/http_client.rb
100
+ - lib/line_notify_client/response.rb
101
+ - lib/line_notify_client/version.rb
102
+ - line_notify_client.gemspec
103
+ homepage: https://github.com/Findy/line_notify_client
104
+ licenses:
105
+ - MIT
106
+ metadata: {}
107
+ post_install_message:
108
+ rdoc_options: []
109
+ require_paths:
110
+ - lib
111
+ required_ruby_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ requirements: []
122
+ rubyforge_project:
123
+ rubygems_version: 2.7.6
124
+ signing_key:
125
+ specification_version: 4
126
+ summary: LINE Notify API client library
127
+ test_files: []