Sha256: 6d7d61f07fb3908ac9290c19c793d5e91e20aa15d96623910df430183297ea29

Contents?: true

Size: 1.92 KB

Versions: 2

Compression:

Stored size: 1.92 KB

Contents

module MotionResource
  class Base
    HTTP_METHODS = [:get, :post, :put, :delete]

    HTTP_METHODS.each do |method|
      define_method method do |*args, &block|
        self.class.send(method, *args, &block)
      end
    end

    class << self
      def get(url, params = {}, &block)
        http_call(:get, url, params, &block)
      end

      def post(url, params = {}, &block)
        http_call(:post, url, params, &block)
      end

      def put(url, params = {}, &block)
        http_call(:put, url, params, &block)
      end

      def delete(url, params = {}, &block)
        http_call(:delete, url, params, &block)
      end

      def on_auth_failure(&block)
        @on_auth_failure = block
      end

    private
      def complete_url(fragment)
        if fragment[0..3] == "http"
          return fragment
        end
        (self.root_url || MotionResource::Base.root_url) + fragment
      end

      def http_call(method, url, call_options = {}, &block)
        url = complete_url(url)

        options = call_options
        options.merge!(MotionResource::Base.default_url_options || {})
        if query = options.delete(:query)
          url.build_query_string!(query)
        end
        if self.default_url_options
          options.merge!(self.default_url_options)
        end
        logger.log "#{method.upcase} #{url}"

        url.insert_extension!(self.extension)

        BubbleWrap::HTTP.send(method, url, options) do |response|
          if response.ok?
            body = response.body.to_str.strip rescue nil
            logger.log "response: #{body}"
            if body.blank?
              block.call(response, {})
            else
              block.call response, BubbleWrap::JSON.parse(body)
            end
          elsif response.status_code.to_s =~ /401/ && @on_auth_failure
            @on_auth_failure.call
          else
            block.call response, nil
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
motion-resource-0.1.1 lib/motion-resource/requests.rb
motion-resource-0.1.0 lib/motion-resource/requests.rb