Sha256: f86455f98b0a77219b943835ed8aa990d7a5f3f5fb65cd4d6fe3c38a5d98c3a7

Contents?: true

Size: 1.59 KB

Versions: 2

Compression:

Stored size: 1.59 KB

Contents

module Slack
  module API
    class Base
      attr_reader :access_token

      def initialize(access_token=nil)
        @access_token = access_token
      end

      protected

      def base_path
        "https://slack.com/api/"
      end

      def with_nil_response
        yield
        nil
      end

      def with_paging(page)
        Cursor.new(page).paginate do |pager|
          yield(pager)
        end
      end

      def request(method, path, arguments={})
        full_path = "#{base_path}#{path}"
        full_path = "#{full_path}?token=#{access_token}" unless access_token.nil?
        arguments.each_pair do |key, value|
          unless value.nil?
            seperator = full_path.include?("?") ? "&" : "?"
            full_path = "#{full_path}#{seperator}#{key}=#{ERB::Util.url_encode(value)}"
          end
        end

        options = {
          headers: {
            "Accept"       => "application/json",
            "Content-Type" => "application/json; charset=utf-8"
          }
        }
        response = HTTParty.send method, full_path, options
        handle_exceptions response
        response
      end

      def handle_exceptions(response)
        parsed = JSON.parse(response.body)
        if !parsed['ok']
          klass = "#{parsed['error'].gsub(/(^|_)(.)/) { $2.upcase }}Error"
          if Slack.const_defined? klass
            raise Slack.const_get(klass)
          elsif response.code == 429
            raise Slack::TooManyRequestsError.new(response)
          else
            raise Slack::APIError.new parsed['error']
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
laziness-0.2.6 lib/laziness/api/base.rb
laziness-0.2.5 lib/laziness/api/base.rb