Sha256: 75a85d3c991fd417db87effa6d78807b36045958f1f6ba66aabef59b7cb15e3e

Contents?: true

Size: 1.32 KB

Versions: 2

Compression:

Stored size: 1.32 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)

      # TODO remove debug print
      #require 'awesome_print'
      #ap response

      case response.status
      when 400, 401
        raise InvalidRequest, 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}
    end

  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
typetalk-0.0.3 lib/typetalk/api.rb
typetalk-0.0.2 lib/typetalk/api.rb