Class: LabClient::Common

Inherits:
Object
  • Object
show all
Extended by:
Docs
Includes:
AccessLevel
Defined in:
lib/labclient/common.rb

Overview

Shared Methods

Constant Summary

Constants included from AccessLevel

AccessLevel::HUMAN_ACCESS_LEVELS, AccessLevel::MACHINE_ACCESS_LEVELS

Instance Attribute 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 AccessLevel

#human_access_level, #human_protected_access_level, #machine_access_level, #machine_protected_access_level

Constructor Details

#initialize(client) ⇒ Common

Returns a new instance of Common.



10
11
12
# File 'lib/labclient/common.rb', line 10

def initialize(client)
  @client = client
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



8
9
10
# File 'lib/labclient/common.rb', line 8

def client
  @client
end

Instance Method Details

#api_methodsObject



14
15
16
# File 'lib/labclient/common.rb', line 14

def api_methods
  public_methods(false).sort
end

#api_methods_help(help_filter = nil) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/labclient/common.rb', line 31

def api_methods_help(help_filter = nil)
  puts group_name
  puts '  Available Methods'

  shown_subclasses = if help_filter
                       api_methods.grep(/#{help_filter}/)
                     else
                       api_methods
                     end

  puts "      #{shown_subclasses.join(' ')}\n"
end

#format_id(obj_id) ⇒ Object

Query Helpers

URL Encoding Object ID Transformation “1” == 1, 1 => 1, “1” => 1 nil => nil ‘group/name’ => group%F2name LabClient::Class => id



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/labclient/common.rb', line 68

def format_id(obj_id)
  # Return if Empty
  return nil if obj_id.nil?

  # Already a Integer
  return obj_id if obj_id.class == Integer

  # If LabClient Object, send ID
  return obj_id.id if obj_id.class.module_parent_name == 'LabClient'

  # Valid Integer in String
  return obj_id if /\A\d+\z/.match(obj_id)

  CGI.escape obj_id.to_s
end

#format_query_id(key, query) ⇒ Object



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

def format_query_id(key, query)
  query[key] = format_id(query[key]) if query.key? key
  query[key.to_s] = format_id(query[key.to_s]) if query.key? key.to_s
end

#format_query_ids(key, query) ⇒ Object



89
90
91
92
93
# File 'lib/labclient/common.rb', line 89

def format_query_ids(key, query)
  query[key] = query[key].map { |x| format_id(x) } if query.key? key

  query[key.to_s] = query[key.to_s].map { |x| format_id(x) } if query.key? key.to_s
end

#format_time?(time) ⇒ Boolean

Formatting Time Helper

Returns:

  • (Boolean)


96
97
98
# File 'lib/labclient/common.rb', line 96

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

#group_nameObject

Category and Primary Key for docs



50
51
52
# File 'lib/labclient/common.rb', line 50

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

#help(help_filter = nil) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/labclient/common.rb', line 18

def help(help_filter = nil)
  api_methods_help(help_filter)

  LabClient::Docs.docs[group_name]&.each do |key, group|
    next if help_filter && !key.downcase.include?(help_filter.to_s)

    puts "\n=====[ #{key} ]====="
    group.select { |x| x[:example] }.each { |x| puts "#{x[:example]}\n" }
  end

  nil
end

#inspectObject



54
55
56
57
# File 'lib/labclient/common.rb', line 54

def inspect
  api_methods_help
  "#<LabClient::Client url: \"#{client.settings[:url]}\">"
end

#klassObject

Helper to get docs



45
46
47
# File 'lib/labclient/common.rb', line 45

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

#protected_query_access_level(query, key = :push_access_level) ⇒ Object

TODO: See if these are even needed Protected Branches Convert Symbol to Integer Numbers 0 => No access 30 => Developer access 40 => Maintainer access 60 => Admin access



120
121
122
# File 'lib/labclient/common.rb', line 120

def protected_query_access_level(query, key = :push_access_level)
  query[key] = machine_protected_access_level query[key] if query.key?(key) && query[key].class == Symbol
end

#query_access_level(query, key = :group_access) ⇒ Object

Convert Symbol to Integer Numbers :developer => 30 :owner => 50



109
110
111
# File 'lib/labclient/common.rb', line 109

def query_access_level(query, key = :group_access)
  query[key] = machine_access_level query[key] if query.key?(key) && query[key].class == Symbol
end

#query_format_time(query, key) ⇒ Object



100
101
102
103
104
# File 'lib/labclient/common.rb', line 100

def query_format_time(query, key)
  return false unless query.key?(key) && format_time?(query[key])

  query[key] = query[key].to_time.utc.iso8601
end