Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert hash query arguments to json #539

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,14 @@ Docker::Image.build_from_dir('.') do |v|
end
end

# Create an Image from a Dockerfile with buildargs
Docker::Image.build_from_dir('.', {
'buildargs' => {
'arg1' => 'value1',
'arg2' => 'value2'
}
})

# Create an Image from a tar file.
# docker command for reference: docker build - < docker_image.tar
Docker::Image.build_from_tar(File.open('docker_image.tar', 'r'))
Expand Down
13 changes: 12 additions & 1 deletion lib/docker/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def compile_request_params(http_method, path, query = nil, opts = nil, &block)
{
:method => http_method,
:path => "/v#{Docker::API_VERSION}#{path}",
:query => query,
:query => compile_nested_request_hash(query),
:headers => { 'Content-Type' => content_type,
'User-Agent' => user_agent,
}.merge(headers),
Expand All @@ -90,4 +90,15 @@ def compile_request_params(http_method, path, query = nil, opts = nil, &block)
:request_block => block,
}.merge(opts).reject { |_, v| v.nil? }
end

def compile_nested_request_hash(query = nil)
return query unless query.respond_to?(:each_with_object)
query.each_with_object({}) do |(k, v), h|
h[k] = if v.is_a?(Hash)
MultiJson.dump(v)
else
v
end
end
end
end
39 changes: 39 additions & 0 deletions spec/docker/connection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,43 @@
expect(subject.to_s).to eq expected_string
end
end

describe '#compile_request_params' do
let(:url) { 'http://localhost:4243' }
let(:method) { :get }
let(:path) { '/test/' }
let(:query) do
{
'simple' => true,
'complex' => {
'test' => 'hash',
'query' => 'to_json'
}
}
end
let(:options) do
{
expects: 201,
headers: {'X-Custom-Header' => 'RSpec Testing'},
}
end

let(:compiled_params) do
{
expects: 201,
headers: { 'Content-Type' => 'text/plain',
'User-Agent' => "Swipely/Docker-API #{Docker::VERSION}",
'X-Custom-Header' => 'RSpec Testing'
},
idempotent: true,
method: :get,
path: "/v#{Docker::API_VERSION}#{path}",
query: { 'simple' => true, 'complex' => '{"test":"hash","query":"to_json"}' }
}
end
subject { described_class.new(url, options) }
it 'creates a request hash' do
expect(subject.send(:compile_request_params, method, path, query, options)).to eq compiled_params
end
end
end