timedoctor 0.2.0 → 0.3.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: 18ec15218465099f9b2dc4ecaea38ee6865185ec641f2993f98d5a4ad13c4bb9
4
- data.tar.gz: f689d43de75653e747524abd2f7937a134917882c44638c76ba24314d013ce06
3
+ metadata.gz: 1863262f810d0a719b977fe636c3533d7466f0a34d39ab89f72384c9ef0ca5b7
4
+ data.tar.gz: 74229ed160f0c670c813975561b54009b5d0be70d1ed63642d9f41f7a6ba8e4c
5
5
  SHA512:
6
- metadata.gz: 7ed21cca98983b5edfc5178e9dff3f352dbe0e3c1cfa56edadc1f3c78d354a9e097ee8b9c68c6f80669949794526a99481dc7ed3146b2c3749c380b57abc1d0c
7
- data.tar.gz: d4ac18f10dffc9a22687493a7b8dca406a0a3b2992dd3cb72696d8e2730ac28824458f429cff602a05c158d62d3527fd3bf2e2ddcdfd0915dcb8a7f24ebf78a4
6
+ metadata.gz: f52af45bb440b0a8d7b408cd70d867e7153c74934e46ee850be47512680ddf851856733af4c745707f330f18a2be40d72205a8f5a62d881d04f0288bd0b1053a
7
+ data.tar.gz: f1158310c37b58803bc15bc7862e1f422e1c81ccdf9df38b305ecb9f47ca0bec0cfa34182eb8d4f64e1de3e264be1d43f4edb112f3679c4e7ecebe910cd0fd0c
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- timedoctor (0.2.0)
4
+ timedoctor (0.3.1)
5
5
  faraday (~> 0.15.0)
6
6
 
7
7
  GEM
@@ -13,30 +13,32 @@ require 'timedoctor'
13
13
  # require 'irb'
14
14
  # IRB.start(__FILE__)
15
15
  #
16
- # config = {
17
- # client_id: '1091_4hnloyc6pqqscckokws808o8soocko4c44owwws404g8k4sgos',
18
- # client_secret: '7wvmks0khckcw40s00cssg4w8s404o8g0g8k00cgo8kow0cow',
19
- #
16
+ config = {
17
+ client_id: '1091_4hnloyc6pqqscckokws808o8soocko4c44owwws404g8k4sgos',
18
+ client_secret: '7wvmks0khckcw40s00cssg4w8s404o8g0g8k00cgo8kow0cow',
19
+
20
20
  # access_token:
21
21
  # "Y2ZkMGY5OTA1NGI3NmJjZTZlYWJlYjczNGM4N2E2NTYwZjFmNzhjZGVhMGY3ZmU0MTIyZjQ5NTNkOGIzYWI3ZA",
22
22
  # refresh_token:
23
23
  # "ZmM3OTg1NjZmYjgwYmZlYzM2NzAwMjFlZmFhZmU4MDk1Y2ZhZWNlYjMyYTg0NzlmMzRmNzBiOWM5NTUxNjY4MA",
24
24
  # user_id: 1188995,
25
25
  # company_id: 587525,
26
- #
27
- # on_token_refresh: ->(data, conf) do
28
- # p 'SUCCESS'
29
- #
30
- # p data
31
- #
32
- # conf[:access_token] = data[:access_token]
33
- # conf[:refresh_token] = data[:refresh_token]
34
- # end,
35
- #
36
- # on_token_refresh_error: ->(data, conf) { p "FAILURE" }
37
- # }
38
- #
39
- # client = TimeDoctor::Client.new(config)
26
+
27
+ # on_token_refresh: ->(data, conf) do
28
+ # p 'SUCCESS'
29
+ #
30
+ # p data
31
+ #
32
+ # conf[:access_token] = data[:access_token]
33
+ # conf[:refresh_token] = data[:refresh_token]
34
+ # end,
35
+ #
36
+ # on_token_refresh_error: ->(data, conf) { p "FAILURE" }
37
+ on_token_authorize_error: ->(data, conf) { p "FAILURE" }
38
+ }
39
+
40
+ p TimeDoctor::Auth.new(config).fetch_token
41
+ # p TimeDoctor::Client.new(config).authorize
40
42
  #
41
43
  # # p client.companies.list
42
44
  #
@@ -6,3 +6,4 @@ require 'timedoctor/exceptions'
6
6
  require 'timedoctor/config'
7
7
  require 'timedoctor/worker'
8
8
  require 'timedoctor/token'
9
+ require 'timedoctor/auth'
@@ -0,0 +1,13 @@
1
+ module TimeDoctor
2
+ class Auth
3
+ def initialize(config)
4
+ @config = Config.new(config)
5
+ end
6
+
7
+ attr_reader :config
8
+
9
+ def fetch_token
10
+ TimeDoctor::Token.new(config).fetch
11
+ end
12
+ end
13
+ end
@@ -1,51 +1,57 @@
1
1
  module TimeDoctor
