Sha256: 5bcd82989db9bbb12f7d0f7fd580cc147a2bcaa7390b708106114e5e016eee6a

Contents?: true

Size: 1.61 KB

Versions: 2

Compression:

Stored size: 1.61 KB

Contents

require 'json'

require 'uploadcare/api/connections'

module Uploadcare
  module RawApi

    def initialize options={}
      @options = Uploadcare::default_settings.merge(options)  
    end


    # basic request method 
    def request method = :get, path = "/files/", params = {}
      response = send_request(method, path, params)
      parse(response)
    end
    alias_method :api_request, :request


    # request with GET verb
    def get path= "/files/", params={}
      request :get, path, params
    end


    # request with POST verb
    def post path= "/files/", params={}
      request :post, path, params 
    end

    # request with PUT verb
    def put path= "/files/", params={}
      request :put, path, params
    end


    # request with DELETE verb
    def delete path= "/files/", params={}
      request :delete, path, params
    end


    protected
      def send_request method, path, params={}
        connection = Uploadcare::Connections.api_connection(@options)
        response = connection.send method, path, params
      end


      def parse response
        begin
          object = JSON.parse(response.body)
        rescue JSON::ParserError
          object = false
        end

        # and returning the object (file actually) or raise new error
        if response.status < 300
          object
        else
          message = "HTTP code #{response.status}"
          if object # add active_support god damn it
            message += ": #{object["detail"]}"
          else
            message += ": unknown error occured."
          end

          raise ArgumentError.new(message)
        end
      end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
uploadcare-ruby-1.0.1.rc2 lib/uploadcare/api/raw_api.rb
uploadcare-ruby-1.0.1.rc1 lib/uploadcare/api/raw_api.rb