timedoctor 0.1.0 → 0.2.0

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: f001d7dff95468e48afdcd400137316259db0879011926b82303ee0121f79a7c
4
- data.tar.gz: f36f2d190fb846a05673d7b99db8a3d4f4874682c503b6e8aada23c550dc5265
3
+ metadata.gz: 18ec15218465099f9b2dc4ecaea38ee6865185ec641f2993f98d5a4ad13c4bb9
4
+ data.tar.gz: f689d43de75653e747524abd2f7937a134917882c44638c76ba24314d013ce06
5
5
  SHA512:
6
- metadata.gz: 2dc32f33dd81c6d8f2a95229a4da8bf1d7648d715f8ff3417cb2c1704a6a7b38ef8e09ee10371ac5d23d8cac4fff6d8d50324b65d7db412075f529ad8d6f4714
7
- data.tar.gz: a76fe0fe980c5a055bf5fa79e2d586ead6a69ec153032dd78403d08a7f10b36196885eecdfebfd98bf1c76668179a43e25e0df736f880a277e9e7d9786f9fe3d
6
+ metadata.gz: 7ed21cca98983b5edfc5178e9dff3f352dbe0e3c1cfa56edadc1f3c78d354a9e097ee8b9c68c6f80669949794526a99481dc7ed3146b2c3749c380b57abc1d0c
7
+ data.tar.gz: d4ac18f10dffc9a22687493a7b8dca406a0a3b2992dd3cb72696d8e2730ac28824458f429cff602a05c158d62d3527fd3bf2e2ddcdfd0915dcb8a7f24ebf78a4
data/.gitignore CHANGED
@@ -10,3 +10,5 @@
10
10
 
11
11
  # rspec failure tracking
12
12
  .rspec_status
13
+
14
+ timedoctor-*.gem
@@ -13,4 +13,7 @@ Metrics/MethodLength:
13
13
 
14
14
  Lint/ScriptPermission:
15
15
  Exclude:
16
- - bin/console
16
+ - bin/console
17
+
18
+ Metrics/AbcSize:
19
+ Max: 20
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- timedoctor (0.1.0)
4
+ timedoctor (0.2.0)
5
5
  faraday (~> 0.15.0)
6
6
 
7
7
  GEM
@@ -18,7 +18,7 @@ GEM
18
18
  safe_yaml (~> 1.0.0)
19
19
  diff-lcs (1.3)
20
20
  docile (1.3.0)
21
- faraday (0.15.0)
21
+ faraday (0.15.1)
22
22
  multipart-post (>= 1.2, < 3)
23
23
  hashdiff (0.3.7)
24
24
  json (2.1.0)
@@ -3,8 +3,6 @@
3
3
  require 'bundler/setup'
4
4
  require 'timedoctor'
5
5
 
6
- require 'date'
7
-
8
6
  # You can add fixtures and/or initialization code here to make experimenting
9
7
  # with your gem easier. You can also use a different console, if you like.
10
8
 
@@ -14,21 +12,35 @@ require 'date'
14
12
 
15
13
  # require 'irb'
16
14
  # IRB.start(__FILE__)
17
-
18
-
19
- TimeDoctor::Config.configure do |conf|
20
- conf.client_id = '1091_4hnloyc6pqqscckokws808o8soocko4c44owwws404g8k4sgos'
21
- conf.client_secret = '7wvmks0khckcw40s00cssg4w8s404o8g0g8k00cgo8kow0cow'
22
-
23
- conf.on_token_refresh = lambda do |data, _payload|
24
- p 'REFRESH'
25
- p data[:access_token], data[:refresh_token]
26
- end
27
- end
28
-
29
- client = TimeDoctor::Client.new access_token: 'MjQ5NDUzYTlhZGJjMDc0OGZlNTQwZjJkY2RmZGYwMjM0OWIxNmIwMmJhMWQ1YWZlZWM0NzRjNjI2NjIwNDk4YQ',
30
- refresh_token: 'YmUxOTFjNjE1Njc0YzRmNDYyZGIzZDZlYzFmZWU0MzViNDBmNjIwNDI1NGVhNGQ3ZThkNTNjOTIwNmZkNzNjNA'
31
-
32
- p client.companies.list[:user][:full_name]
33
-
34
- # TimeDoctor::Client.new('token')
15
+ #
16
+ # config = {
17
+ # client_id: '1091_4hnloyc6pqqscckokws808o8soocko4c44owwws404g8k4sgos',
18
+ # client_secret: '7wvmks0khckcw40s00cssg4w8s404o8g0g8k00cgo8kow0cow',
19
+ #
20
+ # access_token:
21
+ # "Y2ZkMGY5OTA1NGI3NmJjZTZlYWJlYjczNGM4N2E2NTYwZjFmNzhjZGVhMGY3ZmU0MTIyZjQ5NTNkOGIzYWI3ZA",
22
+ # refresh_token:
23
+ # "ZmM3OTg1NjZmYjgwYmZlYzM2NzAwMjFlZmFhZmU4MDk1Y2ZhZWNlYjMyYTg0NzlmMzRmNzBiOWM5NTUxNjY4MA",
24
+ # user_id: 1188995,
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)
40
+ #
41
+ # # p client.companies.list
42
+ #
43
+ # # worker = client.instance_variable_get(:@worker)
44
+ # # p config = client.instance_variable_get(:@config).inspect
45
+ # #
46
+ # TimeDoctor::Token.refresh(config)
@@ -6,4 +6,3 @@ require 'timedoctor/exceptions'
6
6
  require 'timedoctor/config'
