Sha256: 89f45db16a2f0e89914817ae9b1918f550411a2bd06ee79835edd7055592d693

Contents?: true

Size: 736 Bytes

Versions: 1

Compression:

Stored size: 736 Bytes

Contents

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

module TangaServices
  class HTTP
    class Exception < RuntimeError
    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

1 entries across 1 versions & 1 rubygems

Version Path
tanga_services-0.0.10 lib/tanga_services/http.rb