Class: Google::APIClient::Service::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/google/api_client/service/request.rb

Overview

Handles an API request. This contains a full definition of the request to be made (including method name, parameters, body and media). The remote API call can be invoked with execute().

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(service, method, parameters) ⇒ Request

Build a request. This class should not be directly instantiated in user code; instantiation is handled by the stub methods created on Service and Resource objects.

Parameters:

  • service (Google::APIClient::Service)

    The parent Service instance that will execute the request.

  • method (Google::APIClient::Method)

    The Method instance that describes the API method invoked by the request.

  • parameters (Hash)

    A Hash of parameter names and values to be sent in the API call.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/google/api_client/service/request.rb', line 37

def initialize(service, method, parameters)
  @service = service
  @method = method
  @parameters = parameters
  @body = nil
  @media = nil

  metaclass = (class << self; self; end)

  # If applicable, add "body", "body=" and resource-named methods for
  # retrieving and setting the HTTP body for this request.
  # Examples of setting the body for files.insert in the Drive API:
  #   request.body = object
  #   request.execute
  #  OR
  #   request.file = object
  #   request.execute
  #  OR
  #   request.body(object).execute
  #  OR
  #   request.file(object).execute
  # Examples of retrieving the body for files.insert in the Drive API:
  #   object = request.body
  #  OR
  #   object = request.file
  if method.request_schema
    body_name = method.request_schema.data['id'].dup
    body_name[0] = body_name[0].chr.downcase
    body_name_equals = (body_name + '=').to_sym
    body_name = body_name.to_sym

    metaclass.send(:define_method, :body) do |*args|
      if args.length == 1
        @body = args.first
        return self
      elsif args.length == 0
        return @body
      else
        raise ArgumentError,
          "wrong number of arguments (#{args.length}; expecting 0 or 1)"
      end
    end

    metaclass.send(:define_method, :body=) do |body|
      @body = body
    end

    metaclass.send(:alias_method, body_name, :body)
    metaclass.send(:alias_method, body_name_equals, :body=)
  end

  # If applicable, add "media" and "media=" for retrieving and setting
  # the media object for this request.
  # Examples of setting the media object:
  #   request.media = object
  #   request.execute
  #  OR
  #   request.media(object).execute
  # Example of retrieving the media object:
  #   object = request.media
  if method.media_upload
    metaclass.send(:define_method, :media) do |*args|
      if args.length == 1
        @media = args.first
        return self
      elsif args.length == 0
        return @media
      else
        raise ArgumentError,
          "wrong number of arguments (#{args.length}; expecting 0 or 1)"
      end
    end

    metaclass.send(:define_method, :media=) do |media|
      @media = media
    end
  end
end

Instance Attribute Details

#methodGoogle::APIClient::Method (readonly)

Returns the Method instance that describes the API method invoked by the request.

Returns:



127
128
129
# File 'lib/google/api_client/service/request.rb', line 127

def method
  @method
end

#parametersHash

Contains the Hash of parameter names and values to be sent as the parameters for the API call.

Returns:

  • (Hash)

    The request parameters.



134
135
136
# File 'lib/google/api_client/service/request.rb', line 134

def parameters
  @parameters
end

#serviceGoogle::APIClient::Service (readonly)

Returns the parent service capable of executing this request.

Returns:



120
121
122
# File 'lib/google/api_client/service/request.rb', line 120

def service
  @service
end

Instance Method Details

#executeObject

Executes the request.



138
139
140
# File 'lib/google/api_client/service/request.rb', line 138

def execute
  @service.execute(self)
end