timedoctor 0.1.0 → 0.2.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 +4 -4
- data/.gitignore +2 -0
- data/.rubocop.yml +4 -1
- data/Gemfile.lock +2 -2
- data/bin/console +32 -20
- data/lib/timedoctor.rb +0 -1
- data/lib/timedoctor/client.rb +3 -6
- data/lib/timedoctor/config.rb +41 -2
- data/lib/timedoctor/exceptions.rb +7 -3
- data/lib/timedoctor/token.rb +33 -26
- data/lib/timedoctor/worker.rb +10 -11
- data/lib/version.rb +1 -1
- metadata +2 -3
- data/lib/timedoctor/payload.rb +0 -25
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA256:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: 18ec15218465099f9b2dc4ecaea38ee6865185ec641f2993f98d5a4ad13c4bb9
         | 
| 4 | 
            +
              data.tar.gz: f689d43de75653e747524abd2f7937a134917882c44638c76ba24314d013ce06
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: 7ed21cca98983b5edfc5178e9dff3f352dbe0e3c1cfa56edadc1f3c78d354a9e097ee8b9c68c6f80669949794526a99481dc7ed3146b2c3749c380b57abc1d0c
         | 
| 7 | 
            +
              data.tar.gz: d4ac18f10dffc9a22687493a7b8dca406a0a3b2992dd3cb72696d8e2730ac28824458f429cff602a05c158d62d3527fd3bf2e2ddcdfd0915dcb8a7f24ebf78a4
         | 
    
        data/.gitignore
    CHANGED
    
    
    
        data/.rubocop.yml
    CHANGED
    
    
    
        data/Gemfile.lock
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            PATH
         | 
| 2 2 | 
             
              remote: .
         | 
| 3 3 | 
             
              specs:
         | 
| 4 | 
            -
                timedoctor (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. | 
| 21 | 
            +
                faraday (0.15.1)
         | 
| 22 22 | 
             
                  multipart-post (>= 1.2, < 3)
         | 
| 23 23 | 
             
                hashdiff (0.3.7)
         | 
| 24 24 | 
             
                json (2.1.0)
         | 
    
        data/bin/console
    CHANGED
    
    | @@ -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 | 
            -
             | 
| 20 | 
            -
             | 
| 21 | 
            -
             | 
| 22 | 
            -
             | 
| 23 | 
            -
             | 
| 24 | 
            -
             | 
| 25 | 
            -
             | 
| 26 | 
            -
             | 
| 27 | 
            -
             | 
| 28 | 
            -
             | 
| 29 | 
            -
             | 
| 30 | 
            -
             | 
| 31 | 
            -
             | 
| 32 | 
            -
            p  | 
| 33 | 
            -
             | 
| 34 | 
            -
            #  | 
| 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)
         | 
    
        data/lib/timedoctor.rb
    CHANGED
    
    
    
        data/lib/timedoctor/client.rb
    CHANGED
    
    | @@ -14,12 +14,9 @@ module TimeDoctor | |
| 14 14 | 
             
              class Client
         | 
| 15 15 | 
             
                CORE = TimeDoctor::Core
         | 
| 16 16 |  | 
| 17 | 
            -
                 | 
| 18 | 
            -
             | 
| 19 | 
            -
             | 
| 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
         | 
    
        data/lib/timedoctor/config.rb
    CHANGED
    
    | @@ -5,11 +5,27 @@ module TimeDoctor | |
| 5 5 | 
             
                class << self
         | 
| 6 6 | 
             
                  def configure(options = {}, &blk)
         | 
| 7 7 | 
             
                    @config = OpenStruct.new(options)
         | 
| 8 | 
            -
             | 
| 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  | 
| 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 <  | 
| 31 | 
            +
              class UnauthorizedError < StandardError
         | 
| 32 | 
            +
                def initialize(data)
         | 
| 33 | 
            +
                  @message = data
         | 
| 34 | 
            +
                end
         | 
| 32 35 |  | 
| 33 | 
            -
             | 
| 36 | 
            +
                attr_reader :message
         | 
| 37 | 
            +
              end
         | 
| 34 38 |  | 
| 35 | 
            -
              class  | 
| 39 | 
            +
              class UnknownError < TimeDoctorError; end
         | 
| 36 40 |  | 
| 37 41 | 
             
              class EmptyAccessToken < StandardError
         | 
| 38 42 | 
             
                attr_reader :message
         | 
    
        data/lib/timedoctor/token.rb
    CHANGED
    
    | @@ -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( | 
| 7 | 
            -
                    payload = Payload.new(payload)
         | 
| 8 | 
            -
             | 
| 6 | 
            +
                  def authorize(config)
         | 
| 9 7 | 
             
                    response = Faraday.get ENTRY,
         | 
| 10 | 
            -
                                           client_id:      | 
| 11 | 
            -
                                           client_secret:  | 
| 12 | 
            -
                                           code:           | 
| 13 | 
            -
                                           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 | 
            -
                     | 
| 18 | 
            -
                     | 
| 19 | 
            -
             | 
| 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( | 
| 29 | 
            +
                  def refresh(config)
         | 
| 23 30 | 
             
                    response = Faraday.get ENTRY,
         | 
| 24 | 
            -
                                           refresh_token:  | 
| 25 | 
            -
                                           client_id:      | 
| 26 | 
            -
                                           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 | 
            -
                     | 
| 31 | 
            -
                     | 
| 32 | 
            -
             | 
| 33 | 
            -
             | 
| 34 | 
            -
             | 
| 35 | 
            -
             | 
| 36 | 
            -
             | 
| 37 | 
            -
             | 
| 38 | 
            -
             | 
| 39 | 
            -
             | 
| 40 | 
            -
             | 
| 41 | 
            -
                     | 
| 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
         | 
    
        data/lib/timedoctor/worker.rb
    CHANGED
    
    | @@ -2,28 +2,27 @@ module TimeDoctor | |
| 2 2 | 
             
              class Worker
         | 
| 3 3 | 
             
                ENTRY = 'https://webapi.timedoctor.com/'.freeze
         | 
| 4 4 |  | 
| 5 | 
            -
                attr_reader : | 
| 5 | 
            +
                attr_reader :config, :conn
         | 
| 6 6 |  | 
| 7 | 
            -
                def initialize( | 
| 8 | 
            -
                  raise EmptyAccessToken unless  | 
| 7 | 
            +
                def initialize(config)
         | 
| 8 | 
            +
                  raise EmptyAccessToken unless config[:access_token]
         | 
| 9 9 |  | 
| 10 | 
            -
                  @ | 
| 11 | 
            -
                  @conn | 
| 10 | 
            +
                  @config = config
         | 
| 11 | 
            +
                  @conn   = Faraday.new(url: ENTRY)
         | 
| 12 12 | 
             
                end
         | 
| 13 13 |  | 
| 14 | 
            -
                def exchange(method, url, params = {} | 
| 15 | 
            -
                  params[:access_token] =  | 
| 14 | 
            +
                def exchange(method, url, params = {})
         | 
| 15 | 
            +
                  params[:access_token] = config[:access_token]
         | 
| 16 16 | 
             
                  params[:_format]      = :json
         | 
| 17 17 |  | 
| 18 | 
            -
                  response =  | 
| 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 | 
            -
                     | 
| 25 | 
            -
                     | 
| 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
         | 
    
        data/lib/version.rb
    CHANGED
    
    
    
        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. | 
| 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- | 
| 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
         | 
    
        data/lib/timedoctor/payload.rb
    DELETED
    
    | @@ -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
         |