Exception: Cerner::OAuth1a::OAuthError

Inherits:
StandardError
  • Object
show all
Defined in:
lib/cerner/oauth1a/oauth_error.rb

Overview

Public: An OAuth-specific error.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message, http_response_code = nil, oauth_problem = nil, oauth_parameters = nil, realm = nil) ⇒ OAuthError

Public: Construct an instance with a message, optional HTTP response code and optional OAuth Problem string.

message - A descriptive message, passed to the super class. http_response_code - The HTTP response code associated with the error. Optional. oauth_problem - The OAuth Problem string associated with the error. Optional. oauth_parameters - A String/Symbol or Array of Strings/Symbols containing the names of parameters that

are absent or rejected. This is should only be used when oauth_problem
is 'parameter_absent' or 'parameter_rejected' Optional.

realm - The protection realm associated with the error. Optional.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/cerner/oauth1a/oauth_error.rb', line 34

def initialize(
  message,
  http_response_code = nil,
  oauth_problem = nil,
  oauth_parameters = nil,
  realm = nil
)
  @http_response_code = http_response_code
  @oauth_problem = oauth_problem
  @oauth_parameters = oauth_parameters ? Array(oauth_parameters) : nil
  @realm = realm

  parts = []
  parts << message if message
  parts << "HTTP #{@http_response_code}" if @http_response_code
  parts << "OAuth Problem #{@oauth_problem}" if @oauth_problem
  parts << "OAuth Parameters [#{@oauth_parameters.join(', ')}]" if @oauth_parameters
  parts << "OAuth Realm #{@realm}" if @realm
  super(parts.empty? ? nil : parts.join(' '))
end

Instance Attribute Details

#http_response_codeObject (readonly)

Returns the HTTP Response Code, if any, associated with this error. May be nil.



11
12
13
# File 'lib/cerner/oauth1a/oauth_error.rb', line 11

def http_response_code
  @http_response_code
end

#oauth_parametersObject (readonly)

Returns an Array of OAuth parameter names, if any, related to #oauth_problem. May be nil.



19
20
21
# File 'lib/cerner/oauth1a/oauth_error.rb', line 19

def oauth_parameters
  @oauth_parameters
end

#oauth_problemObject (readonly)

Returns the OAuth Problem string, if any, associated with this error. May be nil. See oauth.pbwiki.com/ProblemReporting for more information.



15
16
17
# File 'lib/cerner/oauth1a/oauth_error.rb', line 15

def oauth_problem
  @oauth_problem
end

#realmObject (readonly)

Returns a String with the Protection Realm associated with this error. May be nil.



22
23
24
# File 'lib/cerner/oauth1a/oauth_error.rb', line 22

def realm
  @realm
end

Instance Method Details

#to_http_status(default = :unauthorized) ⇒ Object

Public: Provides an HTTP Status Symbol based on the #oauth_problem using Protocol.convert_problem_to_http_status.

default - The Symbol to return if #oauth_problem contains an unknown value.

Defaults to :unauthorized.

Returns :unauthorized, :bad_request or the value passed in default parameter.



83
84
85
# File 'lib/cerner/oauth1a/oauth_error.rb', line 83

def to_http_status(default = :unauthorized)
  Protocol.convert_problem_to_http_status(@oauth_problem, default)
end

#to_http_www_authenticate_headerObject

Public: Generates an HTTP WWW-Authenticate header value based from the data in this OAuthError.

Returns the generated value or nil if there is no #oauth_problem or #realm.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/cerner/oauth1a/oauth_error.rb', line 59

def to_http_www_authenticate_header
  params = {}
  params[:realm] = @realm if @realm
  params[:oauth_problem] = @oauth_problem if @oauth_problem

  if @oauth_problem && @oauth_parameters
    case @oauth_problem
    when 'parameter_absent'
      params[:oauth_parameters_absent] = format_parameters(@oauth_parameters)
    when 'parameter_rejected'
      params[:oauth_parameters_rejected] = format_parameters(@oauth_parameters)
    end
  end

  Protocol.generate_www_authenticate_header(params)
end