Class: GoogleClient::HttpConnection

Inherits:
Object
  • Object
show all
Defined in:
lib/google_client/http_connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri, query_params = {}, headers = {}) ⇒ HttpConnection

Returns a new instance of HttpConnection.



12
13
14
15
16
17
18
19
20
# File 'lib/google_client/http_connection.rb', line 12

def initialize(uri, query_params = {}, headers = {})
  @headers = headers
  uri = URI.parse(uri)
  @host = uri.host
  @port = uri.port
  @scheme = uri.scheme
  @query_params = query_params
  @headers = headers
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



8
9
10
# File 'lib/google_client/http_connection.rb', line 8

def access_token
  @access_token
end

#headersObject

Returns the value of attribute headers.



7
8
9
# File 'lib/google_client/http_connection.rb', line 7

def headers
  @headers
end

#query_paramsObject

Returns the value of attribute query_params.



9
10
11
# File 'lib/google_client/http_connection.rb', line 9

def query_params
  @query_params
end

Instance Method Details

#create_uri(path, query_params = {}) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/google_client/http_connection.rb', line 58

def create_uri(path, query_params = {})
  Addressable::URI.new({:host => @host, 
                              :port => @port, 
                              :scheme => @scheme, 
                              :path => path,
                              :query => create_query(query_params)})
end

#delete(path, query_params = {}, headers = {}) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/google_client/http_connection.rb', line 50

def delete(path, query_params = {}, headers = {})
  uri = create_uri(path, {})
  uri = uri.to_s[0..-2]
  RestClient.delete(uri.to_s, headers.merge({:Authorization => self.headers[:Authorization]})) do |response, request, result, &block|
    handle_response(response, request, result, &block)
  end
end

#get(path, query_params = {}, headers = {}) ⇒ Object

Http::GET request

Parameters

  • path URI path

  • query_params query parameters to be added to the request

Return

  • Hash JSON decoded response



31
32
33
34
35
36
# File 'lib/google_client/http_connection.rb', line 31

def get(path, query_params = {}, headers = {})
  uri = create_uri(path, self.query_params.merge(query_params))
  RestClient.get(uri.to_s, self.headers.merge(headers)) do |response, request, result, &block|
    handle_response(response, request, result, &block)
  end
end

#post(path, body = {}, headers = {}) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/google_client/http_connection.rb', line 38

def post(path, body = {}, headers = {})
  uri = create_uri(path, {})

  _headers = self.headers.merge(headers)
  if _headers.has_key?("Content-Type") && _headers["Content-Type"].eql?("json")
    body.is_a?(Hash) and body = body.to_json
  end
  RestClient.post(uri.to_s, body, _headers) do |response, request, result, &block|
    handle_response(response, request, result, &block)
  end
end