7
7
  require 'timedoctor/worker'
8
8
  require 'timedoctor/token'
9
- require 'timedoctor/payload'
@@ -14,12 +14,9 @@ module TimeDoctor
14
14
  class Client
15
15
  CORE = TimeDoctor::Core
16
16
 
17
- attr_reader :payload
18
-
19
- def initialize(payload = {})
20
- raise EmptyAccessToken unless payload[:access_token]
21
- @payload = Payload.new(payload)
22
- @worker = Worker.new(@payload)
17
+ def initialize(config = {})
18
+ @config = Config.new(config)
19
+ @worker = Worker.new(@config)
23
20
  end
24
21
 
25
22
  def absent_and_late
@@ -5,11 +5,27 @@ module TimeDoctor
5
5
  class << self
6
6
  def configure(options = {}, &blk)
7
7
  @config = OpenStruct.new(options)
8
- instance_exec(@config, &blk) if block_given?
8
+
9
+ config[:on_token_authorize] ||= lambda do |_data, _conf|
10
+ end
11
+
12
+ config[:on_token_authorize_error] ||= lambda do |data, _conf|
13
+ raise UnauthorizedError, data
14
+ end
15
+
16
+ config[:on_token_refresh] ||= lambda do |_data, _conf|
17
+ end
18
+
19
+ config[:on_token_refresh_error] ||= lambda do |data, _conf|
20
+ raise UnauthorizedError, data
21
+ end
22
+
23
+ instance_exec(config, &blk) if block_given?
24
+ config
9
25
  end
10
26
 
11
27
  def config
12
- @config ||= OpenStruct.new
28
+ @config || configure
13
29
  end
14
30
 
15
31
  def [](key)
@@ -24,5 +40,28 @@ module TimeDoctor
24
40
  config.to_h
25
41
  end
26
42
  end
43
+
44
+ def initialize(options)
45
+ @config = OpenStruct.new(options)
46
+ end
47
+
48
+ def config
49
+ @config ||= OpenStruct.new
50
+ end
51
+
52
+ def [](key)
53
+ config[key] || Config[key]
54
+ end
55
+
56
+ def []=(key, value)
57
+ config[key] = value
58
+ end
59
+
60
+ def inspect
61
+ {
62
+ local: @config.to_h,
63
+ global: Config.inspect
64
+ }
65
+ end
27
66
  end
28
67
  end
@@ -28,11 +28,15 @@ module TimeDoctor
28
28
  end
29
29
  end
30
30
 
31
- class UnauthorizedError < TimeDoctorError; end
31
+ class UnauthorizedError < StandardError
32
+ def initialize(data)
33
+ @message = data
34
+ end
32
35
 
33
- class UnknownError < TimeDoctorError; end
36
+ attr_reader :message
37
+ end
34
38
 
35
- class InvalidRefreshTokenError < TimeDoctorError; end
39
+ class UnknownError < TimeDoctorError; end
36
40
 
37
41
  class EmptyAccessToken < StandardError
38
42
  attr_reader :message
@@ -3,42 +3,49 @@ module TimeDoctor
3
3
  class << self
4
4
  ENTRY = 'https://webapi.timedoctor.com/oauth/v2/token'.freeze
