class Google::APIClient::Resource

A resource that has been described by a discovery document.

Attributes

discovery_document[R]

@return [String] unparsed discovery document for the resource

method_base[R]

Returns the base URI for this resource.

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

name[R]

Returns the identifier for the resource.

@return [String] The resource identifier.

Public Class Methods

new(api, method_base, resource_name, discovery_document) click to toggle source

Creates a description of a particular version of a resource.

@param [Google::APIClient::API] api

The API this resource belongs to.

@param [Addressable::URI] method_base

The base URI for the service.

@param [String] resource_name

The identifier for the resource.

@param [Hash] discovery_document

The section of the discovery document that applies to this resource.

@return [Google::APIClient::Resource] The constructed resource object.

# File lib/google/api_client/discovery/resource.rb, line 41
def initialize(api, method_base, resource_name, discovery_document)
  @api = api
  @method_base = method_base
  @name = resource_name
  @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

description() click to toggle source

Returns a human-readable description of the resource.

@return [Hash] The API description.

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

A list of methods available on this resource.

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

# File lib/google/api_client/discovery/resource.rb, line 118
def discovered_methods
  return @discovered_methods ||= (
    (@discovery_document['methods'] || []).inject([]) do |accu, (k, v)|
      accu << Google::APIClient::Method.new(@api, self.method_base, k, v)
      accu
    end
  )
end
discovered_resources() click to toggle source

A list of sub-resources available on this resource.

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

# File lib/google/api_client/discovery/resource.rb, line 103
def discovered_resources
  return @discovered_resources ||= (
    (@discovery_document['resources'] || []).inject([]) do |accu, (k, v)|
      accu << Google::APIClient::Resource.new(
        @api, self.method_base, k, v
      )
      accu
    end
  )
end
inspect() click to toggle source

Returns a String representation of the resource's state.

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

# File lib/google/api_client/discovery/resource.rb, line 149
def inspect
  sprintf(
    "#<%s:%#0x NAME:%s>", self.class.to_s, self.object_id, self.name
  )
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 resource.
# File lib/google/api_client/discovery/resource.rb, line 89
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
to_h() click to toggle source

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

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

# File lib/google/api_client/discovery/resource.rb, line 132
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