Class: Google::APIClient::Resource

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

Overview

A resource that has been described by a discovery document.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api, method_base, resource_name, discovery_document) ⇒ Google::APIClient::Resource

Creates a description of a particular version of a resource.

Parameters:

  • base (Addressable::URI)

    The base URI for the service.

  • resource_name (String)

    The identifier for the resource.

  • resource_description (Hash)

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



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

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 = Google::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 = Google::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

#descriptionHash (readonly)

Returns the parsed section of the discovery document that applies to this resource.

Returns:

  • (Hash)

    The resource description.



70
71
72
# File 'lib/google/api_client/discovery/resource.rb', line 70

def description
  @description
end

#method_baseAddressable::URI

Returns the base URI for this resource.

Returns:

  • (Addressable::URI)

    The base URI that methods are joined to.



76
77
78
# File 'lib/google/api_client/discovery/resource.rb', line 76

def method_base
  @method_base
end

#nameString (readonly)

Returns the identifier for the resource.

Returns:

  • (String)

    The resource identifier.



63
64
65
# File 'lib/google/api_client/discovery/resource.rb', line 63

def name
  @name
end

Instance Method Details

#discovered_methodsArray

A list of methods available on this resource.

Returns:

  • (Array)

    A list of Method objects.



112
113
114
115
116
117
118
119
# File 'lib/google/api_client/discovery/resource.rb', line 112

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_resourcesArray

A list of sub-resources available on this resource.

Returns:



97
98
99
100
101
102
103
104
105
106
# File 'lib/google/api_client/discovery/resource.rb', line 97

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

#inspectString

Returns a String representation of the resource’s state.

Returns:

  • (String)

    The resource’s state, as a String.



143
144
145
146
147
# File 'lib/google/api_client/discovery/resource.rb', line 143

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

#to_hHash

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

Returns:

  • (Hash)

    All methods available on the resource.



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

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