Class: Megam::Account

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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ServerAPI

#megam_rest

Constructor Details

#initialize(email = nil, api_key = nil) ⇒ Account

Returns a new instance of Account.



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

def initialize(email=nil, api_key=nil)
  @id = nil
  @email = nil
  @api_key = nil
  @authority = nil
  @created_at = nil
  @some_msg = {}
  super(email, api_key)
end

Class Method Details

.create(o) ⇒ Object



145
146
147
148
# File 'lib/megam/core/account.rb', line 145

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

.from_hash(o) ⇒ Object



130
131
132
133
134
# File 'lib/megam/core/account.rb', line 130

def self.from_hash(o)
  acct = self.new(o[:email], o[:api_key])
  acct.from_hash(o)
  acct
end

.json_create(o) ⇒ Object

Create a Megam::Account from JSON (used by the backgroud job workers)



116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/megam/core/account.rb', line 116

def self.json_create(o)
  acct = new
  acct.id(o["id"]) if o.has_key?("id")
  acct.email(o["email"]) if o.has_key?("email")
  acct.api_key(o["api_key"]) if o.has_key?("api_key")
  acct.authority(o["authority"]) if o.has_key?("authority")
  acct.created_at(o["created_at"]) if o.has_key?("created_at")
  acct.some_msg[:code] = o["code"] if o.has_key?("code")
  acct.some_msg[:msg_type] = o["msg_type"] if o.has_key?("msg_type")
  acct.some_msg[:msg]= o["msg"] if o.has_key?("msg")
  acct.some_msg[:links] = o["links"] if o.has_key?("links")
  acct
end

.show(email, api_key = nil) ⇒ Object

Load a account by email_p



151
152
153
154
# File 'lib/megam/core/account.rb', line 151

def self.show(email,api_key=nil)
  acct = self.new(email, api_key)
  acct.megam_rest.get_accounts(email)
end

Instance Method Details

#accountObject

used by resque workers and any other background job



29
30
31
# File 'lib/megam/core/account.rb', line 29

def 
  self
end

#api_key(arg = nil) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/megam/core/account.rb', line 49

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

#authority(arg = nil) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/megam/core/account.rb', line 57

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

#createObject

Create the node via the REST API



157
158
159
# File 'lib/megam/core/account.rb', line 157

def create
  megam_rest.post_accounts(to_hash)
end

#created_at(arg = nil) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/megam/core/account.rb', line 65

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

#email(arg = nil) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/megam/core/account.rb', line 41

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

#error?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/megam/core/account.rb', line 81

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

#for_jsonObject



104
105
106
107
108
109
110
111
112
113
# File 'lib/megam/core/account.rb', line 104

def for_json
  result = {
    "id" => id,
    "email" => email,
    "api_key" => api_key,
    "authority" => authority,
    "created_at" => created_at
  }
  result
end

#from_hash(o) ⇒ Object



136
137
138
139
140
141
142
143
# File 'lib/megam/core/account.rb', line 136

def from_hash(o)
  @id        = o[:id] if o.has_key?(:id)
  @email     = o[:email] if o.has_key?(:email)
  @api_key   = o[:api_key] if o.has_key?(:api_key)
  @authority = o[:authority] if o.has_key?(:authority)
  @created_at = o[:created_at] if o.has_key?(:created_at)
  self
end

#id(arg = nil) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/megam/core/account.rb', line 33

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

#some_msg(arg = nil) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/megam/core/account.rb', line 73

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



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/megam/core/account.rb', line 86

def to_hash
  index_hash = Hash.new
  index_hash["json_claz"] = self.class.name
  index_hash["id"] = id
  index_hash["email"] = email
  index_hash["api_key"] = api_key
  index_hash["authority"] = authority
  index_hash["created_at"] = created_at
  index_hash["some_msg"] = some_msg
  index_hash
end

#to_json(*a) ⇒ Object

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



100
101
102
# File 'lib/megam/core/account.rb', line 100

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

#to_sObject



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

def to_s
  Megam::Stuff.styled_hash(to_hash)
end