Class: Megam::Request

Inherits:
ServerAPI show all
Defined in:
lib/megam/core/request.rb

Instance Attribute Summary

Attributes inherited from ServerAPI

#api_key, #email, #host

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ServerAPI

#megam_rest

Constructor Details

#initialize(email = nil, api_key = nil, host = nil) ⇒ Request

Returns a new instance of Request.



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

def initialize(email=nil, api_key=nil, host=nil)
  @id = nil
  @cat_id = nil
  @name = nil
  @cattype = nil
  @command =nil
  @action = nil
  @category = nil
  @some_msg = {}
  @created_at = nil
  super(email, api_key, host)
end

Class Method Details

.create(params) ⇒ Object



186
187
188
189
# File 'lib/megam/core/request.rb', line 186

def self.create(params)
  acct = from_hash(params, params["email"] || params[:email], params["api_key"] || params[:api_key], params["host"] || params[:host])
  acct.create
end

.from_hash(o, tmp_email = nil, tmp_api_key = nil, tmp_host = nil) ⇒ Object



167
168
169
170
171
# File 'lib/megam/core/request.rb', line 167

def self.from_hash(o,tmp_email=nil, tmp_api_key=nil, tmp_host=nil)
  node = self.new(tmp_email, tmp_api_key, tmp_host)
  node.from_hash(o)
  node
end

.json_create(o) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/megam/core/request.rb', line 149

def self.json_create(o)
  node = new
  node.id(o["id"]) if o.has_key?("id")
  node.cat_id(o["cat_id"]) if o.has_key?("cat_id")
  node.name(o["name"]) if o.has_key?("name")
  node.cattype(o["cattype"]) if o.has_key?("cattype")
  node.action(o["action"]) if o.has_key?("action")
  node.category(o["category"]) if o.has_key?("category")
  node.command(o["command"]) if o.has_key?("command")
  node.created_at(o["created_at"]) if o.has_key?("created_at")
  #success or error
  node.some_msg[:code] = o["code"] if o.has_key?("code")
  node.some_msg[:msg_type] = o["msg_type"] if o.has_key?("msg_type")
  node.some_msg[:msg]= o["msg"] if o.has_key?("msg")
  node.some_msg[:links] = o["links"] if o.has_key?("links")
  node
end

.list(n_name, tmp_email = nil, tmp_api_key = nil, tmp_host = nil) ⇒ Object



203
204
205
206
# File 'lib/megam/core/request.rb', line 203

def self.list(n_name,tmp_email=nil, tmp_api_key=nil, tmp_host=nil)
  prede = self.new(tmp_email,tmp_api_key, tmp_host)
  prede.megam_rest.get_request(n_name)
end

.show(params) ⇒ Object



198
199
200
201
# File 'lib/megam/core/request.rb', line 198

def self.show(params)
  prede = self.new(params["email"] || params[:email], params["api_key"] || params[:api_key], params["host"] || params[:host])
  prede.megam_rest.get_requests
end

Instance Method Details

#action(arg = nil) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/megam/core/request.rb', line 67

def action(arg=nil)
  if arg != nil
    @action = arg
  else
  @action
  end
end

#cat_id(arg = nil) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/megam/core/request.rb', line 44

def cat_id(arg=nil)
  if arg != nil
    @cat_id = arg
  else
  @cat_id
  end
end

#category(arg = nil) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/megam/core/request.rb', line 76

def category(arg=nil)
  if arg != nil
    @category = arg
  else
  @category
  end
end

#cattype(arg = nil) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/megam/core/request.rb', line 59

def cattype(arg=nil)
  if arg != nil
    @cattype = arg
  else
  @cattype
  end
end

#command(arg = nil) ⇒ Object



93
94
95
96
97
98
99
# File 'lib/megam/core/request.rb', line 93

def command(arg=nil)
  if arg != nil
    @command = arg
  else
  @command
  end
end

#createObject

Create the node via the REST API



192
193
194
195
# File 'lib/megam/core/request.rb', line 192

def create
puts "entering megam_api"
  megam_rest.post_request(to_hash)
end

#created_at(arg = nil) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/megam/core/request.rb', line 85

def created_at(arg=nil)
  if arg != nil
    @created_at = arg
  else
  @created_at
  end
end

#error?Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/megam/core/request.rb', line 109

def error?
  crocked  = true if (some_msg.has_key?(:msg_type) && some_msg[:msg_type] == "error")
end

#for_jsonObject



134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/megam/core/request.rb', line 134

def for_json
  result = {
    "id" => id,
    "cat_id" => cat_id,
    "name" => name,
    "cattype" => cattype,
    "action" => action,
    "category" => category,
    "command" => command,
    "created_at" => created_at
  }
  result
end

#from_hash(o) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
# File 'lib/megam/core/request.rb', line 173

def from_hash(o)
  @id = o[:id] if o.has_key?(:id)
  @cat_id  = o[:cat_id] if o.has_key?(:cat_id)
  @name = o[:name] if o.has_key?(:name)
  @cattype = o[:cattype] if o.has_key?(:cattype)
  @action = o[:action] if o.has_key?(:action)
  @category = o[:category] if o.has_key?(:category)
  @command  = o[:command] if o.has_key?(:command)
  @created_at = o[:created_at] if o.has_key?(:created_at)
  self
end

#id(arg = nil) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/megam/core/request.rb', line 36

def id(arg=nil)
  if arg != nil
    @id = arg
  else
  @id
  end
end

#name(arg = nil) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/megam/core/request.rb', line 52

def name(arg=nil)
  if arg != nil
    @name = arg
  else
  @name
  end
end

#requestObject



31
32
33
# File 'lib/megam/core/request.rb', line 31

def request
  self
end

#some_msg(arg = nil) ⇒ Object



101
102
103
104
105
106
107
# File 'lib/megam/core/request.rb', line 101

def some_msg(arg=nil)
  if arg != nil
    @some_msg = arg
  else
  @some_msg
  end
end

#to_hashObject

Transform the ruby obj -> to a Hash



114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/megam/core/request.rb', line 114

def to_hash
  index_hash = Hash.new
  index_hash["json_claz"] = self.class.name
  index_hash["id"] = id
  index_hash["cat_id"] = cat_id
  index_hash["name"] = name
  index_hash["cattype"] = cattype
  index_hash["action"] = action
  index_hash["category"] = category
  index_hash["command"] = command
  index_hash["created_at"] = created_at
  index_hash
end

#to_json(*a) ⇒ Object

Serialize this object as a hash: called from JsonCompat. Verify if this called from JsonCompat during testing.



130
131
132
# File 'lib/megam/core/request.rb', line 130

def to_json(*a)
  for_json.to_json(*a)
end

#to_sObject



208
209
210
211
# File 'lib/megam/core/request.rb', line 208

def to_s
  Megam::Stuff.styled_hash(to_hash)
#"---> Megam::Account:[error=#{error?}]\n"+
end