class Yade::Common::Client::BaseClient

base client

Public Class Methods

new(microservice, resource_name, model_class) click to toggle source

initialize prepare options hash with authorization header for httpparty requests

# File lib/yade/common/client/base_client.rb, line 11
def initialize(microservice, resource_name, model_class)
  @microservice = microservice
  @resource_name = resource_name
  @model_class = model_class

  @authentication_client = Yade::Common::Client::AuthenticationClient.new
  @options = { headers: { Authorization: "Bearer #{@authentication_client.access_token}", 'Content-Type' => 'application/json' } }
end

Public Instance Methods

get(id) click to toggle source

get by id

# File lib/yade/common/client/base_client.rb, line 30
def get(id)
  response = get_request("#{id}")

  if response.success?
    create_instance.new(response.parsed_response)
  else
    raise "Unable to make get request. Response code was #{response.code}"
  end
end
list() click to toggle source

list all

# File lib/yade/common/client/base_client.rb, line 21
def list
  response = self.class.get(base_path, @options)

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

Private Instance Methods

base_path() click to toggle source
# File lib/yade/common/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
create_instance() click to toggle source
# File lib/yade/common/client/base_client.rb, line 61
def create_instance
  Object.const_get(@model_class)
end
full_path(path = nil) click to toggle source
# File lib/yade/common/client/base_client.rb, line 46
def full_path(path = nil)
  full_path = base_path
  full_path += "/#{path}" if path != nil

  full_path
end
get_request(path = nil) click to toggle source
# File lib/yade/common/client/base_client.rb, line 42
def get_request(path = nil)
  self.class.get(full_path(path), @options)
end