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:

  • api (Google::APIClient::API)

    The API this resource belongs to.

  • method_base (Addressable::URI)

    The base URI for the service.

  • resource_name (String)

    The identifier for the resource.

  • discovery_document (Hash)

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



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# 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

Instance Attribute Details

#discovery_documentString (readonly)

Returns unparsed discovery document for the resource.

Returns:

  • (String)

    unparsed discovery document for the resource



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

def discovery_document
  @discovery_document
end

#method_baseAddressable::URI

Returns the base URI for this resource.

Returns:

  • (Addressable::URI)

    The base URI that methods are joined to.



74
75
76
# File 'lib/google/api_client/discovery/resource.rb', line 74

def method_base
  @method_base
end

#nameString (readonly)

Returns the identifier for the resource.

Returns:

  • (String)

    The resource identifier.



68
69
70
# File 'lib/google/api_client/discovery/resource.rb', line 68

def name
  @name
end

Instance Method Details

#descriptionHash

Returns a human-readable description of the resource.

Returns:

  • (Hash)

    The API description.



80
81
82
# File 'lib/google/api_client/discovery/resource.rb', line 80

def description
  return @discovery_document['description']
end

#discovered_methodsArray

A list of methods available on this resource.

Returns:

  • (Array)

    A list of Method objects.



118
119
120
121
122
123
124
125
# 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_resourcesArray

A list of sub-resources available on this resource.

Returns:



103
104
105
106
107
108
109
110
111
112
# 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

#inspectString

Returns a String representation of the resource’s state.

Returns:

  • (String)

    The resource’s state, as a String.



149
150
151
152
153
# 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

#to_hHash

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

Returns:

  • (Hash)

    All methods available on the resource.



132
133
134
135
136
137
138
139
140
141
142
143
# 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