Class: Idcf::Cli::Lib::Api

Inherits:
Object
  • Object
show all
Defined in:
lib/idcf/cli/lib/api.rb

Overview

api execute class

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(links: [], client: nil) ⇒ Api

initialize

Parameters:

  • links (Array) (defaults to: [])

    in Idcf::JsonHyperSchema::Expands::LinkinfoBase

  • client (Faraday) (defaults to: nil)


42
43
44
45
# File 'lib/idcf/cli/lib/api.rb', line 42

def initialize(links: [], client: nil)
  @links  = links
  @client = client
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



8
9
10
# File 'lib/idcf/cli/lib/api.rb', line 8

def client
  @client
end

Returns the value of attribute links.



8
9
10
# File 'lib/idcf/cli/lib/api.rb', line 8

def links
  @links
end

#rawObject

Returns the value of attribute raw.



8
9
10
# File 'lib/idcf/cli/lib/api.rb', line 8

def raw
  @raw
end

Class Method Details

.command_param_str(link) ⇒ Object

command str

Parameters:

  • link

Returns:

  • String



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/idcf/cli/lib/api.rb', line 15

def command_param_str(link)
  list = []
  link.url_param_names.each do |name|
    list << "<#{name}>"
  end

  if !link.query_param_names.size.zero? || !link.properties.size.zero?
    list << (param_required?(link) ? '<params>' : '[params]')
  end

  list.join(' ')
end

.param_required?(link) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
31
32
33
34
35
# File 'lib/idcf/cli/lib/api.rb', line 28

def param_required?(link)
  param_names = link.properties.keys
  get_names   = link.query_param_names
  link.required.each do |name|
    return true if param_names.include?(name) || get_names.include?(name)
  end
  false
end

Instance Method Details

#do(command, *args) ⇒ Object

do

Parameters:

  • command (String)
  • args (Array)

Returns:

  • Hash

Raises:



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/idcf/cli/lib/api.rb', line 69

def do(command, *args)
  args ||= []
  link = find_link(command)
  cli_error("Not found #{command} Api") if link.nil?

  between_param(link, args)
  @raw = do_api(link, args)
  result_log(@raw)

  return @raw.body if @raw.success?

  raise Idcf::Cli::Error::ApiError.new(@raw), ''
end

find link

Parameters:

  • command (String)

Returns:

  • Idcf::JsonHyperSchema::Expands::LinkInfoBase



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

def find_link(command)
  result = nil
  return result if @links.blank?
  @links.each do |link|
    if link.title.to_s == command.to_s
      result = link
      break
    end
  end
  result
end

#result_api(command, args, api_result, resource_id = nil) ⇒ Object

result_api

Parameters:

  • command (String)
  • args (Array)
  • api_result (Hash)
  • resource_id (defaults to: nil)


89
90
91
92
93
94
95
96
# File 'lib/idcf/cli/lib/api.rb', line 89

def result_api(command, args, api_result, resource_id = nil)
  link = find_link(command)
  cli_error("Not found #{command} Api") if link.nil?
  r_api_title = link.result_api_title
  return nil if r_api_title.empty?
  r_api_params = link.result_api_params(args, api_result, resource_id)
  self.do(r_api_title, *r_api_params)
end