Class: Megam::Organizations

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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



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

def initialize(email=nil, api_key=nil, host=nil)
  @id = nil
  @name = nil
  @accounts_id = nil
  @created_at = nil
  super(email, api_key, host)
end

Class Method Details

.create(o) ⇒ Object



112
113
114
115
# File 'lib/megam/core/organizations.rb', line 112

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

.from_hash(o) ⇒ Object



98
99
100
101
102
# File 'lib/megam/core/organizations.rb', line 98

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

.json_create(o) ⇒ Object

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



89
90
91
92
93
94
95
96
# File 'lib/megam/core/organizations.rb', line 89

def self.json_create(o)
  org = new
  org.id(o["id"]) if o.has_key?("id")
  org.name(o["name"]) if o.has_key?("name")
  org.accounts_id(o["accounts_id"]) if o.has_key?("accounts_id")
  org.created_at(o["created_at"]) if o.has_key?("created_at")
  org
end

.list(o) ⇒ Object



123
124
125
126
# File 'lib/megam/core/organizations.rb', line 123

def self.list(o)
  org = from_hash(o)
  org.megam_rest.get_organizations
end

.show(o) ⇒ Object

Load a organization by email_p



118
119
120
121
# File 'lib/megam/core/organizations.rb', line 118

def self.show(o)
  org = from_hash(o)
  org.megam_rest.get_organizations(email)
end

Instance Method Details

#accounts_id(arg = nil) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/megam/core/organizations.rb', line 39

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

#createObject



128
129
130
# File 'lib/megam/core/organizations.rb', line 128

def create
  megam_rest.post_organizations(to_hash)
end

#created_at(arg = nil) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/megam/core/organizations.rb', line 55

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

#for_jsonObject



78
79
80
81
82
83
84
85
86
# File 'lib/megam/core/organizations.rb', line 78

def for_json
  result = {
    "id" => id,
    "name" => name,
    "accounts_id" => accounts_id,
    "created_at" => created_at
  }
  result
end

#from_hash(o) ⇒ Object



104
105
106
107
108
109
110
# File 'lib/megam/core/organizations.rb', line 104

def from_hash(o)
  @id        = o[:id] if o.has_key?(:id)
  @name     = o[:name] if o.has_key?(:name)
  @accounts_id = o[:accounts_id] if o.has_key?(:accounts_id)
  @created_at = o[:created_at] if o.has_key?(:created_at)
  self
end

#id(arg = nil) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/megam/core/organizations.rb', line 31

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

#name(arg = nil) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/megam/core/organizations.rb', line 47

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

#organizationObject



27
28
29
# File 'lib/megam/core/organizations.rb', line 27

def organization
  self
end

#to_hashObject

Transform the ruby obj -> to a Hash



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

def to_hash
  index_hash = Hash.new
  index_hash["json_claz"] = self.class.name
  index_hash["id"] = id
  index_hash["name"] = name
  index_hash["accounts_id"] = accounts_id
  index_hash["created_at"] = created_at
  index_hash
end

#to_json(*a) ⇒ Object



74
75
76
# File 'lib/megam/core/organizations.rb', line 74

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

#to_sObject



132
133
134
# File 'lib/megam/core/organizations.rb', line 132

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