5
5
 
6
- def authorize(payload)
7
- payload = Payload.new(payload)
8
-
6
+ def authorize(config)
9
7
  response = Faraday.get ENTRY,
10
- client_id: payload[:client_id],
11
- client_secret: payload[:client_secret],
12
- code: payload[:code],
13
- redirect_uri: payload[:redirect_uri],
8
+ client_id: config[:client_id],
9
+ client_secret: config[:client_secret],
10
+ code: config[:code],
11
+ redirect_uri: config[:redirect_uri],
14
12
  grant_type: :authorization_code,
15
13
  _format: :json
16
14
 
17
- data = body(response)
18
- payload.call(:on_token_authorize, data)
19
- data
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
20
27
  end
21
28
 
22
- def refresh(payload)
29
+ def refresh(config)
23
30
  response = Faraday.get ENTRY,
24
- refresh_token: payload[:refresh_token],
25
- client_id: payload[:client_id],
26
- client_secret: payload[:client_secret],
31
+ refresh_token: config[:refresh_token],
32
+ client_id: config[:client_id],
33
+ client_secret: config[:client_secret],
27
34
  grant_type: :refresh_token,
28
35
  _format: :json
29
36
 
30
- data = body(response)
31
- payload[:access_token] = data[:access_token]
32
- payload[:refresh_token] = data[:refresh_token]
33
- payload.call(:on_token_refresh, data)
34
- data
35
- end
36
-
37
- private
38
-
39
- def body(response)
40
- raise UnknownError, response if response.status != 200
41
- JSON.parse(response.body, symbolize_names: true)
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
42
49
  end
43
50
  end
44
51
  end
@@ -2,28 +2,27 @@ module TimeDoctor
2
2
  class Worker
3
3
  ENTRY = 'https://webapi.timedoctor.com/'.freeze
4
4
 
5
- attr_reader :payload
5
+ attr_reader :config, :conn
6
6
 
7
- def initialize(payload)
8
- raise EmptyAccessToken unless payload[:access_token]
7
+ def initialize(config)
8
+ raise EmptyAccessToken unless config[:access_token]
9
9
 
10
- @payload = payload
11
- @conn = Faraday.new(url: ENTRY)
10
+ @config = config
11
+ @conn = Faraday.new(url: ENTRY)
12
12
  end
13
13
 
14
- def exchange(method, url, params = {}, badly = false)
15
- params[:access_token] = @payload[:access_token]
14
+ def exchange(method, url, params = {})
15
+ params[:access_token] = config[:access_token]
16
16
  params[:_format] = :json
17
17
 
18
- response = @conn.public_send method, url, params
18
+ response = conn.public_send method, url, params
19
19
 
20
20
  case response.status
21
21
  when 200
22
22
  JSON.parse(response.body, symbolize_names: true)
23
23
  when 401
24
- raise UnauthorizedError, response if badly
25
- Token.refresh(@payload)
26
- exchange(method, url, params, true)
24
+ Token.refresh(config)
25
+ exchange(method, url, params)
27
26
  else
28
27
  raise UnknownError, response
29
28
  end
@@ -1,3 +1,3 @@
1
1
  module TimeDoctor
2
- VERSION = '0.1.0'.freeze
2
+ VERSION = '0.2.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: timedoctor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eugene Yak
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-05-15 00:00:00.000000000 Z
11
+ date: 2018-07-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -155,7 +155,6 @@ files:
155
155
  - lib/timedoctor/core/web_app.rb
156
156
  - lib/timedoctor/core/worklogs.rb
157
157
  - lib/timedoctor/exceptions.rb
158
- - lib/timedoctor/payload.rb
159
158
  - lib/timedoctor/token.rb
160
159
  - lib/timedoctor/worker.rb
161
160
  - lib/version.rb
@@ -1,25 +0,0 @@
1
- module TimeDoctor
2
- class Payload
3
- attr_reader :data
4
-
5
- def initialize(payload = {})
6
- @data = payload
7
- end
8
-
9
- def [](key)
10
- data[key] || Config[key]
11
- end
12
-
13
- def []=(key, value)
14
- data[key] = value
15
- end
16
-
17
- def call(name, *params)
18
- callback = self[name]
19
- return unless callback
20
-
21
- params = params << self
22
- callback.call(*params)
23
- end
24
- end
25
- end