Class: PurpleAirApi::V1::RaiseHttpException

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/purple_air_api/V1/raise_http_exception.rb

Overview

Handles any HTTP exceptions for 400 and 500 error codes

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ RaiseHttpException

Faraday syntax for implementing middleware



27
28
29
30
# File 'lib/purple_air_api/V1/raise_http_exception.rb', line 27

def initialize(app)
  super app
  @parser = nil
end

Instance Method Details

#call(env) ⇒ Object

A switch statement which determines which error to raise depending on error code



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/purple_air_api/V1/raise_http_exception.rb', line 8

def call(env)
  @app.call(env).on_complete do |response|
    self.response = response
    case response[:status].to_i
    when 400
      raise ApiError.new(error_message, parsed_response[:error], response)
    when 403
      raise ApiKeyError.new(error_message, parsed_response[:error], response)
    when 404
      raise NotFoundError.new(error_message, parsed_response[:error], response)
    when 415
      raise MissingJsonPayloadError.new(error_message, 'MissingJsonPayloadError', response)
    when 500
      raise ServerError.new(error_message, 'ServerError', response)
    end
  end
end