spearly-sdk-ruby 0.10.0 → 0.11.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0ea587fbe14cca4cebc1b12a299ee8ddaa5c64df3c50f51ff4989b1d18094990
4
- data.tar.gz: 14e9537659004b0e52c193a4f0708407b31d2d9f77909352326351fb8f87329c
3
+ metadata.gz: ffd3333dfa37f250288b37eb593db42db89846aa436397371c403a20ec9fe5e9
4
+ data.tar.gz: d1b0eeab87ff53e34bbd1b7c00b8b691e679823c2b09ad5df713003691349d7a
5
5
  SHA512:
6
- metadata.gz: 60dae3572451b45f445cd4ee7487ddeec830bc274e9dead7264a007babc6491732fb4bd1cac5e11e5caa2c2a8a5a8e5fca7f7086bd729dbe356e918113e3b16c
7
- data.tar.gz: 94a932fdc203d988badf93296cef0fd808d08abe0856903ffc85aca5210d58f28e1d5de6bbc1cb19447696cbebca0e9c10ef3f91462684fc2d99854ca18498ce
6
+ metadata.gz: 0e1a2419d6013bac9e3adbb347afd731fc04a9b1a85c87c41e11c7fbec6ade89db3ca2b3d54adfe5c9a1720a680e22968a1df5960901e2cf76cc29d2e1bf9b85
7
+ data.tar.gz: 3c19e93be4e91f9cc17333a5d4a7d7233f678a4a5b9db393bb5122c26ea1a5d2d4df3d2a3848b177097e606938270519a3e2a3ede0b232af40219bda848e39aa
@@ -1,16 +1,21 @@
1
1
  name: Ruby
2
- on: [push]
2
+ on:
3
+ push:
4
+ branches:
5
+ - '**'
3
6
  jobs:
4
7
  build:
5
- runs-on: ubuntu-latest
8
+ runs-on: ubuntu-22.04
6
9
  steps:
7
- - uses: actions/checkout@v2
8
- - name: Set up Ruby
10
+ - name: Checkout code
11
+ uses: actions/checkout@v3
12
+
13
+ - name: Setup Ruby
9
14
  uses: ruby/setup-ruby@v1
10
15
  with:
11
- ruby-version: 2.7.7
16
+ ruby-version: 3.1.4
17
+
12
18
  - name: Run the default task
13
19
  run: |
14
- gem install bundler -v 2.3.26
15
20
  bundle install
16
21
  bundle exec rake
data/.rubocop.yml CHANGED
@@ -2,7 +2,7 @@ require:
2
2
  - rubocop-rspec
3
3
  AllCops:
4
4
  NewCops: enable
5
- TargetRubyVersion: 2.7
5
+ TargetRubyVersion: 3.1
6
6
  Exclude:
7
7
  - 'bin/**/*'
8
8
  Layout/EmptyLineBetweenDefs:
@@ -16,6 +16,8 @@ Metrics/ClassLength:
16
16
  Enabled: false
17
17
  Metrics/MethodLength:
18
18
  Enabled: false
19
+ Naming/AccessorMethodName:
20
+ Enabled: false
19
21
  RSpec/BeNil:
20
22
  EnforcedStyle: 'be'
21
23
  RSpec/ExampleLength:
