Module: Eventbrite::Util
- Defined in:
- lib/eventbrite/util.rb
Class Method Summary collapse
- .convert_to_eventbrite_object(resp, token, parent = nil) ⇒ Object
- .flatten_params(params, parent_key = nil) ⇒ Object
- .flatten_params_array(value, calculated_key) ⇒ Object
- .object_classes ⇒ Object
- .symbolize_names(object) ⇒ Object
- .url_encode(key) ⇒ Object
Class Method Details
.convert_to_eventbrite_object(resp, token, parent = nil) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/eventbrite/util.rb', line 33 def self.convert_to_eventbrite_object(resp, token, parent=nil) # TODO: fix this case resp when Array resp.map { |i| convert_to_eventbrite_object(i, token, parent) } when Hash # Try converting to a known object class. If none available, fall back to generic EventbriteObject object_classes.fetch(parent, EventbriteObject).construct_from(resp, token) else resp end end |
.flatten_params(params, parent_key = nil) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/eventbrite/util.rb', line 66 def self.flatten_params(params, parent_key=nil) result = [] params.each do |key, value| calculated_key = parent_key ? "#{parent_key}[#{url_encode(key)}]" : url_encode(key) if value.is_a?(Hash) result += flatten_params(value, calculated_key) elsif value.is_a?(Array) result += flatten_params_array(value, calculated_key) else result << [calculated_key, value] end end result end |
.flatten_params_array(value, calculated_key) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/eventbrite/util.rb', line 81 def self.flatten_params_array(value, calculated_key) result = [] value.each do |elem| if elem.is_a?(Hash) result += flatten_params(elem, calculated_key) elsif elem.is_a?(Array) result += flatten_params_array(elem, calculated_key) else result << ["#{calculated_key}[]", elem] end end result end |
.object_classes ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/eventbrite/util.rb', line 3 def self.object_classes @object_classes ||= { events: Event, categories: Category, attendees: Attendee, orders: Order, discouns: Discount, access_codes: AccessCode, ticket_classes: TicketClass, transfers: Transfer, teams: Team, webhooks: Webhook # 'balance' => Balance, # 'balance_transaction' => BalanceTransaction, # 'charge' => Charge, # 'customer' => Customer, # 'invoiceitem' => InvoiceItem, # 'invoice' => Invoice, # 'plan' => Plan, # 'coupon' => Coupon, # 'event' => Event, # 'transfer' => Transfer, # 'recipient' => Recipient, # 'card' => Card, # 'subscription' => Subscription, # 'list' => ListObject, # 'application_fee' => ApplicationFee } end |
.symbolize_names(object) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/eventbrite/util.rb', line 46 def self.symbolize_names(object) case object when Hash new = {} object.each do |key, value| key = (key.to_sym rescue key) || key new[key] = symbolize_names(value) end new when Array object.map { |value| symbolize_names(value) } else object end end |
.url_encode(key) ⇒ Object
62 63 64 |
# File 'lib/eventbrite/util.rb', line 62 def self.url_encode(key) URI.escape(key.to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]")) end |