Class: OMDBGateway::ResponseWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/omdbgateway/response_wrapper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(body, http_status = 200, http_error_message = nil) ⇒ ResponseWrapper

Returns a new instance of ResponseWrapper.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/omdbgateway/response_wrapper.rb', line 5

def initialize body, http_status = 200, http_error_message = nil
  @http_status = http_status
  @body = body
  @app_failed = false
  if http_success?
    if @body.instance_of?(Hash)
      if @body.key?('Response') && @body['Response'] == 'False'
        @app_failed = true
        if @body.key?('Error')
          @error_message = @body['Error']
        end
      end
    end
  else
    @error_message = http_error_message
  end
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



3
4
5
# File 'lib/omdbgateway/response_wrapper.rb', line 3

def body
  @body
end

#error_messageObject (readonly)

Returns the value of attribute error_message.



3
4
5
# File 'lib/omdbgateway/response_wrapper.rb', line 3

def error_message
  @error_message
end

#http_statusObject (readonly)

Returns the value of attribute http_status.



3
4
5
# File 'lib/omdbgateway/response_wrapper.rb', line 3

def http_status
  @http_status
end

Instance Method Details

#app_success?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/omdbgateway/response_wrapper.rb', line 27

def app_success?
  !@app_failed
end

#array_first(default = nil) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/omdbgateway/response_wrapper.rb', line 69

def array_first default = nil
  if !as_array.empty?
    as_array[0]
  else
    default
  end
end

#as_arrayObject



43
44
45
46
47
48
49
# File 'lib/omdbgateway/response_wrapper.rb', line 43

def as_array
  if success? && @body.instance_of?(Array)
    @body
  else
    []
  end
end

#as_hashObject



35
36
37
38
39
40
41
# File 'lib/omdbgateway/response_wrapper.rb', line 35

def as_hash
  if success? && @body.instance_of?(Hash)
    @body
  else
    {}
  end
end

#http_success?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/omdbgateway/response_wrapper.rb', line 23

def http_success?;
  @http_status >= 200 && @http_status < 300
end

#prune_array(index, default = nil) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/omdbgateway/response_wrapper.rb', line 60

def prune_array index, default = nil
  if as_array.length > index
    @body = as_array[index]
  else
    @body = default
  end
  self
end

#prune_hash(tag, default = nil) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/omdbgateway/response_wrapper.rb', line 51

def prune_hash tag, default = nil
  if as_hash.has_key? tag
    @body = as_hash[tag]
  else
    @body = default
  end
  self
end

#success?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/omdbgateway/response_wrapper.rb', line 31

def success?;
  http_success? && app_success?
end