class Google::APIClient::API

A service that has been described by a discovery document.

Attributes

discovery_document[R]

@return [String] unparsed discovery document for the API

document_base[R]

Returns the base URI for the discovery document.

@return [Addressable::URI] The base URI.

Public Class Methods

_load(obj) click to toggle source

Marshalling support - Restore an API instance from serialized form

# File lib/google/api_client/discovery/api.rb, line 304
def self._load(obj)
  new(*MultiJson.load(obj)) 
end
new(document_base, discovery_document) click to toggle source

Creates a description of a particular version of a service.

@param [String] document_base

Base URI for the discovery document.

@param [Hash] discovery_document

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

@return [Google::APIClient::API] The constructed service object.

# 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

Public Instance Methods

[](key) click to toggle source

Allows deep inspection of the discovery document.

# File lib/google/api_client/discovery/api.rb, line 260
def [](key)
  return @discovery_document[key]
end
_dump(level) click to toggle source

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

# File lib/google/api_client/discovery/api.rb, line 298
def _dump(level)
  MultiJson.dump([@document_base.to_s, @discovery_document])
end
batch_path() click to toggle source

Returns the base URI for batch calls to this service.

@return [Addressable::URI] The base URI that methods are joined to.

# 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?() click to toggle source

Returns true if this API uses a data wrapper.

@return [TrueClass, FalseClass]

Whether or not this API uses a data wrapper.
# File lib/google/api_client/discovery/api.rb, line 144
def data_wrapper?
  return self.features.include?('dataWrapper')
end
description() click to toggle source

Returns a human-readable description of the API.

@return [Hash] The API description.

# File lib/google/api_client/discovery/api.rb, line 99
def description
  return @discovery_document['description']
end
discovered_methods() click to toggle source

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

@return [Array] A list of {Google::APIClient::Method} objects.

# 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_resources() click to toggle source

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

@return [Array] A list of {Google::APIClient::Resource} objects.

# 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
documentation() click to toggle source

Returns a URI for the API documentation.

@return [Hash] The API documentation.

# File lib/google/api_client/discovery/api.rb, line 107
def documentation
  return Addressable::URI.parse(@discovery_document['documentationLink'])
end
features() click to toggle source

Returns the list of API features.

@return [Array]

The features supported by this API.
# File lib/google/api_client/discovery/api.rb, line 125
def features
  return @discovery_document['features'] || []
end
id() click to toggle source

Returns the id of the service.

@return [String] The service id.

# File lib/google/api_client/discovery/api.rb, line 64
def id
  return (
    @discovery_document['id'] ||
    "#{self.name}:#{self.version}"
  )
end
inspect() click to toggle source

Returns a String representation of the service's state.

@return [String] The service's state, as a String.

# 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_base() click to toggle source

Returns the base URI for this version of the service.

@return [Addressable::URI] The base URI that methods are joined to.

# 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) click to toggle source

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

@param [Addressable::URI, to_str, String] new_method_base

The new base URI to use for the service.
# 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
name() click to toggle source

Returns the identifier for the service.

@return [String] The service identifier.

# File lib/google/api_client/discovery/api.rb, line 75
def name
  return @discovery_document['name']
end
preferred() click to toggle source

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

@return [TrueClass, FalseClass]

Whether or not this is the preferred version of this API.
# File lib/google/api_client/discovery/api.rb, line 116
def preferred
  return !!@discovery_document['preferred']
end
root_uri() click to toggle source

Returns the root URI for this service.

@return [Addressable::URI] The root URI.

# 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) click to toggle source

Returns a schema for a kind value.

@return [Google::APIClient::Schema] The associated Schema object.

# 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
schemas() click to toggle source

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

@return [Hash] A list of {Google::APIClient::Schema} objects.

# 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
title() click to toggle source

Returns a human-readable title for the API.

@return [Hash] The API title.

# File lib/google/api_client/discovery/api.rb, line 91
def title
  return @discovery_document['title']
end
to_h() click to toggle source

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

@return [Hash] All methods available on the service.

@example

# Discover available methods
method_names = client.discovered_api('buzz').to_h.keys
# 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
version() click to toggle source

Returns the version of the service.

@return [String] The service version.

# File lib/google/api_client/discovery/api.rb, line 83
def version
  return @discovery_document['version']
end