bitballoon 0.0.9 → 0.0.10

Sign up to get free protection for your applications and to get access to all the features.
data/bin/bitballoon CHANGED
@@ -10,6 +10,9 @@ opts = Slop.parse do
10
10
  end
11
11
 
12
12
  command 'deploy' do
13
+ on :a=, :'access-token=', 'API Access Token'
14
+ on :s=, :'site-id=', 'BitBalloon site id'
15
+
13
16
  run do |opts, args|
14
17
  path = args.first ? File.expand_path(args.first) : Dir.getwd
15
18
  zip = path.match(/\.zip$/)
@@ -17,11 +20,14 @@ opts = Slop.parse do
17
20
  raise "File or dir doesn't exist: #{path}" unless File.exist?(path)
18
21
  raise "Can't deploy a single file" if File.file?(path) && !zip
19
22
 
20
- credentials_path = File.expand_path(".bitballoon")
23
+ credentials_path = opts[:a] ? nil : File.expand_path(".bitballoon")
21
24
 
22
- if File.exist?(credentials_path)
25
+ if credentials_path && File.exist?(credentials_path)
23
26
  credentials = JSON.parse(File.read(credentials_path))
24
27
  client = BitBalloon::Client.new(:access_token => credentials['access_token'])
28
+ elsif opts[:a]
29
+ credentials = {'access_token' => opts[:a]}
30
+ client = BitBalloon::Client.new(:access_token => credentials['access_token'])
25
31
  else
26
32
  puts "Please enter your BitBalloon API Credentials (if you don't have any, you can create your credentials at https://www.bitballoon.com/applications)"
27
33
  client_id = ask("Client ID:")
@@ -31,6 +37,8 @@ opts = Slop.parse do
31
37
  credentials = {'access_token' => client.access_token}
32
38
  end
33
39
 
40
+ credentials['site_id'] = opts[:s] if opts[:s]
41
+
34
42
  attributes = zip ? {:zip => path} : {:dir => path}
35
43
 
36
44
  if credentials['site_id']
@@ -48,7 +56,7 @@ opts = Slop.parse do
48
56
 
49
57
  File.open(credentials_path, "w") do |file|
50
58
  file.write(JSON.generate(credentials))
51
- end
59
+ end if credentials_path
52
60
 
53
61
  puts "Site deployed: #{site.url}"
54
62
  end
@@ -4,6 +4,7 @@ module BitBalloon
4
4
  class Client
5
5
  ENDPOINT = ENV['OAUTH_CLIENT_API_URL'] || 'https://www.bitballoon.com'
6
6
  API_VERSION = "v1"
7
+ RETRIES = 3
7
8
 
8
9
  class BitBalloonError < StandardError; end
9
10
  class NotFoundError < BitBalloonError; end
@@ -51,25 +52,43 @@ module BitBalloon
51
52
  end
52
53
 
53
54
  def request(verb, path, opts={}, &block)
54
- raise AuthenticationError, "Authorize with BitBalloon before making requests" unless oauth_token
55
+ retries = 0
56
+ begin
57
+ raise AuthenticationError, "Authorize with BitBalloon before making requests" unless oauth_token
55
58
 
56
- oauth_token.request(verb, ::File.join("/api", API_VERSION, path), opts, &block)
57
- rescue OAuth2::Error => e
58
- case e.response.status
59
- when 401
60
- raise AuthenticationError, message_for(e, "Authentication Error")
61
- when 404
62
- raise NotFoundError, message_for(e, "Not Found")
63
- when 500
64
- raise InternalServerError, message_for(e, "Internal Server Error")
65
- else
66
- raise BitBalloonError, message_for(e, "OAuth2 Error")
59
+ oauth_token.request(verb, ::File.join("/api", API_VERSION, path), opts, &block)
60
+ rescue OAuth2::Error => e
61
+ case e.response.status
62
+ when 401
63
+ raise AuthenticationError, message_for(e, "Authentication Error")
64
+ when 404
65
+ raise NotFoundError, message_for(e, "Not Found")
66
+ when 500
67
+ if retry_request?(verb, e.response.status, retries)
68
+ retries += 1
69
+ retry
70
+ else
71
+ raise InternalServerError, message_for(e, "Internal Server Error")
72
+ end
73
+ else
74
+ raise BitBalloonError, message_for(e, "OAuth2 Error")
75
+ end
76
+ rescue Faraday::Error::ConnectionFailed, Faraday::Error::TimeoutError => e
77
+ if retry_request?(verb, e.response && e.response.status, retries)
78
+ retries += 1
79
+ retry
80
+ else
81
+ raise ConnectionError, message_for(e, "Connection Error")
82
+ end
67
83
  end
68
- rescue Faraday::Error::ConnectionFailed => e
69
- raise ConnectionError, message_for(e, "Connection Error")
70
84
  end
71
85
 
72
86
  private
87
+ def retry_request?(http_verb, status_code, retries)
88
+ return false unless [:get, :put, :delete, :head].include?(http_verb.to_s.downcase.to_sym)
89
+ return retries < 3 unless status_code && status_code == 422
90
+ end
91
+
73
92
  def oauth_token
74
93
  @oauth_token ||= access_token && OAuth2::AccessToken.new(oauth, access_token)
75
94
  end
@@ -19,6 +19,7 @@ module BitBalloon
19
19
  end
20
20
 
21
21
  (required || []).each do |sha|
22
+ puts "Uploading #{shas[sha]}"
22
23
  client.request(:put, ::File.join(path, "files", URI.encode(shas[sha])), :body => ::File.read(::File.join(dir, shas[sha])), :headers => {"Content-Type" => "application/octet-stream"})
23
24
  end
24
25
 
@@ -1,3 +1,3 @@
1
1
  module BitBalloon
2
- VERSION = "0.0.9"
2
+ VERSION = "0.0.10"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bitballoon
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.9
4
+ version: 0.0.10
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-10-08 00:00:00.000000000 Z
12
+ date: 2013-10-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: oauth2