Class: PHPfog

Inherits:
Object
  • Object
show all
Defined in:
lib/pf-command/phpfog.rb

Instance Method Summary collapse

Constructor Details

#initializePHPfog

Returns a new instance of PHPfog.



10
11
12
13
14
15
# File 'lib/pf-command/phpfog.rb', line 10

def initialize
  $phpfog = Rest.new("https://www.phpfog.com")
  
  load_session
  $phpfog.cookies = $session['cookies'].clone unless $session['cookies'].nil?
end

Instance Method Details

#app_delete(app_id) ⇒ Object



97
98
99
100
101
102
103
104
# File 'lib/pf-command/phpfog.rb', line 97

def app_delete(app_id)
  authorize!
  
  resp = rpeek $phpfog.get("/apps/#{app_id}")
  resp = rpeek $phpfog.delete("/apps/#{app_id}", { 'authenticity_token' => get_auth_token(resp.body) })

  resp.code == "200"
end

#authorize!Object



165
166
167
168
169
# File 'lib/pf-command/phpfog.rb', line 165

def authorize!
  unless loggedin? || () 
    throw(:halt, "Not logged in")
  end
end

#domain_available?(domain_name) ⇒ Boolean

Returns:

  • (Boolean)


106
107
108
109
110
# File 'lib/pf-command/phpfog.rb', line 106

def domain_available?(domain_name)
  authorize!
  resp = rpeek $phpfog.get("/apps/subdomain_available?app[domain_name]=#{domain_name}", nil, { 'Accept' => 'application/json, text/javascript, */*; q=0.01', 'X-Requested-With' => 'XMLHttpRequest', 'Accept-Charset' => 'ISO-8859-1,utf-8;q=0.7,*;q=0.3', 'Accept-Charset' => 'ISO-8859-1,utf-8;q=0.7,*;q=0.3', 'Connection' => 'keep-alive'  })
  return resp.code == '200' && resp.body == 'true' 
end

#get_app(app_id) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/pf-command/phpfog.rb', line 83

def get_app(app_id)
  authorize!
  
  app = {}
  
  resp = rpeek $phpfog.get("/apps/#{app_id}")
  doc = Nokogiri::HTML(resp.body)
  
  app['site_address'] = doc.css("#app-view-live a").attr('href')
  app['repo'] = doc.css("#source_code ul code").text.strip[2..-1]
  
  app
end

#get_apps(cloud_id) ⇒ Object



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
# File 'lib/pf-command/phpfog.rb', line 42

def get_apps(cloud_id)
  authorize!
  
  apps_url = nil
  app_item_selector = nil
  
  
  if cloud_id == '1' || cloud_id == 'shared'
    apps_url = '/account'
    app_item_selector = '#clouds li:last .drop-down li'
    app_link_selector = 'a'
    app_status_selector = nil
  else
    apps_url = "/clouds/#{cloud_id}"
    app_item_selector = '#apps li.app'
    app_link_selector = 'h4 a'
    app_status_selector = '.title span'
  end
  
  resp = rpeek $phpfog.get(apps_url)

  doc = Nokogiri::HTML(resp.body)

  apps = Array.new
  app_items = doc.css(app_item_selector)
  app_items.each do |app_item|
    app_link = app_item.at_css(app_link_selector)
    app_name = app_link.text.strip
    app_href = app_link.attr('href')
    app_status = app_item.at_css(app_status_selector).text.strip unless app_status_selector.nil?

    appIdRe = /\/(\d+)/
    m = appIdRe.match(app_href)
    app_id = m.captures.shift unless m.nil?

    apps << { 'id' => app_id || 1, 'link' => app_href, 'name' => app_name, 'status' => app_status }
  end

  apps
end

#get_cloudsObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/pf-command/phpfog.rb', line 17

def get_clouds
  authorize!
  
  resp = rpeek $phpfog.get("/account")
  
  doc = Nokogiri::HTML(resp.body)
  
  clouds = Array.new
  cloud_items = doc.css("li.cloud")
  cloud_items.each do |cloud_item|
    cloud_link = cloud_item.at_css("h4 a")
    cloud_name = !cloud_link.nil? ? cloud_link.text.strip : 'Shared Cloud'
    cloud_href = !cloud_link.nil? ? cloud_link.attr('href') : ''
    cloud_desc = cloud_item.at_css(".title p").text.strip
    
    cloudIdRe = /\/(\d+)/
    m = cloudIdRe.match(cloud_href)
    cloud_id = m.captures.shift unless m.nil?
    
    clouds << { 'id' => cloud_id || 1, 'link' => cloud_href, 'name' => cloud_name, 'description' => cloud_desc }
  end

  clouds
end

#loggedin?Boolean

Returns:

  • (Boolean)


132
133
134
135
136
137
138
139
# File 'lib/pf-command/phpfog.rb', line 132

def loggedin?
  if $isLoggedIn == false
    rpeek $phpfog.get("/login") # required to establish session
    resp = rpeek $phpfog.get("/account")
    $isLoggedIn = resp.code == "200"
  end
  $isLoggedIn
end

#loginObject



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/pf-command/phpfog.rb', line 141

def ()
  username = (prompt "Username: ").strip
  password = (prompt "Password: ", true).strip
  
  # open session
  resp = rpeek $phpfog.get("/login")
  resp = rpeek $phpfog.post("/user_session", 
                  { 'authenticity_token' => get_auth_token(resp.body), 
                    'user_session[login]' => username, 
                    'user_session[password]' => password, 
                    'user_session[remember_me]' => '0', 
                    'commit' => 'login' })

  if resp.code == '302'
    puts cyan "Login Successfull."
    $isLoggedIn = true
  else
    puts red "Login Failed."
  end

  resp = rpeek $phpfog.get("/account")
  resp.code == "200"
end

#new_app(cloud_id, jumpstart_id, domain_name, mysql_password) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/pf-command/phpfog.rb', line 112

def new_app(cloud_id, jumpstart_id, domain_name, mysql_password)
  authorize!
  
  resp = rpeek $phpfog.get("/apps/new?cloud_id=#{cloud_id}")
  resp = rpeek $phpfog.post("/apps", { 'authenticity_token' => get_auth_token(resp.body),
                                        'cloud_id' => cloud_id,
                                        'app[jump_start_id]' => jumpstart_id,
                                        'app[login]' => 'Custom App',
                                        'app[password]' => mysql_password, 
                                        'app[domain_name]' => domain_name })
 
 
  if resp.code == "302"
    appIdRe = /\/(\d+)/
    m = appIdRe.match(resp['location'])
    return m.captures.shift unless m.nil?
  end                                   
  nil
end