Sha256: e6c01e48d8ad38e05c7ee40d8d3fbfddfbb590f89b3bc3c8e20ca7bb3fae8295

Contents?: true

Size: 1.29 KB

Versions: 4

Compression:

Stored size: 1.29 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)
          body = body.delete_if {|k,v| v.nil? }
          if method == :get
            result = self.class.send(method, safe_path, query: body)
          else
            result = self.class.send(method, safe_path, body: body.to_json)
          end
          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 403 then raise Auth0::AccessDenied, 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

4 entries across 4 versions & 1 rubygems

Version Path
auth0-3.6.1 lib/auth0/mixins/httparty_proxy.rb
auth0-3.6.0 lib/auth0/mixins/httparty_proxy.rb
auth0-3.5.0 lib/auth0/mixins/httparty_proxy.rb
auth0-3.4.0 lib/auth0/mixins/httparty_proxy.rb