Class: Fjords::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/fjords-client/version.rb,
lib/fjords-client/fjords-client.rb

Defined Under Namespace

Classes: BadPathError, ClientError, Conflict, ConnectionRefused, Forbidden, InternalServerError, LoginInvalid, NotAuthorizedError, NothingToUpload, PreconditionFailed, ResourceNotFound, TooManyConnections, UploadTooLarge

Constant Summary collapse

VERSION =
"1.1.1"
EXCLUSION_GLOBS =
['..', '.', '*~', '#*#', '.*', '*/.*']

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.hostObject

Returns the value of attribute host.



26
27
28
# File 'lib/fjords-client/fjords-client.rb', line 26

def host
  @host
end

Instance Attribute Details

#usernameObject (readonly)

Returns the value of attribute username.



103
104
105
# File 'lib/fjords-client/fjords-client.rb', line 103

def username
  @username
end

Class Method Details

.ruby_zip=(ruby_zip) ⇒ Object



44
45
46
# File 'lib/fjords-client/fjords-client.rb', line 44

def ruby_zip=(ruby_zip)
  @_ruby_zip = ruby_zip
end

.ruby_zip?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/fjords-client/fjords-client.rb', line 40

def ruby_zip?
  @_ruby_zip ||= false
end

.sharedObject



28
29
30
# File 'lib/fjords-client/fjords-client.rb', line 28

def shared
  @_shared_client ||= new
end

Instance Method Details

#bugreport(body, attach = nil) ⇒ Object



282
283
284
285
286
287
288
289
290
# File 'lib/fjords-client/fjords-client.rb', line 282

def bugreport(body, attach=nil)
  params = { :body => body }
  if attach
    params[:attach] = File.new(attach, 'r')
  end

  json = post("/bugreport", params)
  json['bugreport']
end

#delete_site(domain_name) ⇒ Object



174
175
176
177
# File 'lib/fjords-client/fjords-client.rb', line 174

def delete_site(domain_name)
  json = delete("/sites", :domain => domain_name)
  !!json["site"]
end

#disable_cdn(domain) ⇒ Object



254
255
256
257
258
259
# File 'lib/fjords-client/fjords-client.rb', line 254

def disable_cdn(domain)
  json = post("/cdn/disable", :domain => domain)
  json['site']
rescue ResourceNotFound
  false
end

#disable_gzip(domain) ⇒ Object



275
276
277
278
279
280
# File 'lib/fjords-client/fjords-client.rb', line 275

def disable_gzip(domain)
  json = post("/gzip/disable", :domain => domain)
  json['site']
rescue ResourceNotFound
  false
end

#enable_cdn(domain) ⇒ Object



247
248
249
250
251
252
# File 'lib/fjords-client/fjords-client.rb', line 247

def enable_cdn(domain)
  json = post("/cdn/enable", :domain => domain)
  json['site']
rescue ResourceNotFound
  false
end

#enable_gzip(domain) ⇒ Object



268
269
270
271
272
273
# File 'lib/fjords-client/fjords-client.rb', line 268

def enable_gzip(domain)
  json = post("/gzip/enable", :domain => domain)
  json['site']
rescue ResourceNotFound
  false
end

#get_stripe_token(name_on_card, number, exp_month, exp_year, cvc) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
# File 'lib/fjords-client/fjords-client.rb', line 124

def get_stripe_token(name_on_card, number, exp_month, exp_year, cvc)
  Stripe::Token.create(
    :card => {
      :name => name_on_card,
      :number => number.to_s,
      :exp_month => exp_month.to_i,
      :exp_year => exp_year.to_i,
      :cvc => cvc.to_i
    }
  )
end

#has_cdn(domain) ⇒ Object



240
241
242
243
244
245
# File 'lib/fjords-client/fjords-client.rb', line 240

def has_cdn(domain)
  json = get("/site/details", :params => { :domain => domain })
  json['site']['cdn_enabled']
rescue ResourceNotFound
  false
end

#has_gzip(domain) ⇒ Object



261
262
263
264
265
266
# File 'lib/fjords-client/fjords-client.rb', line 261

def has_gzip(domain)
  json = get("/site/details", :params => { :domain => domain })
  json['site']['gzip_assets']
