Sha256: 187d715046f0f63ce8422d3b27568fc5acb2944086e7c55952b260ae95da9f77

Contents?: true

Size: 1.79 KB

Versions: 4

Compression:

Stored size: 1.79 KB

Contents

require 'wework/request'

module Wework
  module Api
    class Base

      include Methods::Media
      include Methods::User
      include Methods::Department

      attr_accessor :corp_id, :secret, :options

      def initialize options={}
        @corp_id      = options.delete(:corp_id)
        @secret       = options.delete(:secret)
        @token_store  = options.delete(:token_store)
        @options      = options
      end

      def request
        @request ||= Wework::Request.new(API_ENDPOINT, false)
      end

      def valid?
        return false if corp_id.nil?
        token_store.token.present?
      rescue AccessTokenExpiredError
        false
      rescue => e
        Rails.logger.error "[WEWORK] (valid?) fetch access token error(#{corp_id}): #{e.inspect}" if defined?(Rails)
        false
      end

      def get(path, headers = {})
        with_token(headers[:params]) do |params|
          request.get path, headers.merge(params: params)
        end
      end

      def post(path, payload, headers = {})
        with_token(headers[:params]) do |params|
          request.post path, payload, headers.merge(params: params)
        end
      end

      def post_file(path, file, headers = {})
        with_token(headers[:params]) do |params|
          request.post_file path, file, headers.merge(params: params)
        end
      end

      def access_token
        token_store.token
      end

      private

      def with_token(params = {}, tries = 2)
        params ||= {}
        yield(params.merge(token_params))
      rescue AccessTokenExpiredError
        token_store.update_token
        retry unless (tries -= 1).zero?
      end

      def token_store
        @token_store ||= Token::AppToken.new self
      end

      def token_params
        {access_token: access_token}
      end

    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
wework-1.1.3 lib/wework/api/base.rb
wework-1.1.2 lib/wework/api/base.rb
wework-1.1.1 lib/wework/api/base.rb
wework-1.1.0 lib/wework/api/base.rb