Class: Google::APIClient::Resource
- Inherits:
-
Object
- Object
- Google::APIClient::Resource
- Defined in:
- lib/google/api_client/discovery/resource.rb
Overview
A resource that has been described by a discovery document.
Instance Attribute Summary collapse
-
#description ⇒ Hash
readonly
Returns the parsed section of the discovery document that applies to this resource.
-
#method_base ⇒ Addressable::URI
Returns the base URI for this resource.
-
#name ⇒ String
readonly
Returns the identifier for the resource.
Instance Method Summary collapse
-
#discovered_methods ⇒ Array
A list of methods available on this resource.
-
#discovered_resources ⇒ Array
A list of sub-resources available on this resource.
-
#initialize(api, method_base, resource_name, discovery_document) ⇒ Google::APIClient::Resource
constructor
Creates a description of a particular version of a resource.
-
#inspect ⇒ String
Returns a
String
representation of the resource’s state. -
#to_h ⇒ Hash
Converts the resource to a flat mapping of RPC names and method objects.
Constructor Details
#initialize(api, method_base, resource_name, discovery_document) ⇒ Google::APIClient::Resource
Creates a description of a particular version of a 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 = (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) .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) .send(:define_method, method_name) { method } end end end |
Instance Attribute Details
#description ⇒ Hash (readonly)
Returns the parsed section of the discovery document that applies to this resource.
70 71 72 |
# File 'lib/google/api_client/discovery/resource.rb', line 70 def description @description end |
#method_base ⇒ Addressable::URI
Returns the base URI for this resource.
76 77 78 |
# File 'lib/google/api_client/discovery/resource.rb', line 76 def method_base @method_base end |
#name ⇒ String (readonly)
Returns the identifier for the resource.
63 64 65 |
# File 'lib/google/api_client/discovery/resource.rb', line 63 def name @name end |
Instance Method Details
#discovered_methods ⇒ Array
A list of methods available on this resource.
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_resources ⇒ Array
A list of sub-resources available on this resource.
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 |
#inspect ⇒ String
Returns a String
representation of the resource’s state.
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_h ⇒ Hash
Converts the resource to a flat mapping of RPC names and method objects.
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 |