Module: OSCRuby::ClassFactoryModule

Included in:
ServiceClass
Defined in:
lib/osc_ruby/class_factory_module.rb

Class Method Summary collapse

Class Method Details

.all(client, obj_query, return_json, class_name) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/osc_ruby/class_factory_module.rb', line 69

def all(client,obj_query,return_json,class_name)

  ValidationsModule::check_client(client)
    
  resource = URI.escape("queryResults/?query=select * from #{obj_query}")

  object_json = QueryModule::find(client,resource)

  ClassFactoryModule::instantiate_multiple_objects(return_json, object_json, class_name)

end

.create(client, obj, resource_uri, return_json) ⇒ Object



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

def create(client,obj,resource_uri,return_json)

  ValidationsModule::check_client(client)

    final_json = obj.class.check_self(obj)

    resource = URI.escape(resource_uri)

    response = QueryModule::create(client,resource,final_json)

    response_body = JSON.parse(response.body)

    if response.code.to_i == 201 && return_json == false

    obj.set_attributes(response_body)

    elsif return_json == true

      response.body

    end

end

.destroy(client, obj, resource_uri, return_json) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/osc_ruby/class_factory_module.rb', line 121

def destroy(client,obj,resource_uri,return_json)

  ValidationsModule::check_client(client)

  ValidationsModule::check_object_for_id(obj,obj.class)

  resource = URI.escape("/#{resource_uri}/#{obj.id}")

  response = QueryModule::destroy(client,resource)

  if response.code.to_i == 200 && return_json == false

    nil

  elsif return_json == true

    response.body

  end

end

.find(client, id, obj_query, return_json, class_name) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/osc_ruby/class_factory_module.rb', line 43

def find(client,id,obj_query,return_json,class_name)

    ValidationsModule::check_client(client)

    ValidationsModule::check_for_id(id)

    url = "queryResults/?query=select * from #{obj_query} where id = #{id}"
      
    resource = URI.escape(url)

    class_json = QueryModule::find(client,resource)

  if return_json == true

    class_json

  else

    class_json_final = JSON.parse(class_json)

    ClassFactoryModule::new_from_fetch(class_json_final[0],class_name)

  end

end

.instantiate_multiple_objects(return_json, object_json, class_name) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/osc_ruby/class_factory_module.rb', line 143

def instantiate_multiple_objects(return_json, object_json, class_name)

  if return_json == true

    object_json

  else

  object_json_final = JSON.parse(object_json)

  object_json_final.map { |attributes| ClassFactoryModule::new_from_fetch(attributes,class_name) }

 end

end

.new_from_fetch(attributes, class_name) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/osc_ruby/class_factory_module.rb', line 11

def new_from_fetch(attributes,class_name)

    ValidationsModule.check_attributes(attributes)

    class_name.new(attributes)

end

.update(client, obj, resource_uri, return_json) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/osc_ruby/class_factory_module.rb', line 95

def update(client,obj,resource_uri,return_json)

        ValidationsModule::check_client(client)

  ValidationsModule::check_object_for_id(obj,obj.class)

  final_json = obj.class.check_self(obj,true)

  resource = URI.escape("#{resource_uri}/#{obj.id}")

  response = QueryModule::update(client,resource,final_json)

  if response.code.to_i == 200 && return_json == false

    updated_obj = obj.class.find(client,obj.id)

    obj.update_attributes(updated_obj)

  elsif return_json == true

    response.body

  end

end

.where(client, query, object_in_query, return_json, class_name) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/osc_ruby/class_factory_module.rb', line 81

def where(client,query,object_in_query,return_json,class_name)

  ValidationsModule::check_client(client)

  ValidationsModule::check_query(query,'where')

  @query = URI.escape("queryResults/?query=select * from #{object_in_query} where #{query}")

    object_json = QueryModule::find(client,@query)

    ClassFactoryModule::instantiate_multiple_objects(return_json, object_json, class_name)

end