Class: Megam::Request

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRequest

Returns a new instance of Request.



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

def initialize
  @id = nil
  @node_id = nil
  @node_name = nil
  @req_type = nil
  @command =nil
  @some_msg = {}
  @created_at = nil
end

Class Method Details

.create(o) ⇒ Object



160
161
162
163
# File 'lib/megam/core/request.rb', line 160

def self.create(o)
  acct = from_hash(o)
  acct.create
end

.from_hash(o) ⇒ Object



143
144
145
146
147
# File 'lib/megam/core/request.rb', line 143

def self.from_hash(o)
  node = self.new()
  node.from_hash(o)
  node
end

.json_create(o) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/megam/core/request.rb', line 127

def self.json_create(o)
  node = new
  node.id(o["id"]) if o.has_key?("id")
  node.node_id(o["node_id"]) if o.has_key?("node_id")
  node.node_name(o["node_name"]) if o.has_key?("node_name")
  node.req_type(o["req_type"]) if o.has_key?("req_type")
  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) ⇒ Object



176
177
178
179
# File 'lib/megam/core/request.rb', line 176

def self.list(n_name)
  prede = self.new()
  prede.megam_rest.get_request(n_name)
end

.showObject



171
172
173
174
# File 'lib/megam/core/request.rb', line 171

def self.show
  prede = self.new()
  prede.megam_rest.get_requests
end

Instance Method Details

#command(arg = nil) ⇒ Object



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

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

#createObject

Create the node via the REST API



166
167
168
# File 'lib/megam/core/request.rb', line 166

def create
  megam_rest.post_request(to_hash)
end

#created_at(arg = nil) ⇒ Object



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

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

#error?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/megam/core/request.rb', line 91

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

#for_jsonObject



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

def for_json
  result = {
    "id" => id,
    "node_id" => node_id,
    "node_name" => node_name,
    "req_type" => req_type,
    "command" => command,
    "created_at" => created_at
  }
  result
end

#from_hash(o) ⇒ Object



149
150
151
152
153
154
155
156
157
# File 'lib/megam/core/request.rb', line 149

def from_hash(o)
  @id = o["id"] if o.has_key?("id")
  @node_id  = o["node_id"] if o.has_key?("node_id")
  @node_name       = o["node_name"] if o.has_key?("node_name")
  @req_type       = o["req_type"] if o.has_key?("req_type")
  @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



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

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

#megam_restObject



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

def megam_rest
  options = { :email => Megam::Config[:email], :api_key => Megam::Config[:api_key]}
  Megam::API.new(options)
end

#node_id(arg = nil) ⇒ Object



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

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

#node_name(arg = nil) ⇒ Object



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

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

#req_type(arg = nil) ⇒ Object



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

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

#requestObject



28
29
30
# File 'lib/megam/core/request.rb', line 28

def request
  self
end

#some_msg(arg = nil) ⇒ Object



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

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



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/megam/core/request.rb', line 96

def to_hash
  index_hash = Hash.new
  index_hash["json_claz"] = self.class.name
  index_hash["id"] = id
  index_hash["node_id"] = node_id
  index_hash["node_name"] = node_name
  index_hash["req_type"] = req_type
  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.



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

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

#to_sObject



181
182
183
184
# File 'lib/megam/core/request.rb', line 181

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