Class: LabClient::Klass

Inherits:
LabStruct
  • Object
show all
Extended by:
Docs
Includes:
CurlHelper
Defined in:
lib/labclient/klass.rb

Overview

Common Configuration for all Class Helpers

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Docs

demo, desc, doc, docs, example, json, markdown, navigation, option, result, subtitle, title

Methods included from CurlHelper

#curl

Methods inherited from LabStruct

#as_json, #inspect, #keys, #slice

Constructor Details

#initialize(hash = nil, response = nil, client = nil) ⇒ Klass

rubocop:disable Lint/MissingSuper



105
106
107
108
109
110
111
112
113
114
# File 'lib/labclient/klass.rb', line 105

def initialize(hash = nil, response = nil, client = nil)
  @client = client
  @response = response

  @table = {}
  hash&.each_pair do |k, v|
    k = k.to_sym
    @table[k] = v
  end
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



7
8
9
# File 'lib/labclient/klass.rb', line 7

def client
  @client
end

#responseObject (readonly)

Returns the value of attribute response.



7
8
9
# File 'lib/labclient/klass.rb', line 7

def response
  @response
end

Class Method Details

.date_time_attrs(list) ⇒ Object

Define a list of DateTime Attributes



128
129
130
131
132
133
134
# File 'lib/labclient/klass.rb', line 128

def self.date_time_attrs(list)
  list.each do |kind|
    define_method(kind) do
      DateTime.parse @table[kind] if has? kind
    end
  end
end

.user_attrs(list) ⇒ Object

Define a list of LabClient::User Attributes



137
138
139
140
141
142
143
# File 'lib/labclient/klass.rb', line 137

def self.user_attrs(list)
  list.each do |kind|
    define_method(kind) do
      User.new(@table[kind], response, client) if has? kind
    end
  end
end

Instance Method Details

#api_methodsObject

Documented API Methods



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/labclient/klass.rb', line 44

def api_methods
  docs = LabClient::Docs.docs.dig(group_name, 'Reference')

  unless docs
    puts 'No Available Help'
    return false
  end

  LabClient::Docs.docs.dig(group_name, 'Reference').map do |doc|
    doc[:options].map do |opt|
      opt[:name]
    end
  end.flatten.sort
end

#collect_project_id(position = 1) ⇒ Object Also known as: collect_group_id, collect_user_id

TODO: Combine all of these?



64
65
66
67
68
69
70
71
# File 'lib/labclient/klass.rb', line 64

def collect_project_id(position = 1)
  # Check if Path / Pagination will be blank
  if response.path.nil?
    response.request.base_url.split(@client.base_url, 2)[position]
  else
    response.path.split('/')[position]
  end
end

#collect_release_id(position = 3) ⇒ Object



76
77
78
# File 'lib/labclient/klass.rb', line 76

def collect_release_id(position = 3)
  response.path.split('/')[position]
end

#collect_repository_id(position = 4) ⇒ Object



80
81
82
# File 'lib/labclient/klass.rb', line 80

def collect_repository_id(position = 4)
  response.path.split('/')[position]
end

#format_time?(time) ⇒ Boolean

Formatting Time Helper

Returns:

  • (Boolean)


123
124
125
# File 'lib/labclient/klass.rb', line 123

def format_time?(time)
  time.respond_to?(:to_time)
end

#group_nameObject

Category and Primary Key for docs



85
86
87
# File 'lib/labclient/klass.rb', line 85

def group_name
  self.class.instance_variable_get('@group_name') || klass.pluralize
end

#help(help_filter = nil) ⇒ Object

API Methods here have to be explicitly documented / custom helpers Assume no methods by default rubocop:disable Metrics/AbcSize



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/labclient/klass.rb', line 19

def help(help_filter = nil)
  docs = LabClient::Docs.docs.dig(group_name, 'Reference')
  unless docs
    puts 'No Available Help'
    return false
  end

  puts klass
  docs.each do |doc|
    next unless doc[:options]

    doc[:options].each do |opt|
      next if help_filter && !(opt[:name] + opt[:text]).include?(help_filter.to_s)

      puts "  #{opt[:name]}"
      puts "    #{opt[:text]}\n"
    end
  end

  # Ignore Output
  nil
end

#klassObject

Helper to get docs



95
96
97
# File 'lib/labclient/klass.rb', line 95

def klass
  self.class.name.split('::', 2).last.split(/(?=[A-Z])/).join(' ')
end

#success?Boolean

Forward response success

Returns:

  • (Boolean)


118
119
120
# File 'lib/labclient/klass.rb', line 118

def success?
  @response.success?
end

#to_json(*_args) ⇒ Object

Prevent stack level errors, but turning into has first



90
91
92
# File 'lib/labclient/klass.rb', line 90

def to_json(*_args)
  to_h.to_json
end

#update_self(obj) ⇒ Object



99
100
101
102
# File 'lib/labclient/klass.rb', line 99

def update_self(obj)
  @table = obj.table
  self
end

#valid_group_project_levelsObject



59
60
61
# File 'lib/labclient/klass.rb', line 59

def valid_group_project_levels
  %i[guest reporter developer maintainer owner]
end

#verboseObject

TODO: Awesome Print / Amazing Print Conflicts?



12
13
14
# File 'lib/labclient/klass.rb', line 12

def verbose
  ap @table, ruby19_syntax: true
end