Sha256: 4105c304b22fc695f2df84db3ec06ed8cc35dfbbed279713aeccffb96e8db1ca

Contents?: true

Size: 1.3 KB

Versions: 7

Compression:

Stored size: 1.3 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

7 entries across 7 versions & 2 rubygems

Version Path
auth0-3.3.0 lib/auth0/mixins/httparty_proxy.rb
auth0-3.2.0 lib/auth0/mixins/httparty_proxy.rb
auth0-3.1.2 lib/auth0/mixins/httparty_proxy.rb
auth0-3.1.1 lib/auth0/mixins/httparty_proxy.rb
auth0-3.1.0 lib/auth0/mixins/httparty_proxy.rb
auth0-3.0.0 lib/auth0/mixins/httparty_proxy.rb
auth0-ruby-0.10 lib/auth0/mixins/httparty_proxy.rb