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 =
'1.0.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.
17
18
19
20
21
22
23
24
25
|
# File 'lib/jerakia/client.rb', line 17
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.
16
17
18
|
# File 'lib/jerakia/client.rb', line 16
def config
@config
end
|
Class Method Details
.config_file ⇒ Object
27
28
29
30
31
32
33
34
35
|
# File 'lib/jerakia/client.rb', line 27
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
37
38
39
40
41
42
43
44
45
46
47
48
49
|
# File 'lib/jerakia/client.rb', line 37
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
51
52
53
54
55
56
57
58
59
|
# File 'lib/jerakia/client.rb', line 51
def default_config
{
:host => 'localhost',
:port => 9843,
:api => 'v1',
:proto => 'http',
:content_type => 'json',
}
end
|
#default_lookup_options ⇒ Object
61
62
63
64
65
66
|
# File 'lib/jerakia/client.rb', line 61
def default_lookup_options
{
:policy => :default,
:lookup_type => :first,
}
end
|
#encode_request(request) ⇒ Object
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
# File 'lib/jerakia/client.rb', line 102
def encode_request(request)
if not @config.key?(:content_type) or @config[:content_type] == 'json'
request.add_field('Content-Type', 'application/json')
if request.key?('body')
request.body.to_json
end
elsif @config[:content_type] == 'msgpack'
request.add_field('Content-Type', 'application/x-msgpack')
if request.key?('body')
request.body.to_msgpack
end
else
raise Jerakia::Client::Error, "Invalid setting \"#{@config[:content_type]}\" for \":content_type\" in config - supported is either json or msgpack."
exit(false)
end
return request
end
|
#get(url_path, params = {}) ⇒ Object
120
121
122
123
124
125
126
|
# File 'lib/jerakia/client.rb', line 120
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)
request = encode_request(request)
return http_send(request)
end
|
#http_send(request) ⇒ Object
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
# File 'lib/jerakia/client.rb', line 79
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"
if not @config.key?(:content_type) or @config[:content_type] == 'json'
return JSON.parse(response.body)
else @config[:content_type] == 'msgpack'
return MessagePack.unpack(response.body)
end
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
128
129
130
131
132
133
134
135
|
# File 'lib/jerakia/client.rb', line 128
def put(url_path, params)
uri = URI.parse(url_address + url_path)
request = Net::HTTP::Put.new(uri.path)
request = set_content_type(request)
request.body = params
request = encode_request(request)
return http_send(request)
end
|
#token ⇒ Object
68
69
70
71
72
73
|
# File 'lib/jerakia/client.rb', line 68
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
75
76
77
|
# File 'lib/jerakia/client.rb', line 75
def url_address
"#{@config[:proto]}://#{@config[:host]}:#{@config[:port]}"
end
|