Class: GDataPlus::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/gdata_plus/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(authenticator, default_gdata_version = "2.0") ⇒ Client

Returns a new instance of Client.



7
8
9
10
# File 'lib/gdata_plus/client.rb', line 7

def initialize(authenticator, default_gdata_version = "2.0")
  @authenticator = authenticator
  @default_gdata_version = default_gdata_version
end

Instance Attribute Details

#authenticatorObject (readonly)

Returns the value of attribute authenticator.



5
6
7
# File 'lib/gdata_plus/client.rb', line 5

def authenticator
  @authenticator
end

#default_gdata_versionObject (readonly)

Returns the value of attribute default_gdata_version.



5
6
7
# File 'lib/gdata_plus/client.rb', line 5

def default_gdata_version
  @default_gdata_version
end

Instance Method Details

#submit(request, options = {}) ⇒ Object

FIXME detect infinite redirect



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/gdata_plus/client.rb', line 13

def submit(request, options = {})
  options = ::GDataPlus::Util.prepare_options(options, [], [:gdata_version, :hydra, :no_redirect])
  hydra = options[:hydra] || Typhoeus::Hydra.hydra

  request.headers.merge!("GData-Version" => options[:gdata_version] || default_gdata_version)
  @authenticator.sign_request(request)

  # add "If-Match: *" header if there is not already a conditional header
  unless request.headers.keys.any? { |key| key =~ /^If-/ }
    request.headers.merge!("If-Match" => "*")
  end

  hydra.queue(request)
  hydra.run
  response = request.response

  # automatically follow redirects since some GData APIs (like Calendar) redirect GET requests
  if request.method.to_sym == :get && !options[:no_redirect] && (300..399).include?(response.code)
    response = submit ::Typhoeus::Request.new(response.headers_hash["Location"], :method => :get), options.merge(:no_redirect => true)
  end

  Util.raise_if_error(response)

  response
end