Module: JSONAPI::Utility

Defined in:
lib/easy/jsonapi/utility.rb

Overview

A collection of resued logic throughout the gem.

Class Method Summary collapse

Class Method Details

.all_hash_path?(hash, args) ⇒ Boolean

The hash method #dig throws an error if an array appears in the path specified,

this method returns false for such a senario.

Parameters:

  • hash (Hash)

    The hash being inspected

  • args (Array<Symbol | String>)

    The hash keys making up the path

Returns:

  • (Boolean)


151
152
153
154
155
# File 'lib/easy/jsonapi/utility.rb', line 151

def all_hash_path?(hash, args)
  return false if (args.size.positive? && !hash.is_a?(Hash)) || hash.nil?
  return true if args.size.zero? && !hash.nil?
  all_hash_path?(hash[args.first], args[1..-1])
end

.array_to_s(obj_arr) ⇒ Object

Returns the proper to_s for members regardless of

whether they are stored as an array or member object


101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/easy/jsonapi/utility.rb', line 101

def array_to_s(obj_arr)
  return obj_arr.to_s unless obj_arr.is_a? Array
  to_return = '['
  first = true
  obj_arr.each do |obj|
    if first
      to_return += obj.to_s
      first = false
    else
      to_return += ", #{obj}"
    end
  end
  to_return += ']'
end

.integer?(str) ⇒ Boolean

Check if an input is an integer

Parameters:

  • str (String | Integer)

Returns:

  • (Boolean)


130
131
132
133
134
135
# File 'lib/easy/jsonapi/utility.rb', line 130

def integer?(str)
  # integers cannot be valid symbols, so assume string or integer input
  return true if str.is_a?(Integer) ||
                 str =~ /\A\d+\z/ 
  false
end

.member_to_s(str_name, member, first_member: false) ⇒ Object

Helper for #to_s where collections are hashes and members should not

be included if they are nil. It also accounts for arrays.

Parameters:

  • str_name (String | Symbol)

    The name of hash member

  • member (Any)

    The value of the hash member

  • first_member (TrueClass | FalseClass) (defaults to: false)

    Whether or not this is the first member in the hash.



89
90
91
92
93
94
95
96
97
# File 'lib/easy/jsonapi/utility.rb', line 89

def member_to_s(str_name, member, first_member: false)
  return '' if member.nil?
  member = "\"#{member}\"" if member.is_a? String
  if first_member
    "\"#{str_name}\": #{array_to_s(member)}"
  else
    ", \"#{str_name}\": #{array_to_s(member)}"
  end
end

.path_to_res_type(path) ⇒ String

Get resource type from path

Parameters:

  • path (String)

    The url path

Returns:

  • (String)

    The resource type



119
120
121
122
123
124
125
126
# File 'lib/easy/jsonapi/utility.rb', line 119

def path_to_res_type(path)
  path_arr = path.split('/')
  if integer?(path_arr[-1]) || uuid?(path_arr[-1])
    path_arr[-2]
  else
    path_arr[-1]
  end
end

.to_h_collection(collection) ⇒ Object

To hash method for a JSONAPI collection (like Attributes)

Parameters:

  • collection (Inumerable)

    The collection to hashify



12
13
14
15
16
17
18
19
# File 'lib/easy/jsonapi/utility.rb', line 12

def to_h_collection(collection)
  to_return = {}
  collection.each do |mem|
    h_val = JSONAPI::Utility.to_h_value(mem)
    to_return[mem.name.to_sym] = h_val
  end
  to_return
end

.to_h_member(hash_to_add_to, obj_member, obj_member_name) ⇒ Hash

Helper for #to_h for classes that arent collection and have a set number

of instance variables to hashify

Parameters:

  • hash_to_add_to (Hash)

    The hash to return

  • obj_member (Any)

    The instance variable to hashify

  • obj_member_name (Symbol)

    The hash key to store the object under

Returns:

  • (Hash)

    The hash for a specific instance variable



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/easy/jsonapi/utility.rb', line 51

def to_h_member(hash_to_add_to, obj_member, obj_member_name)
  return if obj_member.nil?
  case obj_member
  when String
    hash_to_add_to[obj_member_name] = obj_member
  when Array
    hash_arr = obj_member.map(&:to_h)
    hash_to_add_to[obj_member_name] = hash_arr
  else
    hash_to_add_to[obj_member_name] = obj_member.to_h
  end
end

.to_h_value(val) ⇒ Object

Helper method for #to_h_collection

Parameters:

  • val (Any)

    The value to call to hash on.

Returns:

  • a hash value member



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/easy/jsonapi/utility.rb', line 24

def to_h_value(val)
  case val
  when String
    val
  when JSONAPI::Collection
    obj_hash = {}
    val.each { |i| obj_hash[i.name.to_sym] = to_h_value(i) }
    obj_hash
  else
    v = val.value
    case v
    when String
      to_h_value(v)
    when Array
      v.map { |e| to_h_value(e) }
    else
      v.to_h
    end
  end
end

.to_string_collection(collection, delimiter: ', ', pre_string: '', post_string: '') ⇒ Object

Helper method for #to_s that stringifys a collection

Parameters:

  • collection (Inumerable)

    An array type of collection

  • delimiter (String) (defaults to: ', ')

    The delimieter to separate each item string

  • pre_string (String) (defaults to: '')

    The string to precede the collection string

  • post_string (String) (defaults to: '')

    The string to follow the collection



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/easy/jsonapi/utility.rb', line 69

def to_string_collection(collection, delimiter: ', ', pre_string: '', post_string: '')
  to_return = pre_string
  first = true
  collection.each do |item|
    if first
      to_return += item.to_s
      first = false
    else
      to_return += "#{delimiter}#{item}"
    end
  end
  to_return += post_string
end

.uuid?(uuid) ⇒ TrueClass | FalseClass

Use regix to test whether the input is a valid gen 4 uuid.

Parameters:

  • uuid (String)

Returns:

  • (TrueClass | FalseClass)


140
141
142
143
144
145
# File 'lib/easy/jsonapi/utility.rb', line 140

def uuid?(uuid)
  # uuids cannot be valid symbols, so assume string
  return true if uuid =~ /\A[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}\z/i ||
                 uuid =~ /\A[\dA-F]{8}-[\dA-F]{4}-[\dA-F]{4}-[\dA-F]{4}-[\dA-F]{12}\z/i
  false
end