Class: Megam::Promos

Inherits:
RestAdapter show all
Defined in:
lib/megam/core/promos.rb

Instance Attribute Summary

Attributes inherited from RestAdapter

#api_key, #email, #headers, #host, #master_key, #org_id, #password_hash

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from RestAdapter

#megam_rest

Constructor Details

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

Returns a new instance of Promos.



3
4
5
6
7
8
9
10
# File 'lib/megam/core/promos.rb', line 3

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

Class Method Details

.create(params) ⇒ Object



117
118
119
120
# File 'lib/megam/core/promos.rb', line 117

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

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



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

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

.json_create(o) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/megam/core/promos.rb', line 89

def self.json_create(o)
  promos = new
  promos.id(o["id"]) if o.has_key?("id")
  promos.code(o["code"]) if o.has_key?("code")
  promos.amount(o["amount"]) if o.has_key?("amount")
  promos.created_at(o["created_at"]) if o.has_key?("created_at")
  #success or error
  promos.some_msg[:code] = o["code"] if o.has_key?("code")
  promos.some_msg[:msg_type] = o["msg_type"] if o.has_key?("msg_type")
  promos.some_msg[:msg]= o["msg"] if o.has_key?("msg")
  promos.some_msg[:links] = o["links"] if o.has_key?("links")
  promos
end

.list(params) ⇒ Object



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

def self.list(params)
  promos = self.new(params["email"], params["api_key"],  params["host"])
  promos.megam_rest.get_promos
end

.show(params) ⇒ Object

Show a particular promo by name, Megam::Promos



134
135
136
137
# File 'lib/megam/core/promos.rb', line 134

def self.show(params)
  pre = self.new(params["email"], params["api_key"], params["host"])
  pre.megam_rest.get_promos(params["code"])
end

Instance Method Details

#amount(arg = nil) ⇒ Object



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

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

#code(arg = nil) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/megam/core/promos.rb', line 24

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

#createObject

Create the predef via the REST API



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

def create
  megam_rest.post_promos(to_hash) #WONT BE USED AS OF NOW
end

#created_at(arg = nil) ⇒ Object



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

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

#error?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/megam/core/promos.rb', line 58

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

#for_jsonObject



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

def for_json
  result = {
    "id" => id,
    "code" => code,
    "amount" => amount,
    "created_at" => created_at
  }
  result
end

#from_hash(o) ⇒ Object



109
110
111
112
113
114
115
# File 'lib/megam/core/promos.rb', line 109

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

#id(arg = nil) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/megam/core/promos.rb', line 16

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

#promosObject



12
13
14
# File 'lib/megam/core/promos.rb', line 12

def promos
  self
end

#some_msg(arg = nil) ⇒ Object



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

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



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

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



75
76
77
# File 'lib/megam/core/promos.rb', line 75

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

#to_sObject



140
141
142
# File 'lib/megam/core/promos.rb', line 140

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