Sha256: 89241a010f298046311fb7effc0b1b57154f7ae00e14cec413266865d18ebbce

Contents?: true

Size: 780 Bytes

Versions: 2

Compression:

Stored size: 780 Bytes

Contents

require 'httparty'
require 'http/exceptions'
require 'json'

module TangaServices
  class HTTP
    class Exception < RuntimeError
      def to_s
        cause.to_s
      end
    end

    %i( get put delete post ).each do |method|
      define_singleton_method(method) do |*args|
        new(method, *args).call
      end
    end

    def initialize(method, *args)
      @method = method
      @args   = *args
    end

    def call
      begin
        Http::Exceptions.wrap_and_check do
          response = HTTParty.send(@method, *@args)
          response.parsed_response # See that the response can be accessed
          response
        end
      rescue Http::Exceptions::HttpException, JSON::ParserError
        fail TangaServices::HTTP::Exception
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
tanga_services-0.0.12 lib/tanga_services/http.rb
tanga_services-0.0.11 lib/tanga_services/http.rb