Class: BrownPaperTickets::Price

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

Constant Summary collapse

REQUIRED_ATTR =
["price", "price_name"]
ATTRS =
["price", "price_name", "event_id", "date_id", "price_id"]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Price.



13
14
15
16
17
# File 'lib/brownpapertickets/price.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/price.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/price.rb', line 7

def attributes
  @attributes
end

#server_responseObject (readonly)

Returns the value of attribute server_response.



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

def server_response
  @server_response
end

Instance Method Details

#allObject



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

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

#create(params = {}) ⇒ Object



97
98
99
100
# File 'lib/brownpapertickets/price.rb', line 97

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

#find(event_id) ⇒ Object



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

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

#liveObject



102
103
104
105
# File 'lib/brownpapertickets/price.rb', line 102

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

#live=(param) ⇒ Object



107
108
109
110
111
112
113
# File 'lib/brownpapertickets/price.rb', line 107

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/price.rb', line 19

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

#new_save(param) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/brownpapertickets/price.rb', line 81

def new_save(param)
  body = {"id" => @@id, "account" => @@account, "event_id" => event_id, "date_id"=> date_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 == "addprice"
    self.price_id = (xml/:price_id).inner_html if (xml/:resultcode).inner_html == "000000"
    process_create_response( (xml/:resultcode).inner_html, price_id)
  else
    process_update_response( (xml/:resultcode).inner_html)
  end
end

#process_create_response(response, event_id) ⇒ Object



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

def process_create_response(response, event_id)
  case response
  when "0000" then
    self.event_id = event_id
    @server_response = "success"
    return true
   when "300042" then
    @server_response = "Required variables are missing"
    return false
  when "300043" then
    @server_response = "Unknown error while posting info to DB"
    return false
  when "300044" then
    @server_response = "Event does not belong to account"
    return false
  when "300045" then
    @server_response = "Unknown error while fetching date info from DB"
    return false
   when "300046" then
      @server_response = "Unable to find date"
      return false
   when "300047" then
      @server_response = "Unable to add price"
      return false
  else
    @server_response = "Unknown error"
    return false
  end
end

#process_update_response(response) ⇒ Object



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
# File 'lib/brownpapertickets/price.rb', line 162

def process_update_response(response)
  case response
  when "000000" then
    @server_response = "Success"
    return true
  when "300063" then
    @server_response = "Required variables are missing"
    return false
  when "300064" then
    @server_response = "Unable to find event"
    return false
  when "300065" then
    @server_response = "Event does not belong to account"
    return false
  when "300066" then
    @server_response = "Unknown error"
    return false
  when "300067" then
    @server_response = "No such price for this event"
    return false
  when "300068" then
    @server_response = "Required variables are missing"
    return false   
  when "300069" then
    @server_response = "Unable to change price"
    return false 
  else
    @server_response = "Unknown error"
    return false                            
  end
end

#publicObject



115
116
117
118
# File 'lib/brownpapertickets/price.rb', line 115

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

#public=(param) ⇒ Object



120
121
122
123
124
125
126
# File 'lib/brownpapertickets/price.rb', line 120

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

#save!Object

Response while saving 300042 - Required variables are missing

300043 - Unknown error while fetching event info from DB
300044 - Event does not belong to account
300045 - Unknown error while fetching date info from DB
300046 - Unable to find date
300047 - Unable to add price
0000 - Success.


72
73
74
75
76
77
78
79
# File 'lib/brownpapertickets/price.rb', line 72

def save!
  if self.price_id.blank?
    return false unless validates_required_attr
    new_save("addprice")
  else
    new_save("changeprice")
  end
end

#update_attribute(key, value) ⇒ Object

resultcode

	  300063 - Required variables are missing
   300064 - Unable to find event
   300065 - Event does not belong to account
   300066 - Unknown error
   300067 - No such price for this event
   300068 - Required variables are missing
   300069 - Unable to change price
   000000 - Success


138
139
140
141
142
143
144
145
146
147
# File 'lib/brownpapertickets/price.rb', line 138

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/changeprice",: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



149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/brownpapertickets/price.rb', line 149

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/changeprice",: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/price.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