brownpapertickets 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,229 @@
1
+ require 'hpricot'
2
+ module BrownPaperTickets
3
+ class Event
4
+ include HTTParty
5
+
6
+ base_uri "https://www.brownpapertickets.com/api2"
7
+
8
+ attr_reader :attributes, :server_response
9
+
10
+ REQUIRED_ATTR=["e_name","e_city","e_state", "e_short_description", "e_description"]
11
+
12
+ ATTRS=["e_name","e_city","e_state", "e_short_description", "e_description","e_address1","e_address2","e_zip","e_phone","e_web","end_of_event_message",
13
+ "end_of_sale_message","date_notes","e_notes","keywords","c_name","c_email","c_phone","c_fax","c_address1","c_address2","c_city","c_state","c_zip",
14
+ "c_country","public", "title","link", "description", "event_id", "tickets_sold"]
15
+
16
+ def initialize(id, account, attributes={})
17
+ @@id = id
18
+ @@account = account
19
+ @attributes = attributes
20
+ end
21
+
22
+ def new(params={})
23
+ Event.new(@@id,@@account, params)
24
+ end
25
+
26
+ def all
27
+ events = Event.get("/eventlist", :query =>{"id" => @@id , "account" => @@account })
28
+ parsed_event = []
29
+ events.parsed_response["document"]["event"].each do |event|
30
+ parsed_event << Event.new(@id,@account, event)
31
+ end
32
+ return parsed_event
33
+ end
34
+
35
+ def find(event_id)
36
+ event = Event.get("/eventlist",:query =>{"id" => @@id , "account" => @@account, "event_id" => event_id })
37
+ @attributes = event.parsed_response["document"]["event"]
38
+ return self
39
+ end
40
+
41
+ def method_missing(m, *args, &block)
42
+ if ATTRS.include?(m.to_s.gsub("=",""))
43
+ if m.to_s.include?("=")
44
+ self.attributes[m.to_s.gsub("=","")] = *args.to_s
45
+ else
46
+ result = self.attributes[m.to_s]
47
+ end
48
+ else
49
+ raise NoMethodError.new("Method missing #{m}")
50
+ end
51
+ end
52
+
53
+ def validates_required_attr
54
+ missing_field = []
55
+ REQUIRED_ATTR.each do |attr|
56
+ missing_field << attr if self.send(attr,nil,nil).blank?
57
+ end
58
+ unless missing_field.blank?
59
+ @server_response = "The following attributes are missing:"
60
+ missing_field.each{|att| @server_response = @server_response + " " + att }
61
+ return false
62
+ end
63
+ return true
64
+ end
65
+
66
+ # Response while saving
67
+ # 300036 - Required variables are missing
68
+ # 300037 - Unknown error while posting info to DB
69
+ # 000000 - Success
70
+
71
+ def save!
72
+ if self.event_id.blank?
73
+ #changeevent
74
+ return false unless validates_required_attr
75
+ new_save("createevent")
76
+ else
77
+ #createevent
78
+ new_save("changeevent")
79
+ end
80
+ end
81
+
82
+ def new_save(param)
83
+ body = {"id" => @@id, "account" => @@account}
84
+ query = self.attributes.merge("id" => @@id, "account" => @@account)
85
+ response = BrownPaperTickets::Httpost.new(Net::HTTP::Post, "https://www.brownpapertickets.com/api2/#{param}",:query => query)
86
+ response.options[:body] = query
87
+ st = response.perform
88
+ xml = Hpricot(st.response.body)
89
+
90
+ if param == "createevent"
91
+ event_id = (xml/:event_id).inner_html if (xml/:resultcode).inner_html == "000000"
92
+ process_create_response( (xml/:resultcode).inner_html, event_id)
93
+ p event_id
94
+ else
95
+ process_update_response( (xml/:resultcode).inner_html)
96
+ end
97
+ end
98
+
99
+ def title=(param)
100
+ self.attributes["e_name"] = param
101
+ self.attributes["title"] = param
102
+ end
103
+
104
+ def e_name=(param)
105
+ self.attributes["e_name"] = param
106
+ self.attributes["title"] = param
107
+ end
108
+
109
+ def create(params={})
110
+ event = Event.new(@@id,@@account, params)
111
+ event.save!
112
+ end
113
+
114
+ def live
115
+ return true if self.attributes["live"] == "y"
116
+ return false
117
+ end
118
+
119
+ def live=(param)
120
+ if param
121
+ self.attributes["live"] = "y"
122
+ else
123
+ self.attributes["live"] = "f"
124
+ end
125
+ end
126
+
127
+ def public
128
+ return true if self.attributes["public"] == "t"
129
+ return false
130
+ end
131
+
132
+ def public=(param)
133
+ if param
134
+ self.attributes["public"] = "t"
135
+ else
136
+ self.attributes["public"] = "n"
137
+ end
138
+ end
139
+
140
+ # resultcode
141
+ # 300049 - Required variables are missing
142
+ # 300050 - Unknown error
143
+ # 300051 - Unable to find event
144
+ # 300052 - Event does not belong to account
145
+ # 300053 - Required variables are missing
146
+ # 300054 - Unable to update event
147
+ # 000000 - Success
148
+
149
+ def update_attribute(key, value)
150
+ assign = key.to_s + "="
151
+ self.send(assign,value)
152
+ query = {"id" => @@id, "account" => @@account, key.to_s => value, "event_id" => self.event_id}
153
+ response = BrownPaperTickets::Httpost.new(Net::HTTP::Post, "https://www.brownpapertickets.com/api2/changeevent",:query => query)
154
+ response.options[:body] = query
155
+ st = response.perform
156
+ xml = Hpricot(st.response.body)
157
+ return process_update_response((xml/:resultcode).inner_html)
158
+ end
159
+
160
+ def update_attributes(params)
161
+ params.each do |key, value|
162
+ assign = key.to_s + "="
163
+ self.send(assign,value)
164
+ end
165
+ query = {"id" => @@id, "account" => @@account, "event_id" => self.event_id}.merge(params)
166
+ response = BrownPaperTickets::Httpost.new(Net::HTTP::Post, "https://www.brownpapertickets.com/api2/changeevent",:query => query)
167
+ response.options[:body] = query
168
+ st = response.perform
169
+ xml = Hpricot(st.response.body)
170
+ return process_update_response((xml/:resultcode).inner_html)
171
+ end
172
+
173
+ def process_update_response(response)
174
+ case response
175
+ when "000000" then
176
+ @server_response = "success"
177
+ return true
178
+ when "300049" then
179
+ @server_response = "Required variables are missing"
180
+ return false
181
+ when "300050" then
182
+ @server_response = "Unknown error"
183
+ return false
184
+ when "300051" then
185
+ @server_response = "Unable to find event"
186
+ return false
187
+ when "300052" then
188
+ @server_response = "Event does not belong to account"
189
+ return false
190
+ when "300053" then
191
+ @server_response = "Required variables are missing"
192
+ return false
193
+ when "300054" then
194
+ @server_response = "Unable to update event"
195
+ return false
196
+ else
197
+ @server_response = "Unknown error"
198
+ return false
199
+ end
200
+ end
201
+
202
+ def process_create_response(response, event_id)
203
+ case response
204
+ when "000000" then
205
+ self.event_id = event_id
206
+ @server_response = "success"
207
+ return true
208
+ when "300036" then
209
+ @server_response = "There are some missing attr"
210
+ return false
211
+ when "300037" then
212
+ @server_response = "Unknown error while posting info to DB"
213
+ return false
214
+ else
215
+ @server_response = "Unknown error"
216
+ return false
217
+ end
218
+ end
219
+
220
+ def server_response
221
+ @server_response
222
+ end
223
+
224
+ def attributes
225
+ @attributes
226
+ end
227
+
228
+ end
229
+ end
@@ -0,0 +1,215 @@
1
+ module BrownPaperTickets
2
+ class Httpost < HTTParty::Request
3
+
4
+ SupportedHTTPMethods = [
5
+ Net::HTTP::Get,
6
+ Net::HTTP::Post,
7
+ Net::HTTP::Put,
8
+ Net::HTTP::Delete,
9
+ Net::HTTP::Head,
10
+ Net::HTTP::Options
11
+ ]
12
+
13
+ SupportedURISchemes = [URI::HTTP, URI::HTTPS]
14
+
15
+ attr_accessor :http_method, :path, :options, :last_response, :redirect
16
+
17
+ def initialize(http_method, path, o={})
18
+ super
19
+ end
20
+
21
+ def path=(uri)
22
+ @path = URI.parse(uri)
23
+ end
24
+
25
+ def uri
26
+ new_uri = path.relative? ? URI.parse("#{options[:base_uri]}#{path}") : path
27
+
28
+ # avoid double query string on redirects [#12]
29
+ unless redirect
30
+ new_uri.query = query_string(new_uri)
31
+ end
32
+
33
+ unless SupportedURISchemes.include? new_uri.class
34
+ raise UnsupportedURIScheme, "'#{new_uri}' Must be HTTP or HTTPS"
35
+ end
36
+
37
+ new_uri
38
+ end
39
+
40
+ def format
41
+ options[:format] || (format_from_mimetype(last_response['content-type']) if last_response)
42
+ end
43
+
44
+ def parser
45
+ options[:parser]
46
+ end
47
+
48
+ def perform
49
+ validate
50
+ setup_raw_request
51
+ get_response
52
+ return self.last_response
53
+ #handle_response
54
+ end
55
+
56
+ #def body
57
+ # options[:body].is_a?(Hash) ? options[:body].to_params : options[:body]
58
+ #end
59
+
60
+ private
61
+
62
+ def http
63
+ http = Net::HTTP.new(uri.host, uri.port, options[:http_proxyaddr], options[:http_proxyport])
64
+ http.use_ssl = ssl_implied?
65
+
66
+ if options[:timeout] && options[:timeout].is_a?(Integer)
67
+ http.open_timeout = options[:timeout]
68
+ http.read_timeout = options[:timeout]
69
+ end
70
+
71
+ if options[:pem] && http.use_ssl?
72
+ http.cert = OpenSSL::X509::Certificate.new(options[:pem])
73
+ http.key = OpenSSL::PKey::RSA.new(options[:pem])
74
+ http.verify_mode = OpenSSL::SSL::VERIFY_PEER
75
+ else
76
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
77
+ end
78
+
79
+ if options[:debug_output]
80
+ http.set_debug_output(options[:debug_output])
81
+ end
82
+
83
+ http
84
+ end
85
+
86
+ def ssl_implied?
87
+ uri.port == 443 || uri.instance_of?(URI::HTTPS)
88
+ end
89
+
90
+ def body
91
+ options[:body].is_a?(Hash) ? options[:body].to_params : options[:body]
92
+ end
93
+
94
+ def credentials
95
+ options[:basic_auth] || options[:digest_auth]
96
+ end
97
+
98
+ def username
99
+ credentials[:username]
100
+ end
101
+
102
+ def password
103
+ credentials[:password]
104
+ end
105
+
106
+ def setup_raw_request
107
+ @raw_request = http_method.new(uri.request_uri)
108
+ @raw_request.body = body if body
109
+ @raw_request.initialize_http_header(options[:headers])
110
+ @raw_request.basic_auth(username, password) if options[:basic_auth]
111
+ setup_digest_auth if options[:digest_auth]
112
+ end
113
+
114
+ def setup_digest_auth
115
+ res = http.head(uri.request_uri, options[:headers])
116
+ if res['www-authenticate'] != nil && res['www-authenticate'].length > 0
117
+ @raw_request.digest_auth(username, password, res)
118
+ end
119
+ end
120
+
121
+ def perform_actual_request
122
+ http.request(@raw_request)
123
+ end
124
+
125
+ def get_response
126
+ self.last_response = perform_actual_request
127
+ end
128
+
129
+ def query_string(uri)
130
+ query_string_parts = []
131
+ query_string_parts << uri.query unless uri.query.nil?
132
+
133
+ if options[:query].is_a?(Hash)
134
+ query_string_parts << options[:default_params].merge(options[:query]).to_params
135
+ else
136
+ query_string_parts << options[:default_params].to_params unless options[:default_params].empty?
137
+ query_string_parts << options[:query] unless options[:query].nil?
138
+ end
139
+
140
+ query_string_parts.size > 0 ? query_string_parts.join('&') : nil
141
+ end
142
+
143
+ # Raises exception Net::XXX (http error code) if an http error occured
144
+ def handle_response
145
+ handle_deflation
146
+ case last_response
147
+ when Net::HTTPMultipleChoice, # 300
148
+ Net::HTTPMovedPermanently, # 301
149
+ Net::HTTPFound, # 302
150
+ Net::HTTPSeeOther, # 303
151
+ Net::HTTPUseProxy, # 305
152
+ Net::HTTPTemporaryRedirect
153
+ if last_response.key?('location')
154
+ options[:limit] -= 1
155
+ self.path = last_response['location']
156
+ self.redirect = true
157
+ self.http_method = Net::HTTP::Get unless options[:maintain_method_across_redirects]
158
+ capture_cookies(last_response)
159
+ perform
160
+ else
161
+ last_response
162
+ end
163
+ else
164
+ Response.new(last_response, parse_response(last_response.body))
165
+ end
166
+ end
167
+
168
+ # Inspired by Ruby 1.9
169
+ def handle_deflation
170
+ case last_response["content-encoding"]
171
+ when "gzip"
172
+ body_io = StringIO.new(last_response.body)
173
+ last_response.body.replace Zlib::GzipReader.new(body_io).read
174
+ when "deflate"
175
+ last_response.body.replace Zlib::Inflate.inflate(last_response.body)
176
+ end
177
+ end
178
+
179
+ def parse_response(body)
180
+ parser.call(body, format)
181
+ end
182
+
183
+ def capture_cookies(response)
184
+ return unless response['Set-Cookie']
185
+ cookies_hash = HTTParty::CookieHash.new()
186
+ cookies_hash.add_cookies(options[:headers]['Cookie']) if options[:headers] && options[:headers]['Cookie']
187
+ cookies_hash.add_cookies(response['Set-Cookie'])
188
+ options[:headers] ||= {}
189
+ options[:headers]['Cookie'] = cookies_hash.to_cookie_string
190
+ end
191
+
192
+ # Uses the HTTP Content-Type header to determine the format of the
193
+ # response It compares the MIME type returned to the types stored in the
194
+ # SupportedFormats hash
195
+ def format_from_mimetype(mimetype)
196
+ if mimetype && parser.respond_to?(:format_from_mimetype)
197
+ parser.format_from_mimetype(mimetype)
198
+ end
199
+ end
200
+
201
+ def validate
202
+ raise HTTParty::RedirectionTooDeep.new(last_response), 'HTTP redirects too deep' if options[:limit].to_i <= 0
203
+ raise ArgumentError, 'only get, post, put, delete, head, and options methods are supported' unless SupportedHTTPMethods.include?(http_method)
204
+ raise ArgumentError, ':headers must be a hash' if options[:headers] && !options[:headers].is_a?(Hash)
205
+ raise ArgumentError, 'only one authentication method, :basic_auth or :digest_auth may be used at a time' if options[:basic_auth] && options[:digest_auth]
206
+ raise ArgumentError, ':basic_auth must be a hash' if options[:basic_auth] && !options[:basic_auth].is_a?(Hash)
207
+ raise ArgumentError, ':digest_auth must be a hash' if options[:digest_auth] && !options[:digest_auth].is_a?(Hash)
208
+ raise ArgumentError, ':query must be hash if using HTTP Post' if post? && !options[:query].nil? && !options[:query].is_a?(Hash)
209
+ end
210
+
211
+ def post?
212
+ Net::HTTP::Post == http_method
213
+ end
214
+ end
215
+ end
@@ -0,0 +1,225 @@
1
+ require 'hpricot'
2
+ module BrownPaperTickets
3
+ class Price
4
+ include HTTParty
5
+ base_uri "https://www.brownpapertickets.com/api2"
6
+
7
+ attr_reader :attributes, :server_response
8
+
9
+ REQUIRED_ATTR=["price", "price_name"]
10
+
11
+ ATTRS=["price", "price_name", "event_id", "date_id", "price_id"]
12
+
13
+ def initialize(id, account, attributes={})
14
+ @@id = id
15
+ @@account = account
16
+ @attributes = attributes
17
+ end
18
+
19
+ def new(params={})
20
+ Event.new(@@id,@@account, params, event_id, date_id)
21
+ end
22
+
23
+ def all
24
+ prices = Event.get("/pricelist", :query =>{"id" => @@id , "account" => @@account, "event_id" => event_id , "date_id" => date_id})
25
+ parsed_date = []
26
+ price.parsed_response["document"]["price"].each do |date|
27
+ parsed_date << Event.new(@id,@account, price)
28
+ end
29
+ return parsed_event
30
+ end
31
+
32
+ def find(event_id)
33
+ price = Event.get("/pricelist",:query =>{"id" => @@id , "account" => @@account, "event_id" => event_id, "date_id"=>date_id})
34
+ @attributes = event.parsed_response["document"]["event"]
35
+ return self
36
+ end
37
+
38
+ def method_missing(m, *args, &block)
39
+ if ATTRS.include?(m.to_s.gsub("=",""))
40
+ if m.to_s.include?("=")
41
+ self.attributes[m.to_s.gsub("=","")] = *args.to_s
42
+ else
43
+ result = self.attributes[m.to_s]
44
+ end
45
+ else
46
+ raise NoMethodError.new("Method missing #{m}")
47
+ end
48
+ end
49
+
50
+ def validates_required_attr
51
+ missing_field = []
52
+ REQUIRED_ATTR.each do |attr|
53
+ missing_field << attr if self.send(attr,nil,nil).blank?
54
+ end
55
+ unless missing_field.blank?
56
+ @server_response = "The following attributes are missing:"
57
+ missing_field.each{|att| @server_response = @server_response + " " + att }
58
+ return false
59
+ end
60
+ return true
61
+ end
62
+
63
+ # Response while saving
64
+ # 300036 - Required variables are missing
65
+ # 300037 - Unknown error while posting info to DB
66
+ # 000000 - Success
67
+
68
+ def save!
69
+ new_save("addprice")
70
+ p "save1"*12
71
+ end
72
+
73
+ def new_save(param)
74
+ body = {"id" => @@id, "account" => @@account, "event_id" => event_id, "date_id"=> date_id}
75
+ query = self.attributes.merge("id" => @@id, "account" => @@account)
76
+ response = BrownPaperTickets::Httpost.new(Net::HTTP::Post, "https://www.brownpapertickets.com/api2/#{param}",:query => query)
77
+ response.options[:body] = query
78
+ st = response.perform
79
+ xml = Hpricot(st.response.body)
80
+
81
+ if param == "addprice"
82
+ p "algo"*12
83
+ self.price_id = (xml/:price_id).inner_html if (xml/:resultcode).inner_html == "000000"
84
+ process_create_response( (xml/:resultcode).inner_html, price_id)
85
+ p (xml/:resultcode).inner_html
86
+ else
87
+ process_update_response( (xml/:resultcode).inner_html)
88
+ end
89
+ end
90
+
91
+ def create(params={})
92
+ price = Event.new(@@id,@@account, params)
93
+ price.save!
94
+ end
95
+
96
+ def live
97
+ return true if self.attributes["live"] == "y"
98
+ return false
99
+ end
100
+
101
+ def live=(param)
102
+ if param
103
+ self.attributes["live"] = "y"
104
+ else
105
+ self.attributes["live"] = "f"
106
+ end
107
+ end
108
+
109
+ def public
110
+ return true if self.attributes["public"] == "t"
111
+ return false
112
+ end
113
+
114
+ def public=(param)
115
+ if param
116
+ self.attributes["public"] = "t"
117
+ else
118
+ self.attributes["public"] = "n"
119
+ end
120
+ end
121
+
122
+ # resultcode
123
+ # 300049 - Required variables are missing
124
+ # 300050 - Unknown error
125
+ # 300051 - Unable to find event
126
+ # 300052 - Event does not belong to account
127
+ # 300053 - Required variables are missing
128
+ # 300054 - Unable to update event
129
+ # 000000 - Success
130
+
131
+ def update_attribute(key, value)
132
+ assign = key.to_s + "="
133
+ self.send(assign,value)
134
+ query = {"id" => @@id, "account" => @@account, key.to_s => value, "event_id" => self.event_id}
135
+ response = BrownPaperTickets::Httpost.new(Net::HTTP::Post, "https://www.brownpapertickets.com/api2/changeprice",:query => query)
136
+ response.options[:body] = query
137
+ st = response.perform
138
+ xml = Hpricot(st.response.body)
139
+ return process_update_response((xml/:resultcode).inner_html)
140
+ end
141
+
142
+ def update_attributes(params)
143
+ params.each do |key, value|
144
+ assign = key.to_s + "="
145
+ self.send(assign,value)
146
+ end
147
+ query = {"id" => @@id, "account" => @@account, "event_id" => self.event_id}.merge(params)
148
+ response = BrownPaperTickets::Httpost.new(Net::HTTP::Post, "https://www.brownpapertickets.com/api2/changeprice",:query => query)
149
+ response.options[:body] = query
150
+ st = response.perform
151
+ xml = Hpricot(st.response.body)
152
+ return process_update_response((xml/:resultcode).inner_html)
153
+ end
154
+
155
+ def process_update_response(response)
156
+ case response
157
+ when "000000" then
158
+ @server_response = "Success"
159
+ return true
160
+ when "300063" then
161
+ @server_response = "Required variables are missing"
162
+ return false
163
+ when "300064" then
164
+ @server_response = "Unable to find event"
165
+ return false
166
+ when "300065" then
167
+ @server_response = "Event does not belong to account"
168
+ return false
169
+ when "300066" then
170
+ @server_response = "Unknown error"
171
+ return false
172
+ when "300067" then
173
+ @server_response = "No such price for this event"
174
+ return false
175
+ when "300068" then
176
+ @server_response = "Required variables are missing"
177
+ return false
178
+ when "300069" then
179
+ @server_response = "Unable to change price"
180
+ return false
181
+ else
182
+ @server_response = "Unknown error"
183
+ return false
184
+ end
185
+ end
186
+
187
+ def process_create_response(response, event_id)
188
+ case response
189
+ when "0000" then
190
+ self.event_id = event_id
191
+ @server_response = "success"
192
+ return true
193
+ when "300042" then
194
+ @server_response = "Required variables are missing"
195
+ return false
196
+ when "300043" then
197
+ @server_response = "Unknown error while posting info to DB"
198
+ return false
199
+ when "300044" then
200
+ @server_response = "Event does not belong to account"
201
+ return false
202
+ when "300045" then
203
+ @server_response = "Unknown error while fetching date info from DB"
204
+ return false
205
+ when "300046" then
206
+ @server_response = "Unable to find date"
207
+ return false
208
+ when "300047" then
209
+ @server_response = "Unable to add price"
210
+ return false
211
+ else
212
+ @server_response = "Unknown error"
213
+ return false
214
+ end
215
+ end
216
+
217
+ def server_response
218
+ @server_response
219
+ end
220
+
221
+ def attributes
222
+ @attributes
223
+ end
224
+ end
225
+ end