Exception: Flinks::Error

Inherits:
StandardError
  • Object
show all
Defined in:
lib/flinks/error.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ Flinks::Error

Parameters:

  • response (HTTP::Response)


66
67
68
69
70
71
# File 'lib/flinks/error.rb', line 66

def initialize(response)
  @response = response
  @code = response.code

  super(build_message)
end

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code.



5
6
7
# File 'lib/flinks/error.rb', line 5

def code
  @code
end

#responseObject (readonly)

Returns the value of attribute response.



5
6
7
# File 'lib/flinks/error.rb', line 5

def response
  @response
end

Class Method Details

.error_for_202(response) ⇒ Flinks::Error

Parameters:

  • response (HTTP::Response)

Returns:



36
37
38
39
40
41
42
# File 'lib/flinks/error.rb', line 36

def self.error_for_202(response)
  if response.parse['FlinksCode'] == 'OPERATION_PENDING'
    Flinks::OperationPending
  else
    Flinks::OperationDispatched
  end
end

.error_for_400(response) ⇒ Flinks::Error

Parameters:

  • response (HTTP::Response)

Returns:



46
47
48
49
50
51
52
# File 'lib/flinks/error.rb', line 46

def self.error_for_400(response)
  if response.parse['FlinksCode'] == 'SESSION_NONEXISTENT'
    Flinks::SessionNonexistent
  else
    Flinks::BadRequest
  end
end

.error_for_403(response) ⇒ Flinks::Error

Parameters:

  • response (HTTP::Response)

Returns:



56
57
58
59
60
61
62
# File 'lib/flinks/error.rb', line 56

def self.error_for_403(response)
  if response.parse['FlinksCode'] == 'TOO_MANY_REQUESTS'
    Flinks::TooManyRequests
  else
    Flinks::Forbidden
  end
end

.from_response(response) ⇒ Flinks::Error

Parameters:

  • response (HTTP::Response)

Returns:



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/flinks/error.rb', line 9

def self.from_response(response)
  klass = case response.code
          when 202      then error_for_202(response)
          when 400      then error_for_400(response)
          when 401      then Flinks::Unauthorized
          when 403      then error_for_403(response)
          when 404      then Flinks::NotFound
          when 405      then Flinks::MethodNotAllowed
          when 406      then Flinks::NotAcceptable
          when 409      then Flinks::Conflict
          when 415      then Flinks::UnsupportedMediaType
          when 422      then Flinks::UnprocessableEntity
          when 400..499 then Flinks::ClientError
          when 500      then Flinks::InternalServerError
          when 501      then Flinks::NotImplemented
          when 502      then Flinks::BadGateway
          when 503      then Flinks::ServiceUnavailable
          when 500..599 then Flinks::ServerError
          else
            self
          end

  klass.new(response)
end

Instance Method Details

#build_messageString

Returns:

  • (String)


74
75
76
77
78
79
# File 'lib/flinks/error.rb', line 74

def build_message
  message = response.parse['Message']
  message << " - FlinksCode: #{response.parse['FlinksCode']}"
rescue HTTP::Error
  response.reason
end