Skip to content

Commit

Permalink
implements Extra::userinfo()
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelp committed Mar 8, 2015
1 parent d62ef3e commit 16d4efb
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 12 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,14 @@ foreach($transactions as $transaction) {
}
```

### Extras

```php
// Dados do usuário logado
$userinfo = BoletoSimples\Extra::userinfo();
```


## Desenvolvendo

Instale as dependências
Expand Down
10 changes: 10 additions & 0 deletions src/BoletoSimples/Extra.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace BoletoSimples;

class Extra {
public static function userinfo() {
$response = BaseResource::sendRequest('GET', 'userinfo');
return $response->json();
}
}
24 changes: 12 additions & 12 deletions src/BoletoSimples/Resources/BaseResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public static function methodFor($action) {
return $methods[$action];
}

public static function _find($id) {
private static function _find($id) {
if (!$id) {
throw new \Exception("Couldn't find ".get_called_class()." without an ID.");
}
Expand All @@ -124,14 +124,14 @@ public static function _find($id) {
return $object;
}

public static function _create($attributes = array()) {
private static function _create($attributes = array()) {
$class = get_called_class();
$object = new $class($attributes);
$object->save();
return $object;
}

public static function _all($params = array()) {
private static function _all($params = array()) {
$class = get_called_class();
$response = self::sendRequest('GET', $class::element_name_plural(), ['query' => $params]);
$collection = [];
Expand All @@ -141,15 +141,7 @@ public static function _all($params = array()) {
return $collection;
}

public static function element_name() {
return Util::underscorize(get_called_class());
}

public static function element_name_plural() {
return Util::pluralize(self::element_name());
}

public static function sendRequest($method, $path, $options = []) {
private static function _sendRequest($method, $path, $options = []) {
$options = array_merge(self::$default_options, $options);
$request = self::$client->createRequest($method, $path, $options);
$response = self::$client->send($request);
Expand All @@ -160,6 +152,14 @@ public static function sendRequest($method, $path, $options = []) {
return $response;
}

public static function element_name() {
return Util::underscorize(get_called_class());
}

public static function element_name_plural() {
return Util::pluralize(self::element_name());
}

public static function __callStatic($name, $arguments) {
self::configure();
return call_user_func_array("self::_".$name, $arguments);
Expand Down
21 changes: 21 additions & 0 deletions tests/BoletoSimples/ExtraTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

class ExtraTest extends PHPUnit_Framework_TestCase {
/**
* @before
*/
public function setupSomeFixtures()
{
BoletoSimples::configure(['environment' => 'sandbox']);
}

/**
* @vcr extra/userinfo
* ATENÇÃO: Após apagar a fixture extra/userinfo e rodar o teste denovo,
* edite o arquivo e remova o token do login_url por questão de segurança.
*/
public function testUserinfo() {
$this->subject = BoletoSimples\Extra::userinfo();
$this->assertEquals(array_keys($this->subject), ["id","login_url","email","account_type","first_name","middle_name","last_name","full_name","cpf","date_of_birth","mother_name","father_name","account_level","phone_number","address_street_name","address_number","address_complement","address_neighborhood","address_postal_code","address_city_name","address_state","business_name","business_cnpj","business_legal_name"]);
}
}
34 changes: 34 additions & 0 deletions tests/fixtures/extra/userinfo
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

-
request:
method: GET
url: 'https://sandbox.boletosimples.com.br/api/v1/userinfo'
headers:
Host: sandbox.boletosimples.com.br
Accept-Encoding: null
Content-Type: application/json
User-Agent: 'BoletoSimples PHP Client v0.0.2 ([email protected])'
Authorization: 'Bearer BOLETOSIMPLES_ACCESS_TOKEN'
Accept: null
response:
status:
http_version: '1.1'
code: '200'
message: OK
headers:
Server: Cowboy
Connection: keep-alive
Strict-Transport-Security: max-age=2592000
Content-Type: 'application/json; charset=utf-8'
X-Ratelimit-Limit: '500'
X-Ratelimit-Remaining: '496'
Etag: 'W/"2d2f54e118a16fc72383e7533ebcfeb1"'
Cache-Control: 'must-revalidate, private, max-age=0'
X-Request-Id: f3a2b980-ba43-4b51-a61f-81b5f5159c29
X-Runtime: '0.072303'
Date: 'Sun, 08 Mar 2015 18:14:55 GMT'
X-Rack-Cache: miss
Vary: Accept-Encoding
Transfer-Encoding: chunked
Via: '1.1 vegur'
body: '{"id":53,"login_url":"https://sandbox.boletosimples.com.br/welcome?email=user1%40example.com\u0026token=xxx","email":"[email protected]","account_type":null,"first_name":null,"middle_name":null,"last_name":null,"full_name":null,"cpf":null,"date_of_birth":null,"mother_name":null,"father_name":null,"account_level":2,"phone_number":null,"address_street_name":null,"address_number":null,"address_complement":null,"address_neighborhood":null,"address_postal_code":null,"address_city_name":null,"address_state":null,"business_name":null,"business_cnpj":null,"business_legal_name":null}'

0 comments on commit 16d4efb

Please sign in to comment.