Class: BrownPaperTickets::Date

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/brownpapertickets/date.rb

Constant Summary collapse

REQUIRED_ATTR =
["begin_time", "end_time", "sales_end", "max_sales"]
ATTRS =
["begin_time", "end_time", "event_id", "date_id", "sales_end", "max_sales"]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, account, attributes = {}) ⇒ Date

Returns a new instance of Date.



13
14
15
16
17
# File 'lib/brownpapertickets/date.rb', line 13

def initialize(id, , attributes={})
  @@id       = id
  @@account  = 
  @attributes = attributes
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/brownpapertickets/date.rb', line 38

def method_missing(m, *args, &block)
  if ATTRS.include?(m.to_s.gsub("=",""))
    if m.to_s.include?("=")
      self.attributes[m.to_s.gsub("=","")] = *args.to_s 
    else
      result = self.attributes[m.to_s]
    end
  else
    raise NoMethodError.new("Method missing #{m}")
  end
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



7
8
9
# File 'lib/brownpapertickets/date.rb', line 7

def attributes
  @attributes
end

#server_responseObject (readonly)

Returns the value of attribute server_response.



7
8
9
# File 'lib/brownpapertickets/date.rb', line 7

def server_response
  @server_response
end

Instance Method Details

#allObject



23
24
25
26
27
28
29
30
# File 'lib/brownpapertickets/date.rb', line 23

def all
  dates = Event.get("/datelist", :query =>{"id" => @@id , "account" => @@account})
  parsed_date = []
  date.parsed_response["document"]["date"].each do |date|
    parsed_date << Event.new(@id,@account, date)
  end
  return parsed_event
end

#create(params = {}) ⇒ Object



94
95
96
97
# File 'lib/brownpapertickets/date.rb', line 94

def create(params={})
  date = Event.new(@@id,@@account, params)
  date.save!
end

#find(event_id) ⇒ Object



32
33
34
35
36
# File 'lib/brownpapertickets/date.rb', line 32

def find(event_id)
  date = Event.get("/datelist",:query =>{"id" => @@id , "account" => @@account, "event_id" => event_id, "date_id"=>date_id})
  @attributes = event.parsed_response["document"]["date"]
  return self
end

#liveObject



99
100
101
102
# File 'lib/brownpapertickets/date.rb', line 99

def live
  return true if self.attributes["live"] == "y"
  return false
end

#live=(param) ⇒ Object



104
105
106
107
108
109
110
# File 'lib/brownpapertickets/date.rb', line 104

def live=(param)
  if param
    self.attributes["live"] = "y"
  else
    self.attributes["live"] = "f"
  end 
end

#new(params = {}) ⇒ Object



19
20
21
# File 'lib/brownpapertickets/date.rb', line 19

def new(params={})
  Event.new(@@id,@@account, params, event_id)
end

#new_save(param) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/brownpapertickets/date.rb', line 79

def new_save(param)
  body = {"id" => @@id, "account" => @@account, "event_id" => event_id}
  query = self.attributes.merge("id" => @@id, "account" => @@account)
  response = BrownPaperTickets::Httpost.new(Net::HTTP::Post, "https://www.brownpapertickets.com/api2/#{param}",:query => query)
  response.options[:body] = query
  st = response.perform
  xml = Hpricot(st.response.body)
  if param == "adddate"
    self.date_id = (xml/:date_id).inner_html if (xml/:resultcode).inner_html == "000000"
    process_create_response( (xml/:resultcode).inner_html, date_id)
  else
    process_update_response( (xml/:resultcode).inner_html)
  end
end

#process_create_response(response, event_id) ⇒ Object



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/brownpapertickets/date.rb', line 195

def process_create_response(response, event_id)
  case response
  when "0000" then
    self.date_id = date_id
    @server_response = "success"
    return true
   when "300038" then
    @server_response = "Required variables are missing"
    return false
  when "300039" then
    @server_response = "Unknown error while posting info to DB"
    return false
  when "300040" then
    @server_response = "Event does not belong to user"
    return false
  when "300041" then
    @server_response = "Unable to add date"
    return false
  else
    @server_response = "Unknown error"
    return false
  end
end

#process_update_response(response) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/brownpapertickets/date.rb', line 160

def process_update_response(response)
  case response
  when "000000" then
    @server_response = "Success"
    return true
  when "300055" then
    @server_response = "Required variables are missing"
    return false
  when "300056" then
    @server_response = "Unknown error"
    return false
  when "300057" then
    @server_response = "Unable to find event"
    return false
  when "300058" then
    @server_response = "Event does not belong to account"
    return false
  when "300059" then
    @server_response = "Unknown error"
    return false
  when "300060" then
    @server_response = "Unable to find date"
    return false   
  when "300061" then
    @server_response = "Required variables are missing"
    return false
  when "300062" then
    @server_response = "Unable to update date"
    return false   
  else
    @server_response = "Unknown error"
    return false                            
  end
end

#publicObject



112
113
114
115
# File 'lib/brownpapertickets/date.rb', line 112

def public
  return true if self.attributes["public"] == "t"
  return false
end

#public=(param) ⇒ Object



117
118
119
120
121
122
123
# File 'lib/brownpapertickets/date.rb', line 117

def public=(param)
  if param
    self.attributes["public"] = "t"
  else
    self.attributes["public"] = "n"
  end 
end

#save!Object

300039 - Unknown error while posting info to DB

300040 - Event does not belong to user
300041 - Unable to add date
0000   - Success


70
71
72
73
74
75
76
77
# File 'lib/brownpapertickets/date.rb', line 70

def save!
  if self.date_id.blank?
    return false unless validates_required_attr
    new_save("adddate")
  else
    new_save("changedate")
  end
end

#update_attribute(key, value) ⇒ Object

resultcode

	  300055 - Required variables are missing
   300056 - Unknown error
   300057 - Unable to find event
   300058 - Event does not belong to account
   300059 - Unknown error
   300060 - Unable to find date
   300061 - Required variables are missing
   300062 - Unable to update date
   000000 -	Success


136
137
138
139
140
141
142
143
144
145
# File 'lib/brownpapertickets/date.rb', line 136

def update_attribute(key, value)
  assign = key.to_s + "="
  self.send(assign,value)
  query = {"id" => @@id, "account" => @@account, key.to_s => value, "event_id" => self.event_id}
  response = BrownPaperTickets::Httpost.new(Net::HTTP::Post, "https://www.brownpapertickets.com/api2/changedate",:query => query)
  response.options[:body] = query
  st = response.perform
  xml = Hpricot(st.response.body)
  return process_update_response((xml/:resultcode).inner_html)
end

#update_attributes(params) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/brownpapertickets/date.rb', line 147

def update_attributes(params)
  params.each do |key, value|
    assign = key.to_s + "="
    self.send(assign,value)
  end
  query = {"id" => @@id, "account" => @@account,  "event_id" => self.event_id}.merge(params)
  response = BrownPaperTickets::Httpost.new(Net::HTTP::Post, "https://www.brownpapertickets.com/api2/changedate",:query => query)
  response.options[:body] = query
  st = response.perform
  xml = Hpricot(st.response.body)
  return process_update_response((xml/:resultcode).inner_html)
end

#validates_required_attrObject



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/brownpapertickets/date.rb', line 50

def validates_required_attr
  missing_field = []
  REQUIRED_ATTR.each do |attr|
    missing_field << attr if self.send(attr,nil,nil).blank?
  end
  unless missing_field.blank?
    @server_response = "The following attributes are missing:"
    missing_field.each{|att| @server_response = @server_response + " " + att }
    return false
  end
  return true
end