Exception: Redd::Error

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

Defined Under Namespace

Classes: Archived, AuthenticationRequired, BadGateway, Conflict, InternalServerError, InvalidCaptcha, InvalidClassName, InvalidCredentials, InvalidMultiredditName, NotFound, PermissionDenied, RateLimited, RequestError, ServiceUnavailable, TimedOut, TooManyClassNames

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ Error

Returns a new instance of Error.



8
9
10
11
12
# File 'lib/redd/error.rb', line 8

def initialize(env)
  @code = env.status
  @headers = env.response_headers
  @body = env.body
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



6
7
8
# File 'lib/redd/error.rb', line 6

def body
  @body
end

#codeObject (readonly)

Returns the value of attribute code.



4
5
6
# File 'lib/redd/error.rb', line 4

def code
  @code
end

#headersObject (readonly)

Returns the value of attribute headers.



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

def headers
  @headers
end

Class Method Details

.from_response(response) ⇒ Object

Note:

I ripped off RedditKit.rb :|



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/redd/error.rb', line 16

def from_response(response) # rubocop:disable Style/CyclomaticComplexity Style/MethodLength
  status = response[:status]
  body = parse_error(response[:body]).to_s
  case status
  when 200
    case body
    when /WRONG_PASSWORD/i
      Redd::Error::InvalidCredentials
    when /BAD_CAPTCHA/i
      Redd::Error::InvalidCaptcha
    when /RATELIMIT/i
      Redd::Error::RateLimited
    when /BAD_CSS_NAME/i
      Redd::Error::InvalidClassName
    when /TOO_OLD/i
      Redd::Error::Archived
    when /TOO_MUCH_FLAIR_CSS/i
      Redd::Error::TooManyClassNames
    when /USER_REQUIRED/i
      Redd::Error::AuthenticationRequired
    end
  when 400
    Redd::Error::BadRequest
  when 403
    case body
    when /USER_REQUIRED/i
      Redd::Error::AuthenticationRequired
    else
      Redd::Error::PermissionDenied
    end
  when 404
    Redd::Error::NotFound
  when 409
    Redd::Error::Conflict
  when 500
    Redd::Error::InternalServerError
  when 502
    Redd::Error::BadGateway
  when 503
    Redd::Error::ServiceUnavailable
  when 504
    Redd::Error::TimedOut
  end
end

.parse_error(body) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'lib/redd/error.rb', line 61

def parse_error(body)
  return nil unless body.is_a?(Hash)

  if body.key?(:json) && body[:json].key?(:errors)
    body[:json][:errors].first
  elsif body.key?(:jquery)
    body[:jquery]
  end
end