Sha256: 8b3bda622339114a37e0444344014709318227b3a2e44e02be509209439a0d5b

Contents?: true

Size: 1.09 KB

Versions: 17

Compression:

Stored size: 1.09 KB

Contents

module PushbulletRuby
  class BadRequest    < StandardError; end
  class Unauthorized  < StandardError; end
  class RequestFailed < StandardError; end
  class Forbidden     < StandardError; end
  class NotFound      < StandardError; end
  class ServerError   < StandardError; end

  class HttpException < Faraday::Response::Middleware
    def call(env)
      @app.call(env).on_complete do |response|
        exception =
            case response.status
              when 400      then PushbulletRuby::BadRequest
              when 401      then PushbulletRuby::Unauthorized
              when 402      then PushbulletRuby::RequestFailed
              when 403      then PushbulletRuby::Forbidden
              when 404      then PushbulletRuby::NotFound
              when 500..505 then PushbulletRuby::ServerError
            end

        raise exception, error_message(response.body) if exception
      end
    end

    private

    def error_message(response_body)
      hash = JSON.parse(response_body)['error']

      [hash['message'], hash['cat']].join(' ')
    end
  end
end

Version data entries

17 entries across 16 versions & 2 rubygems

Version Path
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/pushbullet_ruby-1.1.4/lib/pushbullet_ruby/http_exeption.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/tdiary-5.1.5/vendor/bundle/ruby/3.0.0/gems/pushbullet_ruby-1.1.4/lib/pushbullet_ruby/http_exeption.rb
tdiary-5.1.5 vendor/bundle/ruby/3.0.0/gems/pushbullet_ruby-1.1.4/lib/pushbullet_ruby/http_exeption.rb
tdiary-5.1.2 vendor/bundle/ruby/2.6.0/gems/pushbullet_ruby-1.1.4/lib/pushbullet_ruby/http_exeption.rb
tdiary-5.1.1 vendor/bundle/ruby/2.6.0/gems/pushbullet_ruby-1.1.4/lib/pushbullet_ruby/http_exeption.rb
pushbullet_ruby-1.1.4 lib/pushbullet_ruby/http_exeption.rb
tdiary-5.0.8 vendor/bundle/gems/pushbullet_ruby-1.1.3/lib/pushbullet_ruby/http_exeption.rb
pushbullet_ruby-1.1.3 lib/pushbullet_ruby/http_exeption.rb
pushbullet_ruby-1.1.2 lib/pushbullet_ruby/http_exeption.rb
pushbullet_ruby-1.1.1 lib/pushbullet_ruby/http_exeption.rb
pushbullet_ruby-1.1.0 lib/pushbullet_ruby/http_exeption.rb
pushbullet_ruby-1.0.4.1 lib/pushbullet_ruby/http_exeption.rb
pushbullet_ruby-1.0.4 lib/pushbullet_ruby/http_exeption.rb
pushbullet_ruby-1.0.3 lib/pushbullet_ruby/http_exeption.rb
pushbullet_ruby-1.0.2.1 lib/pushbullet_ruby/http_exeption.rb
pushbullet_ruby-1.0.2 lib/pushbullet_ruby/http_exeption.rb
pushbullet_ruby-1.0.1 lib/pushbullet_ruby/http_exeption.rb