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.

Parameters:

  • response (Faraday::Response)
  • envelope (Array) (defaults to: [])

    Property names for expected payload nesting.



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.

Parameters:

  • key (String)

Returns:

  • (Object)


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.

Returns:

  • (Object)


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.

Returns:

  • (Array)


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.

Returns:

  • (Array)


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.

Returns:

  • (true, false)


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

def warnings?
  !warnings.empty?
end