Class: GData::Client::AppEngine

Inherits:
Base
  • Object
show all
Defined in:
lib/googletastic/app_engine.rb

Overview

Client class to wrap working with the TBD App Engine API.

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ AppEngine

Returns a new instance of AppEngine.



7
8
9
10
# File 'lib/googletastic/app_engine.rb', line 7

def initialize(options = {})
  options[:clientlogin_service] = 'ah'
  super(options)
end

Instance Method Details

#get(url) ⇒ Object

override so we can get cookie



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/googletastic/app_engine.rb', line 13

def get(url)
  set_cookie(url)
  response = self.make_request(:get, url)
  if response.status_code == 302
    # if it's redirecting us, we need to get the cookie
    new_cookie(url)
    # now we can make the request again
    response = self.make_request(:get, url)
  end
  response
end


44
45
46
47
48
49
50
# File 'lib/googletastic/app_engine.rb', line 44

def get_cookie(url)
  uri = URI.parse(url)
  url_for_cookie = "#{uri.scheme}://#{uri.host}/_ah/login?continue=#{url}&auth=#{auth_handler.token}"
  response = self.make_request(:get, url_for_cookie)
  cookie = response.headers["set-cookie"].split('; ',2)[0]
  cookie
end


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/googletastic/app_engine.rb', line 25

def new_cookie(url)
  cookie = get_cookie(url)
  headers["Cookie"] = cookie
  Googletastic.credentials[:cookie] = cookie
  # save the cookie to a file since we're
  # not in the browser!
  storage = Googletastic.keys.has_key?(:storage) ? Googletastic.keys[:storage] : :filesystem
  
  # heroku can't store anything on filesystem
  if storage == :filesystem
    File.open("config/gdata.yml", 'w') do |file|
      file.write Googletastic.credentials.to_yaml
    end
  elsif storage == :environment
    ENV["GOOGLETASTIC_GAE_COOKIE"] = cookie
  end
  cookie
end

#push(username, password, options = {}) ⇒ Object

globally push to google app engine, using the python library pushes files to gae required options:

folder => where the python command executes
username, password


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
# File 'lib/googletastic/app_engine.rb', line 66

def push(username, password, options = {})
  options.symbolize_keys!
  raise "Please define local GAE Folder" unless options[:folder] and File.exists?(options[:folder])
  sleep 0.5
  # update GAE
  puts "Pushing to GAE..."
  cmd = "appcfg.py update #{options[:folder]}"
  original_values = [STDOUT.sync, STDERR.sync, $expect_verbose]
  STDOUT.sync     = true
  STDERR.sync     = true
  $expect_verbose = true
  PTY.spawn("#{cmd} 2>&1") do | input, output, pid |
    begin
      input.expect("Email:") do
        output.puts("#{username}\n")
      end
      input.expect("Password") do
        output.write("#{password}\n")
      end
      while ((str = input.gets) != nil)
        if str =~ /Checking if new version is ready to serve/
          
        end
        puts str
      end
    rescue Exception => e
      puts "GAE Error... #{e.inspect}"
    end
  end
  STDOUT.sync     = original_values[0]
  STDERR.sync     = original_values[1]
  $expect_verbose = original_values[2]
  sleep 0.5
end


52
53
54
55
56
57
58
59
# File 'lib/googletastic/app_engine.rb', line 52

def set_cookie(url)
  if Googletastic.credentials[:cookie]
    headers["Cookie"] = Googletastic.credentials[:cookie]
    return headers["Cookie"]
  else
    new_cookie(url)
  end
end