p4_web_api_client 2014.2.0.pre1 → 2014.2.0.pre2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 199bda143441561260c895321ab9c1c57d42a4ef
4
- data.tar.gz: 30f094cc9402abdbc2b60707b4f16f03db187c57
3
+ metadata.gz: 7b8e4e28235231bd2413b64682c44d6bdd6d5bbb
4
+ data.tar.gz: 2ccbe57364a8ec3a59442786a5da4dde61b90286
5
5
  SHA512:
6
- metadata.gz: d91a36aad1d32dc268db168db220f0d5951cc3aabc28d53fe6b3ed6789c1c0ea30e9d07717bc458191df98870f9788dfa9d2b08e93b096ccc2335ee4e9bdc280
7
- data.tar.gz: 3500061dd4dd4084729653a53ad1a4bc269dec65d2761727d110691f6673e5860a256d0e3ad80a5da736d4e83025949c340ba5ae156e5eb1c379374edd3dc1cf
6
+ metadata.gz: 790388c690229c7ec5c2b7415b46de0cfded8c46225e7b2aec1f807541a30754ff81bf1bbc17617780df5f5c2cb43307008421b201edbf53430718f34d0b3a66
7
+ data.tar.gz: e37faaf0d997fd72505dc0cbf685bb396d8303db31fc73eae3921c48fb6943ffb62d77cb80647ba4df9fb3660b48266674d9314821648462cb5e94dec7e9c858
@@ -2,6 +2,7 @@ require 'open-uri'
2
2
  require 'p4_web_api_client/models/dir'
3
3
  require 'p4_web_api_client/models/depot'
4
4
  require 'p4_web_api_client/models/file'
5
+ require 'p4_web_api_client/models/upload_file'
5
6
 
6
7
  module P4WebApiClient
7
8
  class Client
@@ -26,5 +27,64 @@ module P4WebApiClient
26
27
  end
27
28
  end
28
29
  end
30
+
31
+ # Returns the content of the path.
32
+ #
33
+ # The path should be a depot path, we'll basically just print out the
34
+ # latest version of the file.
35
+ def print(path)
36
+ return unless path
37
+
38
+ path = path.gsub(/^[\/]*/, '') # Strip leading slashes
39
+
40
+ path = "/print/#{path}"
41
+ response = connection.run_method_no_body(:get, path)
42
+ response.body
43
+ end
44
+
45
+ # Uploads file contents to the server.
46
+ #
47
+ # Takes an array of P4WebApiClient::Models::UploadFile definitions, or
48
+ # hashes that will be turned into those UploadFile definitions. Which means
49
+ # that each hash should include the following properties:
50
+ #
51
+ # - `depot_path`
52
+ # - `content`: If this is a string, we'll assume it's a local file path,
53
+ # or it can be an IO handle, which we'll just read
54
+ # - `content_type`: If not set, we'll assume `application/octet-stream`
55
+ #
56
+ # You can optionally send in a description, which will be used as the
57
+ # changelist message.
58
+ def upload(files, description = nil)
59
+ file_models = init_upload_files(files)
60
+
61
+ post_body = {
62
+ mappings: file_models.map(&:depot_path)
63
+ }
64
+
65
+ file_models.each_index do |idx|
66
+ key = "file_#{idx}"
67
+ post_body[key.to_sym] = file_models[idx].upload_io
68
+ end
69
+
70
+ post_body[:description] = description if description
71
+
72
+ connection.run_method_with_body(:post, '/upload', nil, post_body)
73
+
74
+ true
75
+ end
76
+
77
+ private
78
+
79
+ def init_upload_files(files)
80
+ file_models = []
81
+ files.each do |file|
82
+ unless file.is_a?(Models::UploadFile)
83
+ file = Models::UploadFile.new(file)
84
+ end
85
+ file_models << file
86
+ end
87
+ file_models
88
+ end
29
89
  end
30
90
  end
@@ -8,7 +8,7 @@ module P4WebApiClient
8
8
  # arguments.
9
9
  #
10
10
  # Expect to always have an array of hashes as output.
11
- def run(cmd, args = [])
11
+ def run(cmd, *args)
12
12
  params = arg_params(args)
13
13
  params[:cmd] = cmd
14
14
  execute_method_no_body(:get, '/run', params)
@@ -20,7 +20,7 @@ module P4WebApiClient
20
20
  # then followed by additional command line arguments.
21
21
  #
22
22
  # Expect the output to always be an array of hashes.
23
- def run_input(cmd, input, args = [])
23
+ def run_input(cmd, input, *args)
24
24
  params = arg_params(args)
25
25
  params[:cmd] = cmd
26
26
  execute_method_with_body(:post, '/run', params, input)
@@ -16,7 +16,11 @@ module P4WebApiClient
16
16
  local_opts = [:login, :password, :prefix]
17
17
  conn_options = options.select { |k| !local_opts.include?(k) }
18
18
 
19
- @conn = Faraday.new(conn_options)
19
+ @conn = Faraday.new(conn_options) do |conn|
20
+ conn.request :multipart
21
+ conn.request :url_encoded
22
+ conn.adapter :net_http
23
+ end
20
24
 
21
25
  @session_token = nil
22
26
  @login = options[:login]
