Sha256: 23c756a443269905dc104d29f9e69f42a83fc008214afd93401fad3b3186290b

Contents?: true

Size: 1.07 KB

Versions: 2

Compression:

Stored size: 1.07 KB

Contents

module Auth0
  module Mixins
    # here's the proxy for HTTParty, we're building all request on that gem for now, if you want to feel free to use your own http client
    module HTTPartyProxy
      # proxying requests from instance methods to HTTParty class methods
      %i(get post put patch delete).each do |method|
        define_method(method) do |path, body={}|
          safe_path = URI.escape(path)
          result = self.class.send(method, safe_path, body: body.to_json)
          response_body =
            begin
              JSON.parse(result.body.to_s)
            rescue JSON::ParserError
              result.body
            end
          case result.code
          when 200...226 then response_body
          when 400 then raise Auth0::BadRequest, response_body
          when 401 then raise Auth0::Unauthorized, response_body
          when 404 then raise Auth0::NotFound, response_body
          when 500 then raise Auth0::ServerError, response_body
          else
            raise Auth0::Unsupported, response_body
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
auth0-ruby-0.9.1 lib/auth0/mixins/httparty_proxy.rb
auth0-ruby-0.9 lib/auth0/mixins/httparty_proxy.rb