Module: OSCRuby::ValidationsModule

Included in:
AnalyticsReportResults, QueryModule, QueryResults, QueryResultsSet
Defined in:
lib/osc_ruby/modules/validations_module.rb

Class Method Summary collapse

Class Method Details

.attr_hash_exists_and_is_type_of(obj, key, val, class_of_value) ⇒ Object



103
104
105
106
107
# File 'lib/osc_ruby/modules/validations_module.rb', line 103

def attr_hash_exists_and_is_type_of(obj,key,val,class_of_value)

  return obj[0][key][val].nil? || obj[0][key][val].class != class_of_value

end

.check_attributes(attributes) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/osc_ruby/modules/validations_module.rb', line 61

def check_attributes(attributes)

  if attributes.class != Hash

    puts attributes
    
    raise ArgumentError, "Attributes must be a hash; please use the appropriate data structure"

  end

end

.check_attributes_request(attributes_request, class_name) ⇒ Object



83
84
85
86
87
88
89
90
91
# File 'lib/osc_ruby/modules/validations_module.rb', line 83

def check_attributes_request(attributes_request,class_name)

        if attributes_request.empty?
          
          raise ArgumentError, "The attributes you are requesting for the #{class_name} object must be specified when using the 'select' method"

        end

end

.check_client(client) ⇒ Object



93
94
95
96
97
98
99
100
101
# File 'lib/osc_ruby/modules/validations_module.rb', line 93

def check_client(client)

  if client.class != OSCRuby::Client || client.nil?

    raise ArgumentError, "Client must have some configuration set; please create an instance of OSCRuby::Client with configuration settings"

  end

end

.check_for_id(id) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/osc_ruby/modules/validations_module.rb', line 37

def check_for_id(id)

  if id.nil? == true

      raise ArgumentError, 'ID cannot be nil'

  elsif id.class != Fixnum

      raise ArgumentError, 'ID must be an integer'

  end

end

.check_for_names(obj_attrs, class_name) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
# File 'lib/osc_ruby/modules/validations_module.rb', line 109

def check_for_names(obj_attrs,class_name)

  if obj_attrs[0]['names'].count == 0 || obj_attrs[0]['names'][0]['labelText'].nil? || obj_attrs[0]['names'][0]['language'].nil?
    
    raise ArgumentError, "#{class_name} should at least have one name set"
  
  end

  obj_attrs

end

.check_for_parents(obj_attrs) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
# File 'lib/osc_ruby/modules/validations_module.rb', line 121

def check_for_parents(obj_attrs)

  if !obj_attrs[0]['parent'].nil? && obj_attrs[0]['parent'].is_a?(Hash) && !obj_attrs[0]['parent'].key?('id') && !obj_attrs[0]['parent'].key?('lookupName')
  
    obj_attrs[0].delete('parent')
  
  end

  obj_attrs

end

.check_interfaces(empty_arr) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/osc_ruby/modules/validations_module.rb', line 133

def check_interfaces(empty_arr)

  if empty_arr[0]['adminVisibleInterfaces'].empty?
    
    empty_arr[0].delete('adminVisibleInterfaces')
  
  end

  if empty_arr[0]['endUserVisibleInterfaces'].empty?
    
    empty_arr[0].delete('endUserVisibleInterfaces')
  
  end

  empty_arr

end

.check_obj_for_errors(obj_to_check) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/osc_ruby/modules/validations_module.rb', line 151

def check_obj_for_errors(obj_to_check)

  json_obj = JSON.parse(obj_to_check.body)

  if !json_obj.nil? && json_obj['items'][0]['rows'].count == 0

    puts obj_to_check.body

    raise ArgumentError, 'There were no objects matching your query; please try again.'

  end

end

.check_object_for_id(obj, class_name) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/osc_ruby/modules/validations_module.rb', line 51

def check_object_for_id(obj,class_name)

  if obj.id.nil?

    raise ArgumentError, "#{class_name} must have a valid ID set"

  end

end

.check_query(query, method_name = "where") ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'lib/osc_ruby/modules/validations_module.rb', line 73

def check_query(query,method_name = "where")

        if query.empty?
          
          raise ArgumentError, "A query must be specified when using the '#{method_name}' method"

        end

end

.extract_attributes(obj) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/osc_ruby/modules/validations_module.rb', line 10

def extract_attributes(obj)

  empty_arr = [{}]

  obj_vars = obj.instance_variables

  obj_vars.each do |var| 

    # Make sure to get rid of the empty ID

    # otherwise everything gets all screwed up


    if var.to_s != '@id'

      obj_attr = var.to_s.delete("@")

      obj_attr_val = obj.instance_variable_get(var)

      empty_arr[0][obj_attr] = obj_attr_val

    end

  end

  empty_arr

end