Class: Google::APIClient::Result

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

Overview

This class wraps a result returned by an API call.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(reference, request, response) ⇒ Result

Returns a new instance of Result.



21
22
23
24
25
# File 'lib/google/api_client/result.rb', line 21

def initialize(reference, request, response)
  @reference = reference
  @request = request
  @response = response
end

Instance Attribute Details

#referenceObject (readonly)

Returns the value of attribute reference.



27
28
29
# File 'lib/google/api_client/result.rb', line 27

def reference
  @reference
end

#requestObject (readonly)

Returns the value of attribute request.



29
30
31
# File 'lib/google/api_client/result.rb', line 29

def request
  @request
end

#responseObject (readonly)

Returns the value of attribute response.



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

def response
  @response
end

Instance Method Details

#bodyObject



41
42
43
# File 'lib/google/api_client/result.rb', line 41

def body
  return @response.body
end

#dataObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/google/api_client/result.rb', line 82

def data
  return @data ||= (begin
    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 @reference.api_method && @reference.api_method.response_schema
      # Automatically parse using the schema designated for the
      # response of this API method.
      data = @reference.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

#data?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/google/api_client/result.rb', line 78

def data?
  self.media_type == 'application/json'
end

#error?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/google/api_client/result.rb', line 56

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

#error_messageObject



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/google/api_client/result.rb', line 64

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

#headersObject



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

def headers
  return @response.headers
end

#media_typeObject



49
50
51
52
53
54
# File 'lib/google/api_client/result.rb', line 49

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

#next_pageObject



126
127
128
129
130
131
132
133
134
135
136
# File 'lib/google/api_client/result.rb', line 126

def next_page
  merged_parameters = Hash[self.reference.parameters].merge({
    self.page_token_param => self.next_page_token
  })
  # Because References 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::Reference.new(
    Hash[self.reference].merge(:parameters => merged_parameters)
  )
end

#next_page_tokenObject



116
117
118
119
120
121
122
123
124
# File 'lib/google/api_client/result.rb', line 116

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_paramObject



112
113
114
# File 'lib/google/api_client/result.rb', line 112

def page_token_param
  return "pageToken"
end

#pagination_typeObject



108
109
110
# File 'lib/google/api_client/result.rb', line 108

def pagination_type
  return :token
end

#prev_pageObject



148
149
150
151
152
153
154
155
156
157
158
# File 'lib/google/api_client/result.rb', line 148

def prev_page
  merged_parameters = Hash[self.reference.parameters].merge({
    self.page_token_param => self.prev_page_token
  })
  # Because References 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::Reference.new(
    Hash[self.reference].merge(:parameters => merged_parameters)
  )
end

#prev_page_tokenObject



138
139
140
141
142
143
144
145
146
# File 'lib/google/api_client/result.rb', line 138

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

#resumable_uploadObject



45
46
47
# File 'lib/google/api_client/result.rb', line 45

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

#statusObject



33
34
35
# File 'lib/google/api_client/result.rb', line 33

def status
  return @response.status
end

#success?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/google/api_client/result.rb', line 60

def success?
  return !self.error?
end