Class: Yadecli::Client::BaseClient

Inherits:
Object
  • Object
show all
Defined in:
lib/yadecli/client/base_client.rb

Overview

base client

Instance Method Summary collapse

Constructor Details

#initialize(microservice, resource_name) ⇒ BaseClient

initialize prepare options hash with authorization header for httpparty requests



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/yadecli/client/base_client.rb', line 12

def initialize(microservice, resource_name)
  @microservice = microservice
  @resource_name = resource_name

  expiration_time = Time.parse(AppConfig['expiration_time'].to_s)

  access_token = AppConfig['access_token']

  if Time.now > expiration_time
    @authentication_client = Yadecli::Client::AuthenticationClient.new

    response = @authentication_client.request_token AppConfig['auth_url'], AppConfig['username'], AppConfig['password']

    AppConfig['access_token'] = response['access_token']
    AppConfig['refresh_token'] = response['refresh_token']
    AppConfig['expiration_time'] = Time.now + response['expires_in']

    AppConfig.write!

    access_token = response['access_token']
  end

  @options = { headers: { Authorization: "Bearer #{access_token}", 'Content-Type' => 'application/json' } }
end

Instance Method Details

#base_pathObject



53
54
55
56
57
58
59
# File 'lib/yadecli/client/base_client.rb', line 53

def base_path
  if @resource_name.end_with? 's'
    "/#{@microservice}/api/#{@resource_name}"
  else
    "/#{@microservice}/api/#{@resource_name}s"
  end
end

#get(id) ⇒ Object

get by id



47
48
49
50
51
# File 'lib/yadecli/client/base_client.rb', line 47

def get(id)
  response = get_request("#{id}")

  create_instance.new(response.parsed_response)
end

#get_request(path = nil) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/yadecli/client/base_client.rb', line 61

def get_request(path = nil)
  full_path = base_path

  full_path += "/#{path}" if path != nil

  self.class.get(full_path, @options)
end

#listObject

list all



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

def list
  response = self.class.get(base_path, @options)

  response.parsed_response.map do |p|
    create_instance.new(p)
  end
end