Skip to content

Commit

Permalink
Merge pull request #123 from arangamani/symbolize_hash_keys_for_login…
Browse files Browse the repository at this point in the history
…_script

Symbolize hash keys for login script
  • Loading branch information
arangamani committed Mar 5, 2015
2 parents eeb8ce3 + 996394c commit 3b2ccfc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
20 changes: 20 additions & 0 deletions lib/jenkins_api_client/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ class Client
# @raise [ArgumentError] when required options are not provided.
#
def initialize(args)
args = symbolize_keys(args)
args.each do |key, value|
if value && VALID_PARAMS.include?(key.to_s)
instance_variable_set("@#{key}", value)
Expand Down Expand Up @@ -683,6 +684,25 @@ def refresh_crumbs(force_refresh = false)
end
end

# Private method. Converts keys passed in as strings into symbols.
#
# @param hash [Hash] Hash containing arguments to login to jenkins.
#
def symbolize_keys(hash)
hash.inject({}){|result, (key, value)|
new_key = case key
when String then key.to_sym
else key
end
new_value = case value
when Hash then symbolize_keys(value)
else value
end
result[new_key] = new_value
result
}
end

# Private method that handles the exception and raises with proper error
# message with the type of exception and returns the required values if no
# exceptions are raised.
Expand Down
5 changes: 3 additions & 2 deletions scripts/login_with_irb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ def get_from_stdin(prompt, mask = false)
end

begin
client_opts = YAML.load_file(File.expand_path(config_file))
unless client_opts.has_key?(:username)
client_opts = Hash[YAML.load_file(File.expand_path(config_file)).map { | (k,v) | [k.to_sym, v]} ]

unless client_opts.has_key?(:username)
client_opts[:username] = prompt_for_username()
end
unless client_opts.has_key?(:password) or client_opts.has_key?(:password_base64)
Expand Down

0 comments on commit 3b2ccfc

Please sign in to comment.