Sha256: aaffbd73e71868da39179c4fbc84713aec58ffc9f482290c46e7f7b892343361

Contents?: true

Size: 1.31 KB

Versions: 1

Compression:

Stored size: 1.31 KB

Contents

require 'typetalk/connection'

module Typetalk

  class Api
    include Connection

    Dir[File.join(File.dirname(__FILE__), 'api/*.rb')].each{|f| require f}
    include Auth
    include User
    include Topic
    include Message
    include Mention
    include Notification


    def access_token
      if @access_token.nil?
        @access_token = get_access_token
        @access_token.expire_time = Time.now.to_i + @access_token.expires_in.to_i
      elsif Time.now.to_i >= @access_token.expire_time
        @access_token = update_access_token(@access_token.refresh_token)
        @access_token.expire_time = Time.now.to_i + @access_token.expires_in.to_i
      end
      @access_token.access_token
    end


    protected
    def parse_response(response)
      case response.status
      when 400
        raise InvalidRequest, response_values(response)
      when 401
        raise Unauthorized, response_values(response)
      when 404
        raise NotFound, response_values(response)
      when 413
        raise InvalidFileSize, response_values(response)
      end

      body = JSON.parse(response.body) rescue response.body
      body.is_a?(Hash) ? Hashie::Mash.new(body) : body
    end

    def response_values(response)
      {status: response.status, headers: response.headers, body: response.body}.to_json
    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
typetalk-0.0.4 lib/typetalk/api.rb