Class: Dbag::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/dbag/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_url, auth_token) ⇒ Client

Returns a new instance of Client.



6
7
8
9
10
11
# File 'lib/dbag/client.rb', line 6

def initialize(base_url, auth_token)
  self.base_url = base_url
  self.auth_token = auth_token
  self.logger = Logger.new(STDOUT)
  self.logger.level = (self.log_level || Logger::DEBUG)
end

Instance Attribute Details

#auth_tokenObject

Returns the value of attribute auth_token.



4
5
6
# File 'lib/dbag/client.rb', line 4

def auth_token
  @auth_token
end

#base_urlObject

Returns the value of attribute base_url.



4
5
6
# File 'lib/dbag/client.rb', line 4

def base_url
  @base_url
end

#log_levelObject

Returns the value of attribute log_level.



4
5
6
# File 'lib/dbag/client.rb', line 4

def log_level
  @log_level
end

#loggerObject

Returns the value of attribute logger.



4
5
6
# File 'lib/dbag/client.rb', line 4

def logger
  @logger
end

Instance Method Details

#allObject



13
14
15
16
17
# File 'lib/dbag/client.rb', line 13

def all
  if (response = get_response(:get, '/data_bags.json')).response.is_a?(Net::HTTPOK)
    response.parsed_response
  end
end

#create(key, environment, data_bag = {}, encrypted = false) ⇒ Object



25
26
27
28
29
30
# File 'lib/dbag/client.rb', line 25

def create(key, environment, data_bag = {}, encrypted = false)
  raise "Invalid Databag!" unless key or data_bag
  response = get_response(:post, '/data_bags.json', 
    {:body => {:data_bag => {:key => key, :bag_string_clear => data_bag.to_json, 
     :encrypted => encrypted, :environment => (environment || 'production')}}})
end

#find(key, environment = 'production') ⇒ Object



19
20
21
22
23
# File 'lib/dbag/client.rb', line 19

def find(key, environment = 'production')
  if (response = get_response(:get, "/data_bags/#{key}.json", {:environment => environment})).response.is_a?(Net::HTTPOK)
    JSON.parse(response.parsed_response["bag_string"]) if response.parsed_response
  end
end

#from_file(new_key, environment, path, encrypted = false, format = :json) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/dbag/client.rb', line 49

def from_file(new_key, environment, path, encrypted = false, format = :json)
  File.open(path, "r" ) do |f|
    if format == :json
      if (json = JSON.load(f))
        create(new_key, environment, json, encrypted)
      end
    elsif format == :yaml
      if (yaml = YAML.load_file(path))
        create(new_key, environment, yaml, encrypted)
      end
    end
  end
end

#to_file(hash, path, format = :json) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/dbag/client.rb', line 39

def to_file(hash, path, format = :json)      
  File.open(path, "w") do |f|
    if format == :json
      f.write(JSON.pretty_generate(hash))
    elsif format == :yaml
      f.write(hash.to_yaml)
    end
  end
end

#update(key, environment, data_bag = {}, encrypted = false) ⇒ Object



32
33
34
35
36
37
# File 'lib/dbag/client.rb', line 32

def update(key, environment, data_bag = {}, encrypted = false)
  raise "Invalid Databag!" unless key or data_bag
  response = get_response(:put, "/data_bags/#{key}.json", 
    {:body => {:data_bag => {:bag_string_clear => data_bag.to_json, 
     :encrypted => encrypted}, :environment => (environment || 'production')}})
end