Sha256: e33a53f0a0c9daddfd7ba8bc088e1e1d640ea4e29b7032443557e74152bd0546

Contents?: true

Size: 1.48 KB

Versions: 1

Compression:

Stored size: 1.48 KB

Contents

require 'pathname'

module Buffer
  class Client
    module Core
      API_VERSION = "1"

      private

      def get(path, options = {})
        options.merge!(auth_query)
        response = connection.get do |req|
          req.url path.remove_leading_slash
          req.params = options
        end

        interpret_response(response)
      end

      def post(path, options = {})
        params = merge_auth_token_and_query(options)
        params.merge!(options)
        response = connection.post do |req|
          req.url path.remove_leading_slash
          req.headers['Content-Type'] = "application/x-www-form-urlencoded"
          req.body = options[:body]
          req.params = params
        end

        Hashie::Mash.new(JSON.parse response.body)
      end

      def merge_auth_token_and_query(options)
        if options[:query]
          auth_query.merge options[:query]
        else
          auth_query
        end
      end

      def interpret_response(response)
        if response.status == 200
          JSON.parse response.body
        else
          handle_response_code(response)
        end
      end

      def handle_response_code(response)
        error = Hashie::Mash.new(JSON.parse(response.body))
        raise Buffer::Error::APIError,
          "Buffer API Error Code: #{error.code} " +
          "HTTP Code: #{response.status}. " +
          "Description: #{error.error}"
      end
    end
  end
end

class String
  def remove_leading_slash
    gsub(/^\//, '')
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
buffer-0.1.3 lib/buffer/core.rb