Class: Jerakia::Client
- Inherits:
-
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, ScopeNotFoundError, Token
Constant Summary
collapse
- VERSION =
'0.5.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
#config ⇒ Object
Returns the value of attribute config.
15
16
17
|
# File 'lib/jerakia/client.rb', line 15
def config
@config
end
|
Class Method Details
.config_file ⇒ Object
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_file ⇒ Object
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_config ⇒ Object
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_options ⇒ Object
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
97
98
99
100
101
102
|
# File 'lib/jerakia/client.rb', line 97
def get(url_path, params={})
uri = URI.parse(url_address + url_path)
uri.query = URI.encode_www_form(params)
request = Net::HTTP::Get.new(uri.to_s)
return http_send(request)
end
|
#http_send(request) ⇒ Object
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
# File 'lib/jerakia/client.rb', line 77
def http_send(request)
request.add_field("X-Authentication", token)
response = Net::HTTP.new(@config[:host], @config[:port]).start do |http|
http.request(request)
end
case response.code
when "200"
return JSON.parse(response.body)
when "401"
raise Jerakia::Client::AuthorizationError, "Request not authorized"
when "501"
raise Jerakia::Client::ScopeNotFoundError, "Scope data not found" if response.body =~ /No scope data found/
raise Jerakia::Client::Error, response.body
else
raise Jerakia::Client::Error, response.body
end
end
|
#put(url_path, params) ⇒ Object
104
105
106
107
108
109
110
|
# File 'lib/jerakia/client.rb', line 104
def put(url_path, params)
uri = URI.parse(url_address + url_path)
request = Net::HTTP::Put.new(uri.path)
request.add_field('Content-Type', 'application/json')
request.body = params.to_json
return http_send(request)
end
|
#token ⇒ Object
66
67
68
69
70
71
|
# File 'lib/jerakia/client.rb', line 66
def token
jerakia_token = ENV['JERAKIA_TOKEN'] || @config[:token]
@token ||= jerakia_token
raise Jerakia::Client::Error, "No authorization token available" if @token.nil?
@token
end
|
#url_address ⇒ Object
73
74
75
|
# File 'lib/jerakia/client.rb', line 73
def url_address
"#{@config[:proto]}://#{@config[:host]}:#{@config[:port]}"
end
|