Class: Google::APIClient::Result

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/google/api_client/result.rb

Overview

This class wraps a result returned by an API call.

Direct Known Subclasses

Service::BatchedCallResult

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request, response) ⇒ Result

Init the result

Parameters:



30
31
32
33
34
# File 'lib/google/api_client/result.rb', line 30

def initialize(request, response)
  @request = request
  @response = response
  @media_upload = reference if reference.kind_of?(ResumableUpload)
end

Instance Attribute Details

#bodyString (readonly)

Returns HTTP response body.

Returns:

  • (String)

    HTTP response body



51
# File 'lib/google/api_client/result.rb', line 51

def_delegators :@response, :status, :headers, :body

#dataObject, ... (readonly)

Return parsed version of the response body.

Returns:

  • (Object, Hash, String)

    Object if body parsable from API schema, Hash if JSON, raw body if unable to parse



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/google/api_client/result.rb', line 137

def data
  return @data ||= (begin
    if self.data?
      media_type = self.media_type
      data = self.body
      case media_type
      when 'application/json'
        data = MultiJson.load(data)
        # Strip data wrapper, if present
        data = data['data'] if data.has_key?('data')
      else
        raise ArgumentError,
          "Content-Type not supported for parsing: #{media_type}"
      end
      if @request.api_method && @request.api_method.response_schema
        # Automatically parse using the schema designated for the
        # response of this API method.
        data = @request.api_method.response_schema.new(data)
        data
      else
        # Otherwise, return the raw unparsed value.
        # This value must be indexable like a Hash.
        data
      end
    end
  end)
end

#data?TrueClass, FalseClass (readonly)

Check for parsable data in response

Returns:

  • (TrueClass, FalseClass)

    true if body can be parsed



127
128
129
# File 'lib/google/api_client/result.rb', line 127

def data?
  !(self.body.nil? || self.body.empty? || self.media_type != 'application/json')
end

#error?TrueClass, FalseClass (readonly)

Check if request failed

Returns:

  • (TrueClass, FalseClass)

    true if result of operation is an error



87
88
89
# File 'lib/google/api_client/result.rb', line 87

def error?
  return self.response.status >= 400
end

#error_messageString (readonly)

Extracts error messages from the response body

Returns:

  • (String)

    error message, if available



107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/google/api_client/result.rb', line 107

def error_message
  if self.data?
    if self.data.respond_to?(:error) &&
       self.data.error.respond_to?(:message)
      # You're going to get a terrible error message if the response isn't
      # parsed successfully as an error.
      return self.data.error.message
    elsif self.data['error'] && self.data['error']['message']
      return self.data['error']['message']
    end
  end
  return self.body
end

#headersHash (readonly)

Returns HTTP response headers.

Returns:

  • (Hash)

    HTTP response headers



51
# File 'lib/google/api_client/result.rb', line 51

def_delegators :@response, :status, :headers, :body

#media_typeString (readonly)

Get the content type of the response

Returns:

  • (String)

    Value of content-type header



70
71
72
73
74
75
76
77
78
79
# File 'lib/google/api_client/result.rb', line 70

def media_type
  _, content_type = self.headers.detect do |h, v|
    h.downcase == 'Content-Type'.downcase
  end
  if content_type
    return content_type[/^([^;]*);?.*$/, 1].strip.downcase
  else
    return nil
  end
end

#next_page_tokenString (readonly)

Get the token used for requesting the next page of data

Returns:

  • (String)

    next page token



171
172
173
174
175
176
177
178
179
# File 'lib/google/api_client/result.rb', line 171

def next_page_token
  if self.data.respond_to?(:next_page_token)
    return self.data.next_page_token
  elsif self.data.respond_to?(:[])
    return self.data["nextPageToken"]
  else
    raise TypeError, "Data object did not respond to #next_page_token."
  end
end

#page_token_paramString (readonly)

Name of the field that contains the pagination token

Returns:

  • (String)

    currently always ‘pageToken’



249
250
251
# File 'lib/google/api_client/result.rb', line 249

def page_token_param
  return "pageToken"
end

#pagination_typeSymbol (readonly)

Pagination scheme used by this request/response

Returns:

  • (Symbol)

    currently always :token



239
240
241
# File 'lib/google/api_client/result.rb', line 239

def pagination_type
  return :token
end

#prev_page_tokenString (readonly)

Get the token used for requesting the previous page of data

Returns:

  • (String)

    previous page token



205
206
207
208
209
210
211
212
213
# File 'lib/google/api_client/result.rb', line 205

def prev_page_token
  if self.data.respond_to?(:prev_page_token)
    return self.data.prev_page_token
  elsif self.data.respond_to?(:[])
    return self.data["prevPageToken"]
  else
    raise TypeError, "Data object did not respond to #next_page_token."
  end
end

#requestGoogle::APIClient::Request (readonly) Also known as: reference

Returns Original request object.

Returns:



37
38
39
# File 'lib/google/api_client/result.rb', line 37

def request
  @request
end

#responseFaraday::Response (readonly)

Returns HTTP response.

Returns:

  • (Faraday::Response)

    HTTP response



39
40
41
# File 'lib/google/api_client/result.rb', line 39

def response
  @response
end

#resumable_uploadGoogle::APIClient::ResumableUpload (readonly)

Returns For resuming media uploads.

Returns:



55
56
57
58
59
60
61
62
63
# File 'lib/google/api_client/result.rb', line 55

def resumable_upload        
  @media_upload ||= (
    options = self.reference.to_hash.merge(
      :uri => self.headers['location'],
      :media => self.reference.media
    )
    Google::APIClient::ResumableUpload.new(options)
  )
end

#statusFixnum (readonly)

Returns HTTP status code.

Returns:

  • (Fixnum)

    HTTP status code



51
# File 'lib/google/api_client/result.rb', line 51

def_delegators :@response, :status, :headers, :body

#success?TrueClass, FalseClass (readonly)

Check if request was successful

Returns:

  • (TrueClass, FalseClass)

    true if result of operation was successful



97
98
99
# File 'lib/google/api_client/result.rb', line 97

def success?
  return !self.error?
end

Instance Method Details

#next_pageGoogle::APIClient::Request

Build a request for fetching the next page of data

Returns:



186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/google/api_client/result.rb', line 186

def next_page
  return nil unless self.next_page_token
  merged_parameters = Hash[self.reference.parameters].merge({
    self.page_token_param => self.next_page_token
  })
  # Because Requests can be coerced to Hashes, we can merge them,
  # preserving all context except the API method parameters that we're
  # using for pagination.
  return Google::APIClient::Request.new(
    Hash[self.reference].merge(:parameters => merged_parameters)
  )
end

#prev_pageGoogle::APIClient::Request

Build a request for fetching the previous page of data

Returns:



220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/google/api_client/result.rb', line 220

def prev_page
  return nil unless self.prev_page_token
  merged_parameters = Hash[self.reference.parameters].merge({
    self.page_token_param => self.prev_page_token
  })
  # Because Requests can be coerced to Hashes, we can merge them,
  # preserving all context except the API method parameters that we're
  # using for pagination.
  return Google::APIClient::Request.new(
    Hash[self.reference].merge(:parameters => merged_parameters)
  )
end