Sha256: 7fbc61e506ef7a629c1b49b6699db892ad18ca1dc3bac7fbdc0962be8918ccea

Contents?: true

Size: 1.24 KB

Versions: 8

Compression:

Stored size: 1.24 KB

Contents

require 'json'

module Tumblr
  module Request

    # Perform a get request and return the raw response
    def get_response(path, params = {})
      connection.get do |req|
        req.url path
        req.params = params
      end
    end

    # get a redirect url
    def get_redirect_url(path, params = {})
      response = get_response path, params
      if response.status == 301
        response.headers['Location']
      else
        response.body['meta']
      end
    end

    # Performs a get request
    def get(path, params={})
      respond get_response(path, params)
    end

    # Performs post request
    def post(path, params={})
      if Array === params[:tags]
        params[:tags] = params[:tags].join(',')
      end
      response = connection.post do |req|
        req.url path
        req.body = params unless params.empty?
      end
      #Check for errors and encapsulate
      respond(response)
    end

    def respond(response)
      if [201, 200].include?(response.status)
        response.body['response']
      else
        # surface the meta alongside response
        res = response.body['meta'] || {}
        res.merge! response.body['response'] if response.body['response'].is_a?(Hash)
        res
      end
    end

  end
end

Version data entries

8 entries across 8 versions & 2 rubygems

Version Path
doomy_client-0.8.5 lib/tumblr/request.rb
tumblr_client-0.8.5 lib/tumblr/request.rb
tumblr_client-0.8.4 lib/tumblr/request.rb
tumblr_client-0.8.3 lib/tumblr/request.rb
tumblr_client-0.8.2 lib/tumblr/request.rb
tumblr_client-0.8.1 lib/tumblr/request.rb
tumblr_client-0.7.5 lib/tumblr/request.rb
tumblr_client-0.7.4 lib/tumblr/request.rb