Class: Google::APIClient::API

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

Overview

A service that has been described by a discovery document.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document_base, discovery_document) ⇒ Google::APIClient::API

Creates a description of a particular version of a service.

Parameters:

  • document_base (String)

    Base URI for the discovery document.

  • discovery_document (Hash)

    The section of the discovery document that applies to this service version.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/google/api_client/discovery/api.rb', line 39

def initialize(document_base, discovery_document)
  @document_base = Addressable::URI.parse(document_base)
  @discovery_document = discovery_document
  metaclass = (class << self; self; end)
  self.discovered_resources.each do |resource|
    method_name = ActiveSupport::Inflector.underscore(resource.name).to_sym
    if !self.respond_to?(method_name)
      metaclass.send(:define_method, method_name) { resource }
    end
  end
  self.discovered_methods.each do |method|
    method_name = ActiveSupport::Inflector.underscore(method.name).to_sym
    if !self.respond_to?(method_name)
      metaclass.send(:define_method, method_name) { method }
    end
  end
end

Instance Attribute Details

#discovery_documentString (readonly)

Returns unparsed discovery document for the API.

Returns:

  • (String)

    unparsed discovery document for the API



58
59
60
# File 'lib/google/api_client/discovery/api.rb', line 58

def discovery_document
  @discovery_document
end

#document_baseAddressable::URI (readonly)

Returns the base URI for the discovery document.

Returns:

  • (Addressable::URI)

    The base URI.



152
153
154
# File 'lib/google/api_client/discovery/api.rb', line 152

def document_base
  @document_base
end

Class Method Details

._load(obj) ⇒ Object

Marshalling support - Restore an API instance from serialized form



304
305
306
# File 'lib/google/api_client/discovery/api.rb', line 304

def self._load(obj)
  new(*MultiJson.load(obj)) 
end

Instance Method Details

#[](key) ⇒ Object

Allows deep inspection of the discovery document.



260
261
262
# File 'lib/google/api_client/discovery/api.rb', line 260

def [](key)
  return @discovery_document[key]
end

#_dump(level) ⇒ Object

Marshalling support - serialize the API to a string (doc base + original discovery document).



298
299
300
# File 'lib/google/api_client/discovery/api.rb', line 298

def _dump(level)
  MultiJson.dump([@document_base.to_s, @discovery_document])
end

#batch_pathAddressable::URI

Returns the base URI for batch calls to this service.

Returns:

  • (Addressable::URI)

    The base URI that methods are joined to.



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

def batch_path
  if @discovery_document['batchPath']
    return @batch_path ||= (
      self.document_base.join(Addressable::URI.parse('/' +
          @discovery_document['batchPath']))
    ).normalize
  else
    return nil
  end
end

#data_wrapper?TrueClass, FalseClass

Returns true if this API uses a data wrapper.

Returns:

  • (TrueClass, FalseClass)

    Whether or not this API uses a data wrapper.



144
145
146
# File 'lib/google/api_client/discovery/api.rb', line 144

def data_wrapper?
  return self.features.include?('dataWrapper')
end

#descriptionHash

Returns a human-readable description of the API.

Returns:

  • (Hash)

    The API description.



99
100
101
# File 'lib/google/api_client/discovery/api.rb', line 99

def description
  return @discovery_document['description']
end

#discovered_methodsArray

A list of methods available at the root level of this version of the API.

Returns:

  • (Array)

    A list of Method objects.



249
250
251
252
253
254
255
256
# File 'lib/google/api_client/discovery/api.rb', line 249

def discovered_methods
  return @discovered_methods ||= (
    (@discovery_document['methods'] || []).inject([]) do |accu, (k, v)|
      accu << Google::APIClient::Method.new(self, self.method_base, k, v)
      accu
    end
  )
end

#discovered_resourcesArray

A list of resources available at the root level of this version of the API.

Returns:

  • (Array)

    A list of Resource objects.



233
234
235
236
237
238
239
240
241
242
# File 'lib/google/api_client/discovery/api.rb', line 233

def discovered_resources
  return @discovered_resources ||= (
    (@discovery_document['resources'] || []).inject([]) do |accu, (k, v)|
      accu << Google::APIClient::Resource.new(
        self, self.method_base, k, v
      )
      accu
    end
  )
end

#documentationHash

Returns a URI for the API documentation.

Returns:

  • (Hash)

    The API documentation.



107
108
109
# File 'lib/google/api_client/discovery/api.rb', line 107

def documentation
  return Addressable::URI.parse(@discovery_document['documentationLink'])
end

#featuresArray

Returns the list of API features.

Returns:

  • (Array)

    The features supported by this API.



