Permalink
Browse files

Merge branch 'develop'

  • Loading branch information...
2 parents 7e72a49 + bbc39f5 commit 6761a15c021bdda8bc7357a2c89a4c176c15e618 @luciferous luciferous committed Jul 15, 2011
Showing with 2,104 additions and 19 deletions.
  1. +1 −0 .gitignore
  2. +19 −19 LICENSE
  3. +38 −0 Makefile
  4. +29 −0 README
  5. +158 −0 Services/Twilio.php
  6. +78 −0 Services/Twilio/ArrayDataProxy.php
  7. +78 −0 Services/Twilio/AutoPagingIterator.php
  8. +163 −0 Services/Twilio/CachingDataProxy.php
  9. +44 −0 Services/Twilio/DataProxy.php
  10. +65 −0 Services/Twilio/Http.php
  11. +55 −0 Services/Twilio/InstanceResource.php
  12. +147 −0 Services/Twilio/ListResource.php
  13. +74 −0 Services/Twilio/Page.php
  14. +41 −0 Services/Twilio/PartialApplicationHelper.php
  15. +85 −0 Services/Twilio/Resource.php
  16. +26 −0 Services/Twilio/Rest/Account.php
  17. +10 −0 Services/Twilio/Rest/Accounts.php
  18. +6 −0 Services/Twilio/Rest/Application.php
  19. +12 −0 Services/Twilio/Rest/Applications.php
  20. +7 −0 Services/Twilio/Rest/AvailablePhoneNumber.php
  21. +30 −0 Services/Twilio/Rest/AvailablePhoneNumbers.php
  22. +18 −0 Services/Twilio/Rest/Call.php
  23. +14 −0 Services/Twilio/Rest/Calls.php
  24. +12 −0 Services/Twilio/Rest/Conference.php
  25. +6 −0 Services/Twilio/Rest/Conferences.php
  26. +6 −0 Services/Twilio/Rest/IncomingPhoneNumber.php
  27. +10 −0 Services/Twilio/Rest/IncomingPhoneNumbers.php
  28. +6 −0 Services/Twilio/Rest/Notification.php
  29. +6 −0 Services/Twilio/Rest/Notifications.php
  30. +6 −0 Services/Twilio/Rest/OutgoingCallerId.php
  31. +12 −0 Services/Twilio/Rest/OutgoingCallerIds.php
  32. +10 −0 Services/Twilio/Rest/Participant.php
  33. +6 −0 Services/Twilio/Rest/Participants.php
  34. +6 −0 Services/Twilio/Rest/Recording.php
  35. +6 −0 Services/Twilio/Rest/Recordings.php
  36. +6 −0 Services/Twilio/Rest/Sandbox.php
  37. +6 −0 Services/Twilio/Rest/ShortCode.php
  38. +6 −0 Services/Twilio/Rest/ShortCodes.php
  39. +6 −0 Services/Twilio/Rest/SmsMessage.php
  40. +24 −0 Services/Twilio/Rest/SmsMessages.php
  41. +6 −0 Services/Twilio/Rest/Transcription.php
  42. +6 −0 Services/Twilio/Rest/Transcriptions.php
  43. +25 −0 Services/Twilio/RestException.php
  44. +100 −0 Services/Twilio/Twiml.php
  45. +10 −0 TODO
  46. +109 −0 package.php
  47. +124 −0 package.xml
  48. +20 −0 tests/Bootstrap.php
  49. +115 −0 tests/ResourcesTest.php
  50. +219 −0 tests/TwilioTest.php
  51. +25 −0 tests/TwimlTest.php
  52. +7 −0 tests/phpunit.xml
