Class: SimpleGData::Request

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

Overview

A simple class that makes a oauth request using the oauth gem Initialize it by passing in a required oauth token and oauth secret recieved form an external service such as omniauth or the oauth gem.

Instance Method Summary collapse

Constructor Details

#initialize(oauth_token, oauth_secret, params = { :application_id => 'anonymous', :application_secret => 'anonymous', :site => 'https://www.google.com' }) ⇒ Request

Returns a new instance of Request.

[View source]

9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/simplegdata.rb', line 9

def initialize(oauth_token, oauth_secret, params = {
                  :application_id     => 'anonymous',
                  :application_secret => 'anonymous',
                  :site               => 'https://www.google.com'
               })
  # Sets up the class variables.  Defaults to Google's anonymous
  # usage if no account is given.
  @oauth_token        = oauth_token
  @oauth_secret       = oauth_secret
  @application_id     = params[:application_id]
  @application_secret = params[:application_secret]
  @site               = params[:site]
end

Instance Method Details

#request(rest, url, data = nil, options = nil) ⇒ Object

A class method to make an optimized request to GData’s APIS. Automatically converts the request to JSON and accepts a a ruby hash to convert to JSON to send for post data. Returns results back as a ruby array.

[View source]

27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/simplegdata.rb', line 27

def request(rest, url, data = nil, options = nil)
  # Default get requests to json 
  if rest == :get && @site == 'https://www.google.com'
    unless url.include? 'alt=json'
      if url.include? '?'
        url = url + '&alt=jsonc'
      else
        url = url + '?alt=jsonc'
      end
    end
  end
  
  # Default post requests to json
  if options.nil?
    options = { 'Content-Type' => 'application/json' }
  else
    unless options.has_key? 'Content-Type'
      options['Content-Type'] = 'application/json'
    end
  end
  
  # Convert any data passed in for post requests to JSON
  if data.class == {}.class
    data = ActiveSupport::JSON.encode(data)
  end
  
  # Run the query and return the results as a ruby object
  response = request_raw(rest, url, data, options)
  
  unless response.nil?
    ActiveSupport::JSON.decode response
  end
end

#request_raw(rest, url, data = nil, options = nil) ⇒ Object

A class method to process a raw request. All handling is done by the user. Deals with the unique issue of GData API’s sometimes returning 302 redirects with gsessionids appended to them.

[View source]

65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/simplegdata.rb', line 65

def request_raw(rest, url, data = nil, options = nil)
  begin
    response = access_token.request(rest, url, data, options)
    case response
      when Net::HTTPSuccess         then response.body
      when Net::HTTPRedirection     then request_raw(rest, response['location'], data, options)
    else
      response.error!
    end
  rescue Exception => e # TODO: Figure out some better exception handling
    nil
  end
end