Source of file class-api.php

Size: 3,007 Bytes - Last Modified: 2016-01-05T15:40:14-05:00

../src/lib/class-api.php

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
Covered by 1 test(s):
  • WpWodify\ApiTest::test_get_api_key
5758596061626364656667
Covered by 1 test(s):
  • WpWodify\ApiTest::test_set_api_key
68
Covered by 1 test(s):
  • WpWodify\ApiTest::test_set_api_key
6970717273747576777879
Covered by 8 test(s):
  • WpWodify\ApiTest::test_get_api_uri with data set #0
  • WpWodify\ApiTest::test_get_api_uri with data set #1
  • WpWodify\ApiTest::test_get_api_uri with data set #2
  • WpWodify\ApiTest::test_get_api_uri with data set #3
  • WpWodify\ApiTest::test_get_api_uri with data set #4
  • WpWodify\ApiTest::test_get_api_uri with data set #5
  • WpWodify\ApiTest::test_get_api_uri with data set #6
  • WpWodify\ApiTest::test_get_api_uri with data set #7
80
Covered by 8 test(s):
  • WpWodify\ApiTest::test_get_api_uri with data set #0
  • WpWodify\ApiTest::test_get_api_uri with data set #1
  • WpWodify\ApiTest::test_get_api_uri with data set #2
  • WpWodify\ApiTest::test_get_api_uri with data set #3
  • WpWodify\ApiTest::test_get_api_uri with data set #4
  • WpWodify\ApiTest::test_get_api_uri with data set #5
  • WpWodify\ApiTest::test_get_api_uri with data set #6
  • WpWodify\ApiTest::test_get_api_uri with data set #7
81
Covered by 8 test(s):
  • WpWodify\ApiTest::test_get_api_uri with data set #0
  • WpWodify\ApiTest::test_get_api_uri with data set #1
  • WpWodify\ApiTest::test_get_api_uri with data set #2
  • WpWodify\ApiTest::test_get_api_uri with data set #3
  • WpWodify\ApiTest::test_get_api_uri with data set #4
  • WpWodify\ApiTest::test_get_api_uri with data set #5
  • WpWodify\ApiTest::test_get_api_uri with data set #6
  • WpWodify\ApiTest::test_get_api_uri with data set #7
82
Covered by 1 test(s):
  • WpWodify\ApiTest::test_get_api_uri with data set #7
8384
Covered by 7 test(s):
  • WpWodify\ApiTest::test_get_api_uri with data set #0
  • WpWodify\ApiTest::test_get_api_uri with data set #1
  • WpWodify\ApiTest::test_get_api_uri with data set #2
  • WpWodify\ApiTest::test_get_api_uri with data set #3
  • WpWodify\ApiTest::test_get_api_uri with data set #4
  • WpWodify\ApiTest::test_get_api_uri with data set #5
  • WpWodify\ApiTest::test_get_api_uri with data set #6
858687888990919293949596
Covered by 1 test(s):
  • WpWodify\ApiTest::test_get with data set #0
97
Covered by 1 test(s):
  • WpWodify\ApiTest::test_get with data set #0
98
Covered by 1 test(s):
  • WpWodify\ApiTest::test_get with data set #0
99
Covered by 1 test(s):
  • WpWodify\ApiTest::test_get with data set #0
100
Covered by 1 test(s):
  • WpWodify\ApiTest::test_get with data set #0
101
Covered by 1 test(s):
  • WpWodify\ApiTest::test_get with data set #0
102
Covered by 1 test(s):
  • WpWodify\ApiTest::test_get with data set #0
103
Covered by 1 test(s):
  • WpWodify\ApiTest::test_get with data set #0
104
Covered by 1 test(s):
  • WpWodify\ApiTest::test_get with data set #0
105
Covered by 1 test(s):
  • WpWodify\ApiTest::test_get with data set #0
106107108
<?php
/**
 * Api Class
 *
 * @category    PHP
 * @package     WpWodify
 * @subpackage  Api
 * @since       File available since release 1.0.x
 * @author      Cory Collier <corycollier@corycollier.com>
 */

namespace WpWodify;

/**
 * Api Class
 *
 * Encapsulation of all things related to connecting to the Wodify API.
 *
 * @category    PHP
 * @package     WpWodify
 * @subpackage  Api
 * @since       Class available since release 1.0.x
 * @author      Cory Collier <corycollier@corycollier.com>
 */
class Api {
    const ERR_API_NOT_AVAILABLE = 'The given api [%s], does not exist';

    /**
     * Stores the API key.
     *
     * @var string
     */
    protected $api_key;

    /**
     * A list of endpoints available.
     *
     * @var array
     */
    protected $endpoints = array(
        'classes'      => 'http://app.wodify.com/API/Classes_v1.aspx',
        'coaches'      => 'http://app.wodify.com/API/Coaches_v1.aspx',
        'leaderboards' => 'http://app.wodify.com/API/Leaderboard_v1.aspx',
        'results'      => 'http://app.wodify.com/API/LeaderboardResults_v1.aspx',
        'locations'    => 'http://app.wodify.com/API/Locations_v1.aspx',
        'programs'     => 'http://app.wodify.com/API/Programs_v1.aspx',
        'whitebaord'   => 'http://app.wodify.com/API/Whiteboard_v1.aspx',
    );

    /**
     * Getter for the api_key attribute.
     *
     * @return string The API key.
     */
    public function get_api_key() {
        return $this->api_key;
    }

    /**
     * Setter for the api_key attribute.
     *
     * @param string $api_key The value to set the api_key to.
     *
     * @return WpWodify\Api Returns $this for object-chaining.
     */
    public function set_api_key( $api_key ) {
        $this->api_key = $api_key;
        return $this;
    }

    /**
     * Gets the full uri for a given api name.
     *
     * @param $name The name of the api to get a uri for.
     *
     * @return string The full uri for the api.
     */
    public function get_api_uri( $name ) {
        $name = strtolower($name);
        $endpoint_exists = array_key_exists( $name, $this->endpoints );
        if ( false == $endpoint_exists ) {
            throw new Exception( sprintf(self::ERR_API_NOT_AVAILABLE, $name ) );
        }
        return $this->endpoints[ $name ];
    }

    /**
     * Gets a result for a given api.
     *
     * @param string $apiName The name of the api to connect to.
     * @param array $params Parameters to pass to build a query.
     *
     * @return string The result.
     */
    public function get( $api_name, $params ) {
        $api_key = $this->get_api_key();
        $uri     = $this->get_api_uri( $api_name );
        $data    = array_merge( $params, array(
            'apikey'   => esc_attr( $api_key ),
            'type'     => 'json',
            'encoding' => 'utf-8',
        ));
        $uri = sprintf( '%s?%s', $uri, http_build_query( $data ) );
        $result = wp_remote_get( $uri );
        return $result;
    }

}