Class: Jerakia::Client

Inherits:
Object
  • Object
show all
Includes:
Lookup, Scope
Defined in:
lib/jerakia/client.rb,
lib/jerakia/client/cli.rb,
lib/jerakia/client/error.rb,
lib/jerakia/client/scope.rb,
lib/jerakia/client/token.rb,
lib/jerakia/client/lookup.rb,
lib/jerakia/client/version.rb,
lib/jerakia/client/cli/lookup.rb

Defined Under Namespace

Modules: Lookup, Scope Classes: AuthorizationError, CLI, Error, HTTPError, NotFoundError, Token

Constant Summary collapse

VERSION =
'0.3.0'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Scope

#get_scope, #get_scope_uuid, #send_scope

Methods included from Lookup

#lookup

Constructor Details

#initialize(opts = {}) ⇒ Client

Returns a new instance of Client.



16
17
18
19
20
21
22
23
24
# File 'lib/jerakia/client.rb', line 16

def initialize(opts={})
  config_file_opts = self.class.load_config_from_file
  @config = default_config.merge(
    config_file_opts.merge(
      opts.reject { |k,v| v.nil? }
    )
  )
  @token = nil
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



15
16
17
# File 'lib/jerakia/client.rb', line 15

def config
  @config
end

Class Method Details

.config_fileObject



26
27
28
29
30
31
32
33
34
# File 'lib/jerakia/client.rb', line 26

def self.config_file
  [
    File.join(ENV['HOME'], '.jerakia', 'jerakia.yaml'),
    '/etc/jerakia/jerakia.yaml'
  ]. each do |filename|
    return filename if File.exists?(filename)
  end
  return nil
end

.load_config_from_fileObject



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/jerakia/client.rb', line 36

def self.load_config_from_file
  filename = config_file
  confighash = {}
  return {} unless filename
  configdata = YAML.load(File.read(filename))
  if configdata['client'].is_a?(Hash)
    configdata['client'].each do |k, v|
      confighash[k.to_sym] = v
    end
    return confighash
  end
  return {}
end

Instance Method Details

#default_configObject



50
51
52
53
54
55
56
57
# File 'lib/jerakia/client.rb', line 50

def default_config
  {
  :host => 'localhost',
  :port => 9843,
  :api  => 'v1',
  :proto => 'http',
  }
end

#default_lookup_optionsObject



59
60
61
62
63
64
# File 'lib/jerakia/client.rb', line 59

def default_lookup_options
  {
    :policy => :default,
    :lookup_type => :first,
  }
end

#get(url_path, params = {}) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/jerakia/client.rb', line 77

def get(url_path, params={})
  headers = { 'X-Authentication' => token }
  uri_params = '?' + URI.encode_www_form(params)
  url = url_address + url_path + uri_params
  begin
    response = RestClient.get(url, headers)

  rescue RestClient::Unauthorized => e
    raise Jerakia::Client::AuthorizationError, "Request not authorized"
  rescue RestClient::NotFound => e
    return nil
  rescue RestClient::Exception => e
    raise Jerakia::Client::Error, e.response.body
  end
  return JSON.parse(response.body)
end

#put(url_path, params) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/jerakia/client.rb', line 94

def put(url_path, params)
  headers = {
    'X-Authentication' => token,
    :content_type => :json
  }
  url = url_address + url_path
  begin
    response = RestClient.put(url, params.to_json, headers)
  rescue RestClient::Unauthorized => e
    raise Jerakia::Client::AuthorizationError, "Request not authorized"
  end
  return JSON.parse(response.body)
end

#tokenObject



66
67
68
69
70
# File 'lib/jerakia/client.rb', line 66

def token
  @token ||= @config[:token]
  raise Jerakia::Client::Error, "No authorization token available" if @token.nil?
  @token
end

#url_addressObject



72
73
74
# File 'lib/jerakia/client.rb', line 72

def url_address
  "#{@config[:proto]}://#{@config[:host]}:#{@config[:port]}"
end