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, Token

Constant Summary collapse

VERSION =
'0.2.0'.freeze

Instance Attribute 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
# File 'lib/jerakia/client.rb', line 16

def initialize(opts={})
  @config = default_config.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

Instance Method Details

#default_configObject



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

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

#default_lookup_optionsObject



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

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

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



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

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"
  end
  return JSON.parse(response.body)
end

#put(url_path, params) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/jerakia/client.rb', line 63

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



39
40
41
42
43
44
# File 'lib/jerakia/client.rb', line 39

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

#url_addressObject



46
47
48
# File 'lib/jerakia/client.rb', line 46

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