@@ -0,0 +1,42 @@
1
+ # Copyright (c) 2014 Perforce Software, Inc. All rights reserved.
2
+
3
+ require 'faraday'
4
+
5
+ module P4WebApiClient
6
+ module Models
7
+ # Models each argument sent into the `client.upload` method. In general,
8
+ # you'll create hashes instead of these instances directly, so this class
9
+ # is 'hidden' in usage.
10
+ class UploadFile
11
+ # Where the file should go in the Perforce server.
12
+ attr_accessor :depot_path
13
+
14
+ # The IO handle to file data. Note that the initializer will open up
15
+ # 'content' properties as local files if specified as strings.
16
+ attr_accessor :content
17
+
18
+ # The MIME type we send the data as, defaults to
19
+ # `application/octet-stream`
20
+ attr_accessor :content_type
21
+
22
+ def initialize(obj = {})
23
+ @depot_path = obj[:depot_path]
24
+
25
+ @content = obj[:content]
26
+ @content = ::File.open(@content, 'r') if @content.is_a?(String)
27
+
28
+ @content_type = obj[:content_type]
29
+ @content_type = 'application/octet-stream' unless @content_type
30
+ end
31
+
32
+ def unique_file_name
33
+ random = (0...8).map { (65 + rand(26)).chr }.join
34
+ "#{random}_#{::File.basename(depot_path)}"
35
+ end
36
+
37
+ def upload_io
38
+ Faraday::UploadIO.new(content, content_type, unique_file_name)
39
+ end
40
+ end
41
+ end
42
+ end
@@ -1,3 +1,3 @@
1
1
  module P4WebApiClient
2
- VERSION = '2014.2.0.pre1'
2
+ VERSION = '2014.2.0.pre2'
3
3
  end
data/spec/files_spec.rb CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  require 'rspec'
4
4
  require 'p4_web_api_client'
5
-
5
+ require 'tempfile'
6
6
  require 'test_connections'
7
7
 
8
8
  RSpec.describe P4WebApiClient::Client, '#files' do
@@ -34,3 +34,60 @@ RSpec.describe P4WebApiClient::Client, '#files' do
34
34
  end
35
35
  end
36
36
  end
37
+
38
+ RSpec.describe P4WebApiClient::Client, '#print' do
39
+
40
+ it 'should print the content of //depot/dev/Experimental/README' do
41
+ client_as_jdoe do |c|
42
+ content = c.print('//depot/dev/Experimental/README')
43
+ expect(content.strip).to eq('This is an experimental project README.')
44
+ end
45
+ end
46
+ end
47
+
48
+ RSpec.describe P4WebApiClient::Client, '#upload' do
49
+
50
+ it 'should be able to edit and add files' do
51
+ client_as_jdoe do |c|
52
+
53
+ exists_up = Tempfile.new('exists')
54
+ new_up = Tempfile.new('new')
55
+
56
+ begin
57
+ exists_up.write('existing file content')
58
+ new_up.write('new file content')
59
+
60
+ exists_up.rewind
61
+ new_up.rewind
62
+
63
+ c.upload(
64
+ [
65
+ {
66
+ depot_path: '//depot/dev/Experimental/README',
67
+ content: exists_up.path
68
+ },
69
+ {
70
+ depot_path: '//depot/dev/Experimental/a.txt',
71
+ content: new_up.path,
72
+ content_type: 'text/plain'
73
+ }
74
+ ]
75
+ )
76
+
77
+ ensure
78
+ exists_up.close
79
+ exists_up.unlink
80
+
81
+ new_up.close
82
+ new_up.unlink
83
+ end
84
+
85
+ exists_content = c.print('//depot/dev/Experimental/README')
86
+ expect(exists_content.strip).to eq('existing file content')
87
+
88
+ new_content = c.print('//depot/dev/Experimental/a.txt')
89
+ expect(new_content.strip).to eq('new file content')
90
+
91
+ end
92
+ end
93
+ end
@@ -16,7 +16,7 @@ RSpec.describe P4WebApiClient::Client, '#run_input' do
16
16
  'FullName' => 'Joe User',
17
17
  'Email' => 'joeuser@example.com'
18
18
  }
19
- results = c.run_input('user', input, ['-i', '-f'])
19
+ results = c.run_input('user', input, '-i', '-f')
20
20
  expect(results.length).to eq(1)
21
21
  end
22
22
  end
data/spec/run_spec.rb CHANGED
@@ -13,7 +13,7 @@ RSpec.describe P4WebApiClient::Client, '#run' do
13
13
 
14
14
  it "should allow us to run 'user -o' for our known user" do
15
15
  client_as_super do |c|
16
- results = c.run('user', ['-o'])
16
+ results = c.run('user', '-o')
17
17
  expect(results.length).to eq(1)
18
18
  expect(results[0]['User']).to eq('super')
19
19
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: p4_web_api_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 2014.2.0.pre1
4
+ version: 2014.2.0.pre2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tristan Juricek
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-25 00:00:00.000000000 Z
11
+ date: 2014-12-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -72,14 +72,14 @@ dependencies:
72
72
  requirements:
73
73
  - - "~>"
74
74
  - !ruby/object:Gem::Version
75
- version: 2014.2.0.pre1
75
+ version: 2014.2.0.pre2
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
- version: 2014.2.0.pre1
82
+ version: 2014.2.0.pre2
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: puma
85
85
  requirement: !ruby/object:Gem::Requirement
@@ -221,6 +221,7 @@ files:
221
221
  - lib/p4_web_api_client/models/server.rb
222
222
  - lib/p4_web_api_client/models/stream.rb
223
223
  - lib/p4_web_api_client/models/triggers.rb
224
+ - lib/p4_web_api_client/models/upload_file.rb
224
225
  - lib/p4_web_api_client/models/user.rb
225
226
  - lib/p4_web_api_client/version.rb
226
227
  - spec/branches_spec.rb