Skip to content

Commit

Permalink
Rename Response class to Location.
Browse files Browse the repository at this point in the history
  • Loading branch information
naylonkessler committed Nov 9, 2017
1 parent d4db53a commit 192f657
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 41 deletions.
18 changes: 6 additions & 12 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ Just import, instantiate and call:
use IpApi\IpApi;

$api = new IpApi();
$response = $api->locate('ip or address');
$location = $api->locate('ip or address');
```

### The \IpApi\Response object
### The \IpApi\Location object

The response object has an schema returned composed by the following fields by default:
The location object has an schema returned composed by the following fields by default:

- status: Status of operation;
- country: Country of address;
Expand All @@ -42,18 +42,12 @@ The response object has an schema returned composed by the following fields by d
- as: The AS number and name;
- query: The address or IP used on query.

The response object has some others methods and features:
The location object has some others methods and features:

```php
// Convert the object to an array
$data = $response->toArray();

// Fill the object with received array data
$response->fill([
'propertyOne' => 'Value one',
'propertyTwo' => 'Value two',
]);
$data = $location->toArray();

// JSON encode
$json = json_encode($response);
$json = json_encode($location);
```
14 changes: 7 additions & 7 deletions src/IpApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ class IpApi
protected $endpoint = 'http://ip-api.com/json/';

/**
* Builds a response object from received parsed data.
* Builds a location object from received parsed data.
*
* @param mixed[] $data
* @return \IpApi\Response
* @return \IpApi\Location
*/
protected function buildResponse(array $data)
protected function buildLocation(array $data)
{
return new Response($data);
return new Location($data);
}

/**
Expand Down Expand Up @@ -55,16 +55,16 @@ protected function fetch($url)
* Locates the received address over api.
*
* @param string $address
* @return \IpApi\Response
* @return \IpApi\Location
*/
public function locate($address)
{
$url = $this->buildUrl($address);
$data = $this->fetch($url);
$parsed = $this->parse($data);
$response = $this->buildResponse($parsed);
$location = $this->buildLocation($parsed);

return $response;
return $location;
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/Response.php → src/Location.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
namespace IpApi;

/**
* Class Response.
* Class Location.
*
* The abstraction of a response fetched from IpApi api.
*
* @package IpApi
*/
class Response implements \JsonSerializable
class Location implements \JsonSerializable
{
/**
* Response attributes collection.
* Location attributes collection.
*
* @var mixed[]
*/
Expand Down
12 changes: 6 additions & 6 deletions tests/IpApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ class IpApiTest extends TestCase
public function testLocateReturnType()
{
$api = new \IpApi\IpApi();
$result = $api->locate('8.8.8.8');
$location = $api->locate('8.8.8.8');

$this->assertInstanceOf(\IpApi\Response::class, $result);
$this->assertInstanceOf(\IpApi\Location::class, $location);
}

public function testResponseOnSuccess()
{
$api = new \IpApi\IpApi();
$result = $api->locate('8.8.8.8');
$location = $api->locate('8.8.8.8');

$this->assertEquals('United States', $result->country);
$this->assertEquals('California', $result->regionName);
$this->assertEquals('Mountain View', $result->city);
$this->assertEquals('United States', $location->country);
$this->assertEquals('California', $location->regionName);
$this->assertEquals('Mountain View', $location->city);
}
}
26 changes: 13 additions & 13 deletions tests/ResponseTest.php → tests/LocationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,55 @@

use PHPUnit\Framework\TestCase;

class ResponseTest extends TestCase
class LocationTest extends TestCase
{
public function testSetting()
{
$response = new \IpApi\Response();
$response->someProp = 'some value';
$location = new \IpApi\Location();
$location->someProp = 'some value';

$this->assertAttributeEquals(['someProp' => 'some value'], 'attributes', $response);
$this->assertAttributeEquals(['someProp' => 'some value'], 'attributes', $location);
}

public function testGetting()
{
$response = new \IpApi\Response();
$response->someProp = 'some value';
$location = new \IpApi\Location();
$location->someProp = 'some value';

$this->assertEquals('some value', $response->someProp);
$this->assertEquals('some value', $location->someProp);
}

public function testFilling()
{
$response = new \IpApi\Response([
$location = new \IpApi\Location([
'propOne' => 'value one',
'propTwo' => 'value two',
]);

$this->assertAttributeEquals([
'propOne' => 'value one',
'propTwo' => 'value two',
], 'attributes', $response);
], 'attributes', $location);
}

public function testToArray()
{
$response = new \IpApi\Response([
$location = new \IpApi\Location([
'propOne' => 'value one',
'propTwo' => 'value two',
]);

$this->assertEquals([
'propOne' => 'value one',
'propTwo' => 'value two',
], $response->toArray());
], $location->toArray());
}

public function testToJson()
{
$response = new \IpApi\Response(['propOne' => 'value one']);
$location = new \IpApi\Location(['propOne' => 'value one']);

$json = json_encode($response);
$json = json_encode($location);
$parsed = json_decode($json, true);

$this->assertEquals(['propOne' => 'value one'], $parsed);
Expand Down

0 comments on commit 192f657

Please sign in to comment.