xfrtuc 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 2.1.2
1
+ 1.9.3-p484
data/Gemfile CHANGED
@@ -1,4 +1,6 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
+ ruby '1.9.3'
4
+
3
5
  # Specify your gem's dependencies in xfrtuc.gemspec
4
6
  gemspec
data/Gemfile.lock CHANGED
@@ -1,21 +1,19 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- xfrtuc (0.0.2)
4
+ xfrtuc (0.0.3)
5
5
  rest-client (~> 1.6)
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
9
9
  specs:
10
10
  diff-lcs (1.2.5)
11
- json (1.8.1)
12
- mime-types (1.25.1)
11
+ mime-types (2.3)
12
+ netrc (0.7.7)
13
13
  rack (1.5.2)
14
- rdoc (4.1.1)
15
- json (~> 1.4)
16
- rest-client (1.6.8)
17
- mime-types (~> 1.16)
18
- rdoc (>= 2.4.2)
14
+ rest-client (1.7.2)
15
+ mime-types (>= 1.16, < 3.0)
16
+ netrc (~> 0.7)
19
17
  rspec (3.0.0)
20
18
  rspec-core (~> 3.0.0)
21
19
  rspec-expectations (~> 3.0.0)
@@ -0,0 +1,3 @@
1
+ module Xfrtuc
2
+ VERSION = "0.0.4"
3
+ end
data/lib/xfrtuc.rb CHANGED
@@ -2,12 +2,10 @@ require 'json'
2
2
  require 'rest_client'
3
3
 
4
4
  module Xfrtuc
5
- VERSION = "0.0.3"
6
-
7
5
  class Client
8
6
  attr_reader :base_url
9
7
 
10
- def initialize(username, password, base_url: 'https://transferatu.heroku.com')
8
+ def initialize(username, password, base_url='https://transferatu.heroku.com')
11
9
  @base_url = base_url
12
10
  @username = username
13
11
  @password = password
@@ -31,7 +29,7 @@ module Xfrtuc
31
29
  @group_client ||= Xfrtuc::Group.new(self)
32
30
  else
33
31
  self.class.new(@username, @password,
34
- base_url: @base_url + "/groups/#{URI.encode(name)}")
32
+ @base_url + "/groups/#{URI.encode(name)}")
35
33
  end
36
34
  end
37
35
 
@@ -39,7 +37,7 @@ module Xfrtuc
39
37
  JSON.parse(@resource[path].get(params))
40
38
  end
41
39
 
42
- def post(path, data)
40
+ def post(path, data={})
43
41
  JSON.parse(@resource[path].post(JSON.generate(data)))
44
42
  end
45
43
 
@@ -81,7 +79,11 @@ module Xfrtuc
81
79
  class Transfer < ApiEndpoint
82
80
  def initialize(client); super; end
83
81
 
84
- def info(id, verbose: false)
82
+ def info(id, opts={})
83
+ verbose = opts.delete(:verbose) || false
84
+ unless opts.empty?
85
+ raise ArgumentError, "Unsupported option(s): #{opts.keys}"
86
+ end
85
87
  client.get("/transfers/#{id}", params: { verbose: verbose })
86
88
  end
87
89
 
@@ -89,8 +91,18 @@ module Xfrtuc
89
91
  client.get("/transfers")
90
92
  end
91
93
 
