Class: MediawikiApi::Response

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/mediawiki_api/response.rb

Overview

Provides access to a parsed MediaWiki API responses.

Some types of responses, depending on the action, contain a level or two of addition structure (an envelope) above the actual payload. The #data method provides a way of easily getting at it.

Examples:

# http.body => '{"query": {"userinfo": {"some": "data"}}}'
response = Response.new(http, ["query", "userinfo"])
response.data # => { "some" => "data" }

Instance Method Summary collapse

Constructor Details

#initialize(response, envelope = []) ⇒ Response

Constructs a new response.


26
27
28
29
# File 'lib/mediawiki_api/response.rb', line 26

def initialize(response, envelope = [])
  @response = response
  @envelope = envelope
end

Instance Method Details

#[](key) ⇒ Object

Accessor for root response object values.


37
38
39
# File 'lib/mediawiki_api/response.rb', line 37

def [](key)
  response_object[key]
end

#dataObject

The main payload from the parsed response, removed from its envelope.


45
46
47
48
49
50
51
52
# File 'lib/mediawiki_api/response.rb', line 45

def data
  case response_object
  when Hash
    open_envelope(response_object)
  else
    response_object
  end
end

#errorsArray

Set of error messages from the response.


58
59
60
# File 'lib/mediawiki_api/response.rb', line 58

def errors
  flatten_resp('errors')
end

#warningsArray

Set of warning messages from the response.


66
67
68
# File 'lib/mediawiki_api/response.rb', line 66

def warnings
  flatten_resp('warnings')
end

#warnings?true, false

Whether the response contains warnings.


74
75
76
# File 'lib/mediawiki_api/response.rb', line 74

def warnings?
  !warnings.empty?
end