125
126
127
# File 'lib/google/api_client/discovery/api.rb', line 125

def features
  return @discovery_document['features'] || []
end

#idString

Returns the id of the service.

Returns:

  • (String)

    The service id.



64
65
66
67
68
69
# File 'lib/google/api_client/discovery/api.rb', line 64

def id
  return (
    @discovery_document['id'] ||
    "#{self.name}:#{self.version}"
  )
end

#inspectString

Returns a String representation of the service’s state.

Returns:

  • (String)

    The service’s state, as a String.



289
290
291
292
293
# File 'lib/google/api_client/discovery/api.rb', line 289

def inspect
  sprintf(
    "#<%s:%#0x ID:%s>", self.class.to_s, self.object_id, self.id
  )
end

#method_baseAddressable::URI

Returns the base URI for this version of the service.

Returns:

  • (Addressable::URI)

    The base URI that methods are joined to.



158
159
160
161
162
163
164
165
166
# File 'lib/google/api_client/discovery/api.rb', line 158

def method_base
  if @discovery_document['basePath']
    return @method_base ||= (
      self.root_uri.join(Addressable::URI.parse(@discovery_document['basePath']))
    ).normalize
  else
    return nil
  end
end

#method_base=(new_method_base) ⇒ Object

Updates the hierarchy of resources and methods with the new base.

Parameters:

  • new_method_base (Addressable::URI, #to_str, String)

    The new base URI to use for the service.



173
174
175
176
177
178
179
180
181
# File 'lib/google/api_client/discovery/api.rb', line 173

def method_base=(new_method_base)
  @method_base = Addressable::URI.parse(new_method_base)
  self.discovered_resources.each do |resource|
    resource.method_base = @method_base
  end
  self.discovered_methods.each do |method|
    method.method_base = @method_base
  end
end

#nameString

Returns the identifier for the service.

Returns:

  • (String)

    The service identifier.



75
76
77
# File 'lib/google/api_client/discovery/api.rb', line 75

def name
  return @discovery_document['name']
end

#preferredTrueClass, FalseClass

Returns true if this is the preferred version of this API.

Returns:

  • (TrueClass, FalseClass)

    Whether or not this is the preferred version of this API.



116
117
118
# File 'lib/google/api_client/discovery/api.rb', line 116

def preferred
  return !!@discovery_document['preferred']
end

#root_uriAddressable::URI

Returns the root URI for this service.

Returns:

  • (Addressable::URI)

    The root URI.



133
134
135
136
137
# File 'lib/google/api_client/discovery/api.rb', line 133

def root_uri
  return @root_uri ||= (
    Addressable::URI.parse(self.discovery_document['rootUrl'])
  )
end

#schema_for_kind(kind) ⇒ Google::APIClient::Schema

Returns a schema for a kind value.

Returns:



215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/google/api_client/discovery/api.rb', line 215

def schema_for_kind(kind)
  api_name, schema_name = kind.split('#', 2)
  if api_name != self.name
    raise ArgumentError,
      "The kind does not match this API. " +
      "Expected '#{self.name}', got '#{api_name}'."
  end
  for k, v in self.schemas
    return v if k.downcase == schema_name.downcase
  end
  return nil
end

#schemasHash

A list of schemas available for this version of the API.

Returns:

  • (Hash)

    A list of Schema objects.



202
203
204
205
206
207
208
209
# File 'lib/google/api_client/discovery/api.rb', line 202

def schemas
  return @schemas ||= (
    (@discovery_document['schemas'] || []).inject({}) do |accu, (k, v)|
      accu[k] = Google::APIClient::Schema.parse(self, v)
      accu
    end
  )
end

#titleHash

Returns a human-readable title for the API.

Returns:

  • (Hash)

    The API title.



91
92
93
# File 'lib/google/api_client/discovery/api.rb', line 91

def title
  return @discovery_document['title']
end

#to_hHash

Converts the service to a flat mapping of RPC names and method objects.

Examples:

# Discover available methods
method_names = client.discovered_api('buzz').to_h.keys

Returns:

  • (Hash)

    All methods available on the service.



272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/google/api_client/discovery/api.rb', line 272

def to_h
  return @hash ||= (begin
    methods_hash = {}
    self.discovered_methods.each do |method|
      methods_hash[method.id] = method
    end
    self.discovered_resources.each do |resource|
      methods_hash.merge!(resource.to_h)
    end
    methods_hash
  end)
end

#versionString

Returns the version of the service.

Returns:

  • (String)

    The service version.



83
84
85
# File 'lib/google/api_client/discovery/api.rb', line 83

def version
  return @discovery_document['version']
end