92
- def create(from_type:, from_url:, from_name: nil,
93
- to_type:, to_url:, to_name: nil, opts: {})
94
+ def create(opts)
95
+ from_type = opts.fetch :from_type
96
+ from_url = opts.fetch :from_url
97
+ to_type = opts.fetch :to_type
98
+ to_url = opts.fetch :to_url
99
+ [ :from_type, :from_url, :to_type, :to_url ].each { |key| opts.delete key }
100
+ from_name = opts.delete :from_name
101
+ to_name = opts.delete :to_name
102
+ unless opts.empty?
103
+ raise ArgumentError, "Unsupported option(s): #{opts.keys}"
104
+ end
105
+
94
106
  client.post("/transfers",
95
107
  from_type: from_type,
96
108
  from_url: from_url,
@@ -103,6 +115,14 @@ module Xfrtuc
103
115
  def delete(id)
104
116
  client.delete("/transfers/#{URI.encode(id)}")
105
117
  end
118
+
119
+ def cancel(id)
120
+ client.post("/transfers/#{URI.encode(id)}/actions/cancel")
121
+ end
122
+
123
+ def public_url(id, opts={})
124
+ client.post("/transfers/#{URI.encode(id)}/actions/public-url", opts)
125
+ end
106
126
  end
107
127
 
108
128
  class Schedule < ApiEndpoint
@@ -116,8 +136,17 @@ module Xfrtuc
116
136
  client.get("/schedules")
117
137
  end
118
138
 
119
- def create(name:, callback_url:, hour:,
120
- days: Date::DAYNAMES, timezone: 'UTC')
139
+ def create(opts)
140
+ name = opts.fetch :name
141
+ callback_url = opts.fetch :callback_url
142
+ hour = opts.fetch :hour
143
+ days = opts.fetch(:days, Date::DAYNAMES)
144
+ timezone = opts.fetch(:timezone, 'UTC')
145
+ [ :name, :callback_url, :hour, :days, :timezone ].each { |key| opts.delete key }
146
+ unless opts.empty?
147
+ raise ArgumentError, "Unsupported option(s): #{opts.keys}"
148
+ end
149
+
121
150
  client.post("/schedules",
122
151
  name: name,
123
152
  callback_url: callback_url,
data/spec/spec_helper.rb CHANGED
@@ -4,6 +4,10 @@
4
4
  # loaded once.
5
5
  #
6
6
  # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+
8
+ require 'bundler'
9
+ require 'xfrtuc'
10
+
7
11
  RSpec.configure do |config|
8
12
  config.run_all_when_everything_filtered = true
9
13
  config.filter_run :focus
data/spec/xfrtuc_spec.rb CHANGED
@@ -1,3 +1,4 @@
1
+ require 'securerandom'
1
2
  require 'spec_helper'
2
3
  require 'sham_rack'
3
4
 
@@ -84,7 +85,7 @@ module Xfrtuc
84
85
  end
85
86
  end
86
87
 
87
- def get_transfer(group_name, xfer_id, verbose: false)
88
+ def get_transfer(group_name, xfer_id, verbose=false)
88
89
  group = @groups.find { |g| g[:name] == group_name }
89
90
  if group.nil?
90
91
  [404, headers, []]
@@ -127,6 +128,33 @@ module Xfrtuc
127
128
  end
128
129
  end
129
130
 
131
+ def take_transfer_action(action, args, group_name, xfer_id)
132
+ unless @transfers.has_key? group_name
133
+ return [404, headers, []]
134
+ end
135
+ xfer = @transfers[group_name].find { |item| item[:uuid] == xfer_id }
136
+ if xfer.nil?
137
+ return [404, headers, []]
138
+ else
139
+ case action
140
+ when 'cancel' then
141
+ now = Time.now
142
+ xfer[:canceled_at] = now
143
+ return [201, headers, [ { canceled_at: now }.to_json ]]
144
+ when 'public-url' then
145
+ expires_at = if args.has_key? 'ttl'
146
+ Time.now + args['ttl'].to_i
147
+ else
148
+ Time.now + (10 * 60)
149
+ end
150
+ url = "https://example.com/backup/#{xfer[:uuid]}"
151
+ return [201, headers, [ { url: url, expires_at: expires_at }.to_json ]]
152
+ else
153
+ return [404, headers, []]
154
+ end
155
+ end
156
+ end
157
+
130
158
  def add_schedule(group_name, schedule)
131
159
  unless @schedules.has_key? group_name
132
160
  return [404, headers, []]
@@ -185,6 +213,8 @@ module Xfrtuc
185
213
  return [401, headers, ["Not authorized"]]
186
214
  end
187
215
  case path(env)
216
+ when %r{/groups/[^/]+/transfers/[^/]+/actions} then
217
+ transfers_actions_endpoint(env)
188
218
  when %r{/groups/[^/]+/transfers} then
189
219
  transfers_endpoint(env)
190
220
  when %r{/groups/[^/]+/schedules} then
@@ -196,6 +226,20 @@ module Xfrtuc
196
226
  end
197
227
  end
198
228
 
229
+ def transfers_actions_endpoint(env)
230
+ path = path(env)
231
+ group_name, xfer_id, action =
232
+ path.match(%r{\A/groups/(.*)/transfers/(.*)/actions/(.*)\z}) && [$1, $2, $3]
233
+ verb = verb(env)
234
+ if verb == 'POST'
235
+ body = body(env)
236
+ args = JSON.parse(body) unless body.empty?
237
+ take_transfer_action(action, args, group_name, xfer_id)
238
+ else
239
+ [405, headers, []]
240
+ end
241
+ end
242
+
199
243
  def transfers_endpoint(env)
200
244
  path = path(env)
201
245
  group_name, xfer_id = path.match(%r{\A/groups/(.*)/transfers(?:/(.*))?\z}) && [$1, $2]
@@ -216,7 +260,7 @@ module Xfrtuc
216
260
  if xfer_id.nil?
217
261
  list_transfers(group_name)
218
262
  else
219
- get_transfer(group_name, xfer_id, verbose: params(env)['verbose'] == 'true')
263
+ get_transfer(group_name, xfer_id, params(env)['verbose'] == 'true')
220
264
  end
221
265
  else
222
266
  [405, headers, []]
@@ -348,7 +392,7 @@ module Xfrtuc
348
392
  let(:user) { User.new(username, password) }
349
393
  let(:fakesferatu) { FakeTransferatu.new(user) }
350
394
  let(:host) { 'transferatu.example.com' }
351
- let(:client) { Client.new(username, password, base_url: "https://#{host}") }
395
+ let(:client) { Client.new(username, password, "https://#{host}") }
352
396
 
353
397
  before do
354
398
  ShamRack.mount(fakesferatu, host, 443)
@@ -482,6 +526,40 @@ module Xfrtuc
482
526
  expect(fakesferatu.last_transfer(g)).to be_nil
483
527
  end
484
528
  end
529
+
530
+ describe "#cancel" do
531
+ before do
532
+ fakesferatu.add_transfer(g, Hash[xfer_data.map { |k, v| [k.to_s, v] }])
533
+ end
534
+
535
+ it "cancels the given transfer" do
536
+ id = fakesferatu.last_transfer(g)[:uuid]
537
+ before = Time.now
538
+ cancel_data = client.group(g).transfer.cancel(id)
539
+ canceled_at = Time.parse(cancel_data["canceled_at"])
540
+ expect(canceled_at).to be_within(60).of(before)
541
+ end
542
+ end
543
+
544
+ describe "#public_url" do
545
+ before do
546
+ fakesferatu.add_transfer(g, Hash[xfer_data.map { |k, v| [k.to_s, v] }])
547
+ end
548
+
549
+ it "provides a public url for the given transfer" do
550
+ id = fakesferatu.last_transfer(g)[:uuid]
551
+ url_data = client.group(g).transfer.public_url(id)
552
+ expect { URI.parse(url_data["url"]) }.not_to raise_error
553
+ end
554
+
555
+ it "supports an optional ttl parameter" do
556
+ id = fakesferatu.last_transfer(g)[:uuid]
557
+ before = Time.now
558
+ url_data = client.group(g).transfer.public_url(id, ttl: 5 * 60)
559
+ expires_at = Time.parse(url_data["expires_at"])
560
+ expect(expires_at).to be_within(60).of(before + (5 * 60))
561
+ end
562
+ end
485
563
  end
486
564
 
487
565
  describe Schedule do
data/xfrtuc.gemspec CHANGED
@@ -1,4 +1,4 @@
1
- require_relative 'lib/xfrtuc'
1
+ require File.expand_path('../lib/xfrtuc/version', __FILE__)
2
2
 
3
3
  Gem::Specification.new do |gem|
4
4
  gem.authors = ["Maciek Sakrejda"]
metadata CHANGED
@@ -1,55 +1,62 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: xfrtuc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
+ prerelease:
5
6
  platform: ruby
6
7
  authors:
7
8
  - Maciek Sakrejda
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2014-09-03 00:00:00.000000000 Z
12
+ date: 2014-12-17 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
15
  name: rest-client
15
16
  requirement: !ruby/object:Gem::Requirement
17
+ none: false
16
18
  requirements:
17
- - - "~>"
19
+ - - ~>
18
20
  - !ruby/object:Gem::Version
19
21
  version: '1.6'
20
22
  type: :runtime
21
23
  prerelease: false
22
24
  version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
23
26
  requirements:
24
- - - "~>"
27
+ - - ~>
25
28
  - !ruby/object:Gem::Version
26
29
  version: '1.6'
27
30
  - !ruby/object:Gem::Dependency
28
31
  name: rspec
29
32
  requirement: !ruby/object:Gem::Requirement
33
+ none: false
30
34
  requirements:
31
- - - "~>"
35
+ - - ~>
32
36
  - !ruby/object:Gem::Version
33
37
  version: '3.0'
34
38
  type: :development
35
39
  prerelease: false
36
40
  version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
37
42
  requirements:
38
- - - "~>"
43
+ - - ~>
39
44
  - !ruby/object:Gem::Version
40
45
  version: '3.0'
41
46
  - !ruby/object:Gem::Dependency
42
47
  name: sham_rack
43
48
  requirement: !ruby/object:Gem::Requirement
49
+ none: false
44
50
  requirements:
45
- - - "~>"
51
+ - - ~>
46
52
  - !ruby/object:Gem::Version
47
53
  version: '1.3'
48
54
  type: :development
49
55
  prerelease: false
50
56
  version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
51
58
  requirements:
52
- - - "~>"
59
+ - - ~>
53
60
  - !ruby/object:Gem::Version
54
61
  version: '1.3'
55
62
  description: Transferatu client
@@ -59,39 +66,40 @@ executables: []
59
66
  extensions: []
60
67
  extra_rdoc_files: []
61
68
  files:
62
- - ".ruby-version"
69
+ - .ruby-version
63
70
  - Gemfile
64
71
  - Gemfile.lock
65
72
  - LICENSE
66
73
  - lib/xfrtuc.rb
74
+ - lib/xfrtuc/version.rb
67
75
  - spec/spec_helper.rb
68
76
  - spec/xfrtuc_spec.rb
69
77
  - xfrtuc.gemspec
70
78
  homepage: https://github.com/heroku/xfrtuc
71
79
  licenses:
72
80
  - MIT
73
- metadata: {}
74
81
  post_install_message:
75
82
  rdoc_options: []
76
83
  require_paths:
77
84
  - lib
78
85
  required_ruby_version: !ruby/object:Gem::Requirement
86
+ none: false
79
87
  requirements:
80
- - - ">="
88
+ - - ! '>='
81
89
  - !ruby/object:Gem::Version
82
90
  version: '0'
83
91
  required_rubygems_version: !ruby/object:Gem::Requirement
92
+ none: false
84
93
  requirements:
85
- - - ">="
94
+ - - ! '>='
86
95
  - !ruby/object:Gem::Version
87
96
  version: '0'
88
97
  requirements: []
89
98
  rubyforge_project:
90
- rubygems_version: 2.2.2
99
+ rubygems_version: 1.8.23
91
100
  signing_key:
92
- specification_version: 4
93
- summary: 'Transferatu client: see https://github.com/heroku/transferatu'
101
+ specification_version: 3
102
+ summary: ! 'Transferatu client: see https://github.com/heroku/transferatu'
94
103
  test_files:
95
104
  - spec/spec_helper.rb
96
105
  - spec/xfrtuc_spec.rb
97
- has_rdoc:
checksums.yaml DELETED
@@ -1,7 +0,0 @@
1
- ---
2
- SHA1:
3
- metadata.gz: 8d8ad3039270c13e25ec3a1754077fa7e1bd9978
4
- data.tar.gz: 8bd0b2b28529b16194059c6b57822a9dac24a98c
5
- SHA512:
6
- metadata.gz: 12eb85112d36a301c4f9a1340e4f800d1c8b86853381562ce9435d713380af313762e9b8009bd3f202bbea614424a8126f79f486c803fde7739901cc159402b8
7
- data.tar.gz: bba43317d78bd508d9c3ecc2de3ad015eeb2e79805bf107ff467ad94fdb453f7e174418b4feaf4f52edd918100182336db3fc1a7eee09c167976c7358cbc3734