2
2
  class Token
3
- class << self
4
- ENTRY = 'https://webapi.timedoctor.com/oauth/v2/token'.freeze
5
-
6
- def authorize(config)
7
- response = Faraday.get ENTRY,
8
- client_id: config[:client_id],
9
- client_secret: config[:client_secret],
10
- code: config[:code],
11
- redirect_uri: config[:redirect_uri],
12
- grant_type: :authorization_code,
13
- _format: :json
14
-
15
- case response.status
16
- when 200
17
- data = JSON.parse(response.body, symbolize_names: true)
18
- config[:access_token] = data[:access_token]
19
- config[:refresh_token] = data[:refresh_token]
20
- config[:on_token_authorize].call(data, config)
21
- when 400
22
- data = JSON.parse(response.body, symbolize_names: true)
23
- config[:on_token_authorize_error].call(data, config)
24
- else
25
- raise UnknownError, response
26
- end
3
+ ENTRY = 'https://webapi.timedoctor.com/oauth/v2/token'.freeze
4
+
5
+ def initialize(config)
6
+ @config = config
7
+ end
8
+
9
+ attr_reader :config
10
+
11
+ def fetch
12
+ response = Faraday.get ENTRY,
13
+ client_id: config[:client_id],
14
+ client_secret: config[:client_secret],
15
+ code: config[:code],
16
+ redirect_uri: config[:redirect_uri],
17
+ grant_type: :authorization_code,
18
+ _format: :json
19
+
20
+ data = JSON.parse(response.body, symbolize_names: true)
21
+
22
+ case response.status
23
+ when 200
24
+ config[:access_token] = data[:access_token]
25
+ config[:refresh_token] = data[:refresh_token]
26
+ config[:on_token_authorize].call(data, config)
27
+ data
28
+ when 400
29
+ config[:on_token_authorize_error].call(data, config)
30
+ else
31
+ raise UnknownError, response
27
32
  end
33
+ end
34
+
35
+ def refresh
36
+ response = Faraday.get ENTRY,
37
+ refresh_token: config[:refresh_token],
38
+ client_id: config[:client_id],
39
+ client_secret: config[:client_secret],
40
+ grant_type: :refresh_token,
41
+ _format: :json
42
+
43
+ data = JSON.parse(response.body, symbolize_names: true)
28
44
 
29
- def refresh(config)
30
- response = Faraday.get ENTRY,
31
- refresh_token: config[:refresh_token],
32
- client_id: config[:client_id],
33
- client_secret: config[:client_secret],
34
- grant_type: :refresh_token,
35
- _format: :json
36
-
37
- case response.status
38
- when 200
39
- data = JSON.parse(response.body, symbolize_names: true)
40
- config[:access_token] = data[:access_token]
41
- config[:refresh_token] = data[:refresh_token]
42
- config[:on_token_refresh].call(data, config)
43
- when 400
44
- data = JSON.parse(response.body, symbolize_names: true)
45
- config[:on_token_refresh_error].call(data, config)
46
- else
47
- raise UnknownError, response
48
- end
45
+ case response.status
46
+ when 200
47
+ config[:access_token] = data[:access_token]
48
+ config[:refresh_token] = data[:refresh_token]
49
+ config[:on_token_refresh].call(data, config)
50
+ data
51
+ when 400
52
+ config[:on_token_refresh_error].call(data, config)
53
+ else
54
+ raise UnknownError, response
49
55
  end
50
56
  end
51
57
  end
@@ -5,13 +5,13 @@ module TimeDoctor
5
5
  attr_reader :config, :conn
6
6
 
7
7
  def initialize(config)
8
- raise EmptyAccessToken unless config[:access_token]
9
-
10
8
  @config = config
11
9
  @conn = Faraday.new(url: ENTRY)
12
10
  end
13
11
 
14
12
  def exchange(method, url, params = {})
13
+ raise EmptyAccessToken unless config[:access_token]
14
+
15
15
  params[:access_token] = config[:access_token]
16
16
  params[:_format] = :json
17
17
 
@@ -21,7 +21,7 @@ module TimeDoctor
21
21
  when 200
22
22
  JSON.parse(response.body, symbolize_names: true)
23
23
  when 401
24
- Token.refresh(config)
24
+ Token.new(config).refresh
25
25
  exchange(method, url, params)
26
26
  else
27
27
  raise UnknownError, response
@@ -1,3 +1,3 @@
1
1
  module TimeDoctor
2
- VERSION = '0.2.0'.freeze
2
+ VERSION = '0.3.1'.freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: timedoctor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eugene Yak
@@ -142,6 +142,7 @@ files:
142
142
  - bin/console
143
143
  - bin/setup
144
144
  - lib/timedoctor.rb
145
+ - lib/timedoctor/auth.rb
145
146
  - lib/timedoctor/client.rb
146
147
  - lib/timedoctor/config.rb
147
148
  - lib/timedoctor/core/absent_late.rb