rescue ResourceNotFound
  false
end

#infoObject



143
144
145
146
# File 'lib/fjords-client/fjords-client.rb', line 143

def info
  json = get("/info")
  json
end

#logged_in?Boolean

Returns:

  • (Boolean)


105
106
107
# File 'lib/fjords-client/fjords-client.rb', line 105

def logged_in?
  read_token
end

#login(username, password) ⇒ Object



136
137
138
139
140
141
# File 'lib/fjords-client/fjords-client.rb', line 136

def (username, password)
  json = post("/login", { :username => username, :password => password })
  token = json['token']
  store_token(token)
  Token.new(token)
end

#logoutObject



155
156
157
# File 'lib/fjords-client/fjords-client.rb', line 155

def logout
  remove_token
end

#prepare_push(path, checksums = nil, domain_file = "Fjordsfile") ⇒ Object

Raises:



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/fjords-client/fjords-client.rb', line 179

def prepare_push(path, checksums=nil, domain_file="Fjordsfile")
  raise BadPathError.new unless File.exists?(path) && File.directory?(path)

  if checksums && checksums["site"] && checksums["site"]["existing_hashes"]
    existing_hashes = checksums["site"]["existing_hashes"]
  end

  zipfile, changeless = zip!(path, domain_file, existing_hashes || nil)
  upload_size = File.size(zipfile)

  if upload_size > (1024*1024*50)
    raise UploadTooLarge.new
  end

  if upload_size > 1024*1024
    upload_size  = (upload_size/(1024.0*1024.0)).ceil.to_s + 'M'
  elsif upload_size > 0
    upload_size  = (upload_size/1024.0).ceil.to_s + 'K'
  else
    upload_size = '0K'
  end

  [zipfile, changeless, upload_size]
end

#push(zipfile, changeless = [], domain = nil, permanent = nil, &block) ⇒ Object

Raises:



204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/fjords-client/fjords-client.rb', line 204

def push(zipfile, changeless=[], domain=nil, permanent=nil, &block)
  raise BadPathError.new unless File.exists?(zipfile)

  data = {
    :site_data => File.new(zipfile, 'rb'),
    :changeless => changeless
  }
  data[:domain] = domain if domain
  data[:permanent] = permanent if permanent
  
  json = post("/sites", data)

  wait_until_complete(json['site'], &block)
end

#signup(email, username, password, stripe_token) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/fjords-client/fjords-client.rb', line 109

def (email, username, password, stripe_token)
  json = post("/accounts", {
    :email => email,
    :username => username,
    :password => password,
    :stripe_token => stripe_token
  })

  token = json['token']

  store_token(token)

  Token.new(token)
end

#site_details(domain) ⇒ Object



148
149
150
151
152
153
# File 'lib/fjords-client/fjords-client.rb', line 148

def site_details(domain)
  json = get("/site/details", :params => { :domain => domain })
  json['site']
rescue ResourceNotFound
  false
end

#sites(include_deleted = false) ⇒ Object



235
236
237
238
# File 'lib/fjords-client/fjords-client.rb', line 235

def sites(include_deleted=false)
  json = get("/sites", :params => { :include_deleted => include_deleted })
  json['sites']
end

#username_available?(username) ⇒ Boolean

Returns:

  • (Boolean)


159
160
161
162
163
164
# File 'lib/fjords-client/fjords-client.rb', line 159

def username_available?(username)
  get("/users/#{username}")
  false
rescue ResourceNotFound
  true
end

#validate_domain(domain) ⇒ Object



166
167
168
169
170
171
172
# File 'lib/fjords-client/fjords-client.rb', line 166

def validate_domain(domain)
  post("/site_available", {
    :domain => domain
  })
rescue Conflict
  false
end

#wait_until_complete(site, &block) ⇒ Object



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/fjords-client/fjords-client.rb', line 219

def wait_until_complete(site, &block)
  sleep(3)

  json = get("/site/details", :params => { :domain => site["custom_name"] })

  block.call(json['site']) if block_given?

  state = json['site']['state']

  if state != "live"
    wait_until_complete(site, &block)
  else
    json
  end
end