data/CHANGELOG.md CHANGED
@@ -1,3 +1,14 @@
1
+ ## [0.11.1] - 2023-09-27
2
+
3
+ - Implement a reader for `Spearly::Auth::Client#token` [#18](https://github.com/unimal-jp/spearly-sdk-ruby/pull/18)
4
+
5
+ ## [0.11.0] - 2023-09-23
6
+
7
+ - Throw errors `from Spearly::Auth` methods [#15](https://github.com/unimal-jp/spearly-sdk-ruby/pull/15)
8
+ - `Spearly::Auth::Client` refactors [#14](https://github.com/unimal-jp/spearly-sdk-ruby/pull/14)
9
+ - Upgrade Ruby to 3.1.4 [#13](https://github.com/unimal-jp/spearly-sdk-ruby/pull/13)
10
+ - Remove deprecated classes and helpers [#12](https://github.com/unimal-jp/spearly-sdk-ruby/pull/12)
11
+
1
12
  ## [0.10.0] - 2022-12-05
2
13
 
3
14
  - `Spearly::Auth::Client#get_team_subscriptions()`
data/Gemfile CHANGED
@@ -3,4 +3,11 @@
3
3
  source 'https://rubygems.org'
4
4
  gemspec
5
5
 
6
- gem 'rails', '>= 6.0', '< 7.1'
6
+ group :development do
7
+ gem 'bundler-audit', '~> 0.9'
8
+ gem 'fuubar', '~> 2.5'
9
+ gem 'pry', '~> 0.13'
10
+ gem 'rspec', '~> 3.10'
11
+ gem 'rubocop', '~> 1.15'
12
+ gem 'rubocop-rspec', '~> 2.3'
13
+ end
data/README.md CHANGED
@@ -50,7 +50,7 @@ git push origin --tags
50
50
  ## Publish
51
51
  ### Build
52
52
  ```sh
53
- rake build
53
+ bundle exec rake build
54
54
  ```
55
55
 
56
56
  ### Push to RubyGems
data/Rakefile CHANGED
@@ -7,6 +7,8 @@ RSpec::Core::RakeTask.new(:spec)
7
7
 
8
8
  require 'rubocop/rake_task'
9
9
 
10
- RuboCop::RakeTask.new
10
+ RuboCop::RakeTask.new(:rubocop) do |task|
11
+ task.options = ['--autocorrect-all']
12
+ end
11
13
 
12
14
  task default: %i[spec rubocop]
@@ -1,7 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'signet/oauth_2/client'
4
- require 'active_support/core_ext/hash/indifferent_access'
5
4
 
6
5
  module Spearly
7
6
  module Auth
@@ -1,176 +1,53 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'team_member'
4
+ require_relative 'team_subscription'
5
+ require_relative 'team'
6
+ require_relative 'token'
7
+ require_relative 'user'
8
+
3
9
  module Spearly
4
10
  module Auth
5
11
  class Client
6
- def initialize(token)
7
- @token = token
8
- end
9
-
10
- def get_user(user_id)
11
- uri = "#{ENV.fetch('SPEARLY_API_URL', nil)}/users/#{user_id}"
12
- uri_parsed = Addressable::URI.parse(uri).normalize.to_s
13
- client = Faraday.default_connection
14
-
15
- res = client.get(uri_parsed,
16
- nil,
17
- 'Accept' => 'application/vnd.spearly.v2+json',
18
- 'Authorization' => @token)
19
-
20
- if res.status == 200
21
- JSON.parse(res.body)['data']
22
- else
23
- {}
24
- end
25
- end
26
-
27
- def create_team(params)
28
- uri = "#{ENV.fetch('SPEARLY_API_URL', nil)}/teams"
29
- uri_parsed = Addressable::URI.parse(uri).normalize.to_s
30
- client = Faraday.default_connection
31
-
32
- res = client.post(uri_parsed,
33
- params.as_json,
34
- 'Accept' => 'application/vnd.spearly.v2+json',
35
- 'Authorization' => @token)
36
-
37
- case res.status
38
- when 201
39
- JSON.parse(res.body)['data']
40
- when 422
41
- JSON.parse(res.body)
42
- else
43
- {}
44
- end
45
- end
46
-
47
- def get_team(team_id)
48
- uri = "#{ENV.fetch('SPEARLY_API_URL', nil)}/teams/#{team_id}"
49
- uri_parsed = Addressable::URI.parse(uri).normalize.to_s
50
- client = Faraday.default_connection
51
-
52
- res = client.get(uri_parsed,
53
- nil,
54
- 'Accept' => 'application/vnd.spearly.v2+json',
55
- 'Authorization' => @token)
56
-
57
- if res.status == 200
58
- JSON.parse(res.body)['data']
59
- else
60
- {}
61
- end
62
- end
63
-
64
- def get_teams(params = {})
65
- uri = "#{ENV.fetch('SPEARLY_API_URL', nil)}/teams"
66
- uri_parsed = Addressable::URI.parse(uri).normalize.to_s
67
- client = Faraday.default_connection
68
-
69
- res = client.get(uri_parsed,
70
- params.as_json,
71
- 'Accept' => 'application/vnd.spearly.v2+json',
72
- 'Authorization' => @token)
73
-
74
- if res.status == 200
75
- JSON.parse(res.body)['data']
76
- else
77
- []
78
- end
79
- end
80
-
81
- def update_team(team_id, params)
82
- uri = "#{ENV.fetch('SPEARLY_API_URL', nil)}/teams/#{team_id}"
83
- uri_parsed = Addressable::URI.parse(uri).normalize.to_s
84
- client = Faraday.default_connection
85
-
86
- res = client.put(uri_parsed,
87
- params.as_json,
88
- 'Accept' => 'application/vnd.spearly.v2+json',
89
- 'Authorization' => @token)
90
-
91
- return unless res.status == 200
92
-
93
- JSON.parse(res.body)['data']
94
- end
95
-
96
- def regenerate_team_api_access_token(team_id)
97
- uri = "#{ENV.fetch('SPEARLY_API_URL', nil)}/teams/#{team_id}/regenerate_api_access_token"
98
- uri_parsed = Addressable::URI.parse(uri).normalize.to_s
99
- client = Faraday.default_connection
100
-
101
- res = client.post(uri_parsed,
102
- nil,
103
- 'Accept' => 'application/vnd.spearly.v2+json',
104
- 'Authorization' => @token)
105
-
106
- return unless res.status == 200
107
-
108
- JSON.parse(res.body)['data']
109
- end
110
-
111
- def invite_user_to_team(team_id, params)
112
- uri = "#{ENV.fetch('SPEARLY_API_URL', nil)}/teams/#{team_id}/invite"
113
- uri_parsed = Addressable::URI.parse(uri).normalize.to_s
114
- client = Faraday.default_connection
12
+ include Team
13
+ include TeamMember
14
+ include TeamSubscription
15
+ include Token
16
+ include User
115
17
 
116
- res = client.post(uri_parsed,
117
- params.as_json,
118
- 'Accept' => 'application/vnd.spearly.v2+json',
119
- 'Authorization' => @token)
18
+ attr_reader :token
120
19
 
121
- return unless res.status == 201
20
+ def initialize(token)
21
+ @token = token
122
22
 
123
- JSON.parse(res.body)['data']
23
+ base_url = ENV.fetch('SPEARLY_API_URL', nil)
24
+ @client = Faraday.new(base_url)
124
25
  end
125
26
 
126
- def get_team_members(team_id)
127
- uri = "#{ENV.fetch('SPEARLY_API_URL', nil)}/teams/#{team_id}/members"
128
- uri_parsed = Addressable::URI.parse(uri).normalize.to_s
129
- client = Faraday.default_connection
27
+ def run_request(method:, path:, params: nil)
28
+ path_parsed = Addressable::URI.parse(path).normalize.to_s
130
29
 
131
- res = client.get(uri_parsed,
132
- nil,
133
- 'Accept' => 'application/vnd.spearly.v2+json',
134
- 'Authorization' => @token)
135
-
136
- if res.status == 200
137
- JSON.parse(res.body)['data']
138
- else
139
- []
140
- end
30
+ @client.run_request(method.to_sym,
31
+ path_parsed,
32
+ params,
33
+ 'Accept' => 'application/vnd.spearly.v2+json',
34
+ 'Authorization' => @token)
35
+ rescue Faraday::ConnectionFailed => e
36
+ raise ServerError, e.message
141
37
  end
142
38
 
143
- def get_team_features(team_id)
144
- uri = "#{ENV.fetch('SPEARLY_API_URL', nil)}/teams/#{team_id}/features"
145
- uri_parsed = Addressable::URI.parse(uri).normalize.to_s
146
- client = Faraday.default_connection
147
-
148
- res = client.get(uri_parsed,
149
- nil,
150
- 'Accept' => 'application/vnd.spearly.v2+json',
151
- 'Authorization' => @token)
152
-
153
- if res.status == 200
154
- JSON.parse(res.body)['data']
155
- else
156
- {}
157
- end
158
- end
159
-
160
- def get_team_subscriptions(team_id, params)
161
- uri = "#{ENV.fetch('SPEARLY_API_URL', nil)}/teams/#{team_id}/subscriptions"
162
- uri_parsed = Addressable::URI.parse(uri).normalize.to_s
163
- client = Faraday.default_connection
164
-
165
- res = client.get(uri_parsed,
166
- params.as_json,
167
- 'Accept' => 'application/vnd.spearly.v2+json',
168
- 'Authorization' => @token)
169
-
170
- if res.status == 200
171
- JSON.parse(res.body)
39
+ def process_response(response)
40
+ case response.status
41
+ when 200, 201, 422
42
+ JSON.parse(response.body)
43
+ when 401
44
+ raise AuthorizationError, 'Unauthorized'
45
+ when 404
46
+ raise NotFoundError, 'Not Found'
47
+ when 503
48
+ raise ServiceUnavailableError, 'Service Unavailable'
172
49
  else
173
- []
50
+ raise ServerError, 'Server Error'
174
51
  end
175
52
  end
176
53
  end
@@ -2,27 +2,58 @@
2
2
 
3
3
  module Spearly
4
4
  module Auth
5
- class Team
6
- attr_reader :data
5
+ module Team
6
+ # POST /teams
7
+ def create_team(params)
8
+ response = run_request(
9
+ :method => :post,
10
+ path: '/teams',
11
+ params: params
12
+ )
7
13
 
8
- def initialize(data)
9
- @data = data
14
+ process_response(response)
10
15
  end
11
16
 
12
- def method_missing(name, *_args)
13
- @data[name.to_s]
17
+ # GET /teams/:team_id
18
+ def get_team(team_id)
19
+ response = run_request(
20
+ :method => :get,
21
+ path: "/teams/#{team_id}"
22
+ )
23
+
24
+ process_response(response)
14
25
  end
15
26
 
16
- def respond_to_missing?(_name, *_args)
17
- true
27
+ # GET /teams
28
+ def get_teams(params)
29
+ response = run_request(
30
+ :method => :get,
31
+ path: '/teams',
32
+ params: params
33
+ )
34
+
35
+ process_response(response)
18
36
  end
19
37
 
20
- def to_json(*_args)
21
- @data.to_json
38
+ # PUT /teams/:team_id
39
+ def update_team(team_id, params)
40
+ response = run_request(
41
+ :method => :put,
42
+ path: "/teams/#{team_id}",
43
+ params: params
44
+ )
45
+
46
+ process_response(response)
22
47
  end
23
48
 
24
- def as_json
25
- @data.as_json
49
+ # GET /teams/:team_id/regenerate_api_access_token
50
+ def regenerate_team_api_access_token(team_id)
51
+ response = run_request(
52
+ :method => :get,
53
+ path: "/teams/#{team_id}/regenerate_api_access_token"
54
+ )
55
+
56
+ process_response(response)
26
57
  end
27
58
  end
28
59
  end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Spearly
4
+ module Auth
5
+ module TeamMember
6
+ # POST /teams/:team_id/invite
7
+ def invite_user_to_team(team_id, params)
8
+ response = run_request(
9
+ :method => :post,
10
+ path: "/teams/#{team_id}/invite",
11
+ params: params
12
+ )
13
+
14
+ process_response(response)
15
+ end
16
+
17
+ # GET /teams/:team_id/members
18
+ def get_team_members(team_id)
19
+ response = run_request(
20
+ :method => :get,
21
+ path: "/teams/#{team_id}/members"
22
+ )
23
+
24
+ process_response(response)
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Spearly
4
+ module Auth
5
+ module TeamSubscription
6
+ # GET /teams/:team_id/subscriptions
7
+ def get_team_subscriptions(team_id, params)
8
+ response = run_request(
9
+ :method => :get,
10
+ path: "/teams/#{team_id}/subscriptions",
11
+ params: params
12
+ )
13
+
14
+ process_response(response)
15
+ end
16
+ end
17
+ end
18
+ end
@@ -1,61 +1,29 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'addressable/uri'
4
- require 'faraday'
5
-
6
3
  module Spearly
7
4
  module Auth
8
- class Token
9
- attr_reader :data, :token
10
-
11
- def initialize(token)
12
- @token = token
13
- end
14
-
15
- def method_missing(name, *_args)
16
- @data[name.to_s]
5
+ module Token
6
+ # GET /oauth/token/info
7
+ def get_token_info
8
+ response = run_request(
9
+ :method => :get,
10
+ path: '/oauth/token/info'
11
+ )
12
+
13
+ process_response(response)
17
14
  end
18
15
 
19
- def respond_to_missing?(_name, *_args)
20
- true
21
- end
22
-
23
- def info
24
- token_info_uri = "#{ENV.fetch('SPEARLY_API_URL', nil)}/oauth/token/info"
25
- url = Addressable::URI.parse(token_info_uri).normalize.to_s
26
- client = Faraday.default_connection
27
-
28
- res = client.get(url,
29
- nil,
30
- 'Accept' => 'application/vnd.spearly.v2+json',
31
- 'Authorization' => @token)
32
-
33
- return unless res.status == 200
16
+ # POST /oauth/revoke
17
+ def revoke_token(params)
18
+ params[:token] = @token.gsub(/^Bearer /i, '')
34
19
 
35
- @data = JSON.parse(res.body)
36
- end
37
-
38
- def revoke(params)
39
- token_revoke_uri = "#{ENV.fetch('SPEARLY_API_URL', nil)}/oauth/revoke"
40
- url = Addressable::URI.parse(token_revoke_uri).normalize.to_s
41
- client = Faraday.default_connection
42
-
43
- bearer_token = @token.gsub(/^Bearer /i, '')
44
-
45
- client.post(url,
46
- {
47
- client_id: params[:client_id],
48
- client_secret: params[:client_secret],
49
- token: bearer_token
50
- })
51
- end
52
-
53
- def to_json(*_args)
54
- @data.to_json
55
- end
20
+ response = run_request(
21
+ :method => :post,
22
+ path: '/oauth/revoke',
23
+ params: params
24
+ )
56
25
 
57
- def as_json
58
- @data.as_json
26
+ process_response(response)
59
27
  end
60
28
  end
61
29
  end
@@ -1,65 +1,27 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'addressable/uri'
4
- require 'faraday'
5
-
6
3
  module Spearly
7
4
  module Auth
8
- class User
9
- attr_reader :data
10
-
11
- def initialize(token)
12
- @token = token
13
- end
14
-
15
- def method_missing(name, *_args)
16
- @data[name.to_s]
17
- end
18
-
19
- def respond_to_missing?(_name, *_args)
20
- true
21
- end
22
-
23
- def find
24
- user_profile_uri = "#{ENV.fetch('SPEARLY_API_URL', nil)}/profile"
25
- url = Addressable::URI.parse(user_profile_uri).normalize.to_s
26
- client = Faraday.default_connection
27
-
28
- res = client.get(url,
29
- nil,
30
- 'Accept' => 'application/vnd.spearly.v2+json',
31
- 'Authorization' => @token)
32
-
33
- return unless res.status == 200
34
-
35
- @data = JSON.parse(res.body)['data']
5
+ module User
6
+ # GET /users/:user_id
7
+ # requires SPEARLY_API_ACCESS_TOKEN as token
8
+ def get_user(user_id)
9
+ response = run_request(
10
+ :method => :get,
11
+ path: "/users/#{user_id}"
12
+ )
13
+
14
+ process_response(response)
36
15
  end
37
16
 
38
- def teams
39
- teams_uri = "#{ENV.fetch('SPEARLY_API_URL', nil)}/teams"
40
- url = Addressable::URI.parse(teams_uri).normalize.to_s
41
- client = Faraday.default_connection
42
-
43
- res = client.get(url,
44
- nil,
45
- 'Accept' => 'application/vnd.spearly.v2+json',
46
- 'Authorization' => @token)
47
-
48
- return [] unless res.status == 200
49
-
50
- team_hashes = JSON.parse(res.body)['data']
51
-
52
- team_hashes.map do |team_hash|
53
- Spearly::Auth::Team.new(team_hash)
54
- end
55
- end
56
-
57
- def to_json(*_args)
58
- @data.to_json
59
- end
17
+ # GET /profile
18
+ def get_profile
19
+ response = run_request(
20
+ :method => :get,
21
+ path: '/profile'
22
+ )
60
23
 
61
- def as_json
62
- @data.as_json
24
+ process_response(response)
63
25
  end
64
26
  end
65
27
  end
data/lib/spearly/auth.rb CHANGED
@@ -2,20 +2,13 @@
2
2
 
3
3
  require_relative 'auth/authorizer'
4
4
  require_relative 'auth/client'
5
- require_relative 'auth/engine'
6
- require_relative 'auth/oauth_user'
7
- require_relative 'auth/team'
8
- require_relative 'auth/token'
9
- require_relative 'auth/user'
10
5
 
11
6
  module Spearly
12
7
  module Auth
13
8
  class Error < StandardError; end
14
9
  class AuthorizationError < Error; end
10
+ class NotFoundError < Error; end
15
11
  class ServerError < Error; end
16
-
17
- module Rails
18
- autoload :Helpers, 'spearly/auth/rails/helpers'
19
- end
12
+ class ServiceUnavailableError < Error; end
20
13
  end
21
14
  end
@@ -7,13 +7,13 @@ module Spearly
7
7
  @token = token
8
8
  end
9
9
 
10
- def get_sites(params = {})
10
+ def get_sites(params)
11
11
  uri = "#{ENV.fetch('SPEARLY_API_URL', nil)}/sites"
12
12
  uri_parsed = Addressable::URI.parse(uri).normalize.to_s
13
13
  client = Faraday.default_connection
14
14
 
15
15
  res = client.get(uri_parsed,
16
- params.as_json,
16
+ params,
17
17
  'Accept' => 'application/vnd.spearly.v2+json',
18
18
  'Authorization' => @token)
19
19
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Spearly
4
- VERSION = '0.10.0'
4
+ VERSION = '0.11.1'
5
5
  end