View
@@ -0,0 +1 @@
+.*.swp
View
38 LICENSE
@@ -1,22 +1,22 @@
-Copyright (c) 2009 Twilio, Inc.
+MIT License
-Permission is hereby granted, free of charge, to any person
-obtaining a copy of this software and associated documentation
-files (the "Software"), to deal in the Software without
-restriction, including without limitation the rights to use,
-copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the
-Software is furnished to do so, subject to the following
-conditions:
+Copyright (C) 2011, Twilio, Inc. <help at twilio dot com>
+Copyright (C) 2011, Neuman Vong <neuman at twilio dot com>
-The above copyright notice and this permission notice shall be
-included in all copies or substantial portions of the Software.
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the "Software"), to deal in
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
+of the Software, and to permit persons to whom the Software is furnished to do
+so, subject to the following conditions:
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
-OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
-HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
-WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
-FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
-OTHER DEALINGS IN THE SOFTWARE.
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
View
@@ -0,0 +1,38 @@
+# Twilio API helper library.
+# See LICENSE file for copyright and license details.
+
+define LICENSE
+<?php
+
+/**
+ * Twilio API helper library.
+ *
+ * @category Services
+ * @package Services_Twilio
+ * @author Neuman Vong <neuman@twilio.com>
+ * @license http://creativecommons.org/licenses/MIT/ MIT
+ * @link http://pear.php.net/package/Services_Twilio
+ */
+endef
+export LICENSE
+
+all: test
+
+clean:
+ @rm -rf dist
+
+PHP_FILES = `find dist -name \*.php`
+dist: clean
+ @mkdir dist
+ @git archive master | (cd dist; tar xf -)
+ @for php in $(PHP_FILES); do\
+ echo "$$LICENSE" > $$php.new; \
+ tail -n+2 $$php >> $$php.new; \
+ mv $$php.new $$php; \
+ done
+
+test:
+ @echo running tests
+ @phpunit --configuration tests/phpunit.xml
+
+.PHONY: all clean dist test
View
29 README
@@ -0,0 +1,29 @@
+Twilio API helper library
+
+Installing
+----------
+
+Via PEAR:
+
+ $ pear install http://github.com/luciferous/services_twilio/tarball/v0.0.2
+
+Indenting
+---------
+
+To comply with PEAR standards, this project keeps an indent of 4-spaces
+(http://pear.php.net/manual/en/standards.indenting.php). Git attributes with
+smudge and clean filters can be used to configure an alternative indent.
+
+1. Create a .gitattributes file in the project root.
+
+ $ echo "*.php filter=tabspace" >> .gitattributes
+
+2. Set up smudge and clean filters. On check-out (i.e. smudge), the filter should
+convert a 2-space indent to 4-space indent. To indent using tabs, for example:
+
+ $ git config filter.tabspace.smudge 'unexpand -t4'
+
+And a corresponding filter should be set up code that's checked-in (i.e.
+clean):
+
+ $ git config filter.tabspace.clean 'expand -t4'
View
@@ -0,0 +1,158 @@
+<?php
+
+function Services_Twilio_autoload($className) {
+ if (substr($className, 0, 15) != 'Services_Twilio') {
+ return false;
+ }
+ $file = str_replace('_', '/', $className);
+ $file = str_replace('Services/', '', $file);
+ return include dirname(__FILE__) . "/$file.php";
+}
+
+spl_autoload_register('Services_Twilio_autoload');
+
+/**
+ * Twilio API client interface.
+ *
+ * @category Services
+ * @package Services_Twilio
+ * @author Neuman Vong <neuman@twilio.com>
+ * @license http://creativecommons.org/licenses/MIT/ MIT
+ * @link http://pear.php.net/package/Services_Twilio
+ */
+class Services_Twilio extends Services_Twilio_Resource
+{
+ protected $http;
+ protected $version;
+
+ /**
+ * Constructor.
+ *
+ * @param string $sid Account SID
+ * @param string $token Account auth token
+ * @param string $version API version
+ * @param Services_Twilio_Http $_http A HTTP client
+ */
+ public function __construct(
+ $sid,
+ $token,
+ $version = '2010-04-01',
+ $_http = null
+ ) {
+ $this->version = $version;
+ $this->http = (null === $_http)
+ ? new Services_Twilio_Http("https://$sid:$token@api.twilio.com")
+ : $_http;
+ $this->accounts = new Services_Twilio_Rest_Accounts($this);
+ $this->account = $this->accounts->get($sid);
+ }
+
+ /**
+ * GET the resource at the specified path.
+ *
+ * @param string $path Path to the resource
+ * @param array $params Query string parameters
+ *
+ * @return object The object representation of the resource
+ */
+ public function retrieveData($path, array $params = array())
+ {
+ $path = "/$this->version/$path.json";
+ return empty($params)
+ ? $this->_processResponse($this->http->get($path))
+ : $this->_processResponse(
+ $this->http->get("$path?" . http_build_query($params, '', '&'))
+ );
+ }
+
+ /**
+ * DELETE the resource at the specified path.
+ *
+ * @param string $path Path to the resource
+ * @param array $params Query string parameters
+ *
+ * @return object The object representation of the resource
+ */
+ public function deleteData($path, array $params = array())
+ {
+ $path = "/$this->version/$path.json";
+ return empty($params)
+ ? $this->_processResponse($this->http->delete($path))
+ : $this->_processResponse(
+ $this->http->delete("$path?" . http_build_query($params, '', '&'))
+ );
+ }
+
+ /**
+ * POST to the resource at the specified path.
+ *
+ * @param string $path Path to the resource
+ * @param array $params Query string parameters
+ *
+ * @return object The object representation of the resource
+ */
+ public function createData($path, array $params = array())
+ {
+ $path = "/$this->version/$path.json";
+ $headers = array('Content-Type' => 'application/x-www-form-urlencoded');
+ return empty($params)
+ ? $this->_processResponse($this->http->post($path, $headers))
+ : $this->_processResponse(
+ $this->http->post(
+ $path,
+ $headers,
+ http_build_query($params, '', '&')
+ )
+ );
+ }
+
+ /**
+ * Convert the JSON encoded resource into a PHP object.
+ *
+ * @param array $response 3-tuple containing status, headers, and body
+ *
+ * @return object PHP object decoded from JSON
+ */
+ private function _processResponse($response)
+ {
+ list($status, $headers, $body) = $response;
+ if ($status == 204) {
+ return TRUE;
+ }
+ if (empty($headers['content-type'])) {
+ throw new DomainException('Response header is missing Content-Type');
+ }
+ switch ($headers['content-type']) {
+ case 'application/json':
+ return $this->_processJsonResponse($status, $headers, $body);
+ break;
+ case 'text/xml':
+ return $this->_processXmlResponse($status, $headers, $body);
+ break;
+ }
+ throw new DomainException('Response was neither JSON nor XML');
+ }
+
+ private function _processJsonResponse($status, $headers, $body) {
+ $decoded = json_decode($body);
+ if (200 <= $status && $status < 300) {
+ return $decoded;
+ }
+ throw new Services_Twilio_RestException(
+ (int)$decoded->status,
+ $decoded->message,
+ isset($decoded->code) ? $decoded->code : null,
+ isset($decoded->more_info) ? $decoded->more_info : null
+ );
+ }
+
+ private function _processXmlResponse($status, $headers, $body) {
+ $decoded = simplexml_load_string($body);
+ throw new Services_Twilio_RestException(
+ (int)$decoded->Status,
+ (string)$decoded->Message,
+ (string)$decoded->Code,
+ (string)$decoded->MoreInfo
+ );
+ }
+}
@@ -0,0 +1,78 @@
+<?php
+
+/**
+ * DataProxy implementation using an array for storage backend.
+ *
+ * @category Services
+ * @package Services_Twilio
+ * @author Neuman Vong <neuman@twilio.com>
+ * @license http://creativecommons.org/licenses/MIT/ MIT
+ * @link http://pear.php.net/package/Services_Twilio
+ */
+class Services_Twilio_ArrayDataProxy
+ implements Services_Twilio_DataProxy
+{
+ protected $array;
+
+ /**
+ * Constructor.
+ *
+ * @param array $array Array representation
+ */
+ public function __construct($array)
+ {
+ $this->array = $array;
+ }
+
+ /**
+ * Implementation of retrieveData.
+ *
+ * @param string $key The index
+ * @param array $params Optional parameters
+ *
+ * @return object Object representation
+ */
+ function retrieveData($key, array $params = array())
+ {
+ return (object) $this->array;
+ }
+
+ /**
+ * Implementation of createData.
+ *
+ * @param string $key The index
+ * @param array $params Optional parameters
+ *
+ * @return object Object representation
+ */
+ function createData($key, array $params = array())
+ {
+ return (object) $this->array;
+ }
+
+ /**
+ * Implementation of updateData.
+ *
+ * @param array $params Update parameters
+ *
+ * @return object Object representation
+ */
+ function updateData(array $params)
+ {
+ return $this->array;
+ }
+
+ /**
+ * Implementation of magic method __get.
+ *
+ * @param string $prop The name of the property to get
+ *
+ * @return mixed The value of the property
+ */
+ function __get($prop)
+ {
+ return is_array($this->array)
+ ? $this->array['prop']
+ : $this->array->$prop;
+ }
+}
Oops, something went wrong.

0 comments on commit 6761a15

Please sign in to comment.