lib/mock/twilio/schemas/api_2010.rb in mock-twilio-0.2.0 vs lib/mock/twilio/schemas/api_2010.rb in mock-twilio-0.3.0
- old
+ new
@@ -1,12 +1,24 @@
# frozen_string_literal: true
+require_relative "../decorators/api_2010/messages"
+require_relative "../decorators/api_2010/calls"
+require_relative "../decorators/api_2010/conferences_participants_update"
+require_relative "../decorators/api_2010/conferences_participants_create"
+
module Mock
module Twilio
module Schemas
class Api2010
class << self
+ RESOURCES = {
+ messages: Mock::Twilio::Decorators::Api2010::Messages,
+ calls: Mock::Twilio::Decorators::Api2010::Calls,
+ conferences_participants_update: Mock::Twilio::Decorators::Api2010::ConferencesParticipantsUpdate,
+ conferences_participants_create: Mock::Twilio::Decorators::Api2010::ConferencesParticipantsCreate,
+ }
+
PAGES_KEYS = [
"end",
"first_page_uri",
"next_page_uri",
"last_page_uri",
@@ -17,38 +29,22 @@
"num_pages",
"start",
"uri"
].freeze
- def decorate(body, request)
- body["date_updated"] = Time.current.rfc2822 if body["date_updated"]
- body["date_sent"] = Time.current.rfc2822 if body["date_sent"]
- body["date_created"] = Time.current.rfc2822 if body["date_created"]
- body["start_time"] = Time.current.rfc2822 if body["start_time"]
- body["end_time"] = Time.current.rfc2822 if body["end_time"]
+ def for(body, request)
+ url = request.url.split(request.host).last
- message_sid(body, request) if body["sid"]
- pagination(body) if body["available_phone_numbers"]
-
- body
- end
-
- def pagination(body)
- # Params returned in mock_server but not on real twilio request for the moment.
- # Not needed for us now.
- PAGES_KEYS.each do |key|
- body.delete(key) if body[key]
+ case url
+ when %r{\/2010-04-01/Accounts/[A-Za-z0-9]+/Messages.json}
+ RESOURCES[:messages].decorate(body, request)
+ when %r{\/2010-04-01/Accounts/[A-Za-z0-9]+/Calls.json}
+ RESOURCES[:calls].decorate(body, request)
+ when %r{\/2010-04-01/Accounts/[A-Za-z0-9]+/Conferences/[A-Za-z0-9]+/Participants/[A-Za-z0-9]+.json}
+ RESOURCES[:conferences_participants_update].decorate(body, request)
+ when %r{\/2010-04-01/Accounts/[A-Za-z0-9]+/Conferences/[A-Za-z0-9]+/Participants.json}
+ RESOURCES[:conferences_participants_create].decorate(body, request)
end
- end
-
- def message_sid(body, request)
- prefix = request.data["MediaUrl"] ? "MM" : "SM"
- sid = prefix + SecureRandom.hex(16)
- scheduler = Rufus::Scheduler.new
- scheduler.in '2s' do
- Mock::Twilio::Webhooks::Messages.trigger(sid)
- end
- body["sid"] = sid
end
end
end
end
end