Skip to content
This repository was archived by the owner on Oct 29, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added ._dosomething_northstar.install
Binary file not shown.
14 changes: 14 additions & 0 deletions dosomething_northstar.install
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
/**
* @file
* Installation and schema hooks for dosomething_northstar.module.
*/

/**
* Implements hook_schema().
*/
function dosomething_northstar_user_info_schema() {
$schema['cache_northstar_user_info'] = drupal_get_schema_unprocessed('dosomething_northstar', 'cache_northstar_user_info');
return $schema;
}

Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,10 @@ protected function transformReportbackItem($data) {
protected function transformUser($data) {
return array(
'id' => $data->id,
'first_name' => $data->first_name,
'last_name' => $data->last_name,
'photo' => $data->photo,
'country' => $data->country,
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,26 @@ function dosomething_northstar_update_user($form_state) {

}

/**
*Get user data from northstar.
*/
function dosomething_northstar_get_northstar_user($id) {
if (!empty($northstar_user_info)) {
$northstar_user_info = cache_get('northstar_user_info_' . $id, 'cache_northstar_user_info');
} else {
$client = _dosomething_northstar_build_http_client();

$northstar_user_info = drupal_http_request($client['base_url'] . '/users/_id/' . $id, array(
'headers' => $client['headers'],
'method' => 'GET',
));

cache_set('northstar_user_info_' . $id, $northstar_user_info, 'cache_northstar_user_info', REQUEST_TIME + 60*60*24);
}

return $northstar_user_info;
}

/**
* Build a user json object, that's accepted by Northstar.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,17 @@ private function build($data) {
'verb' => $data->verb,
],
];

$northstar_user = dosomething_northstar_get_northstar_user($data->uid);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you json_decode the whole thing here, so you don't have to do it for each thing below?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated!

$northstar_user = json_decode($northstar_user->data, true);
$northstar_user = (object) $northstar_user['data'][0];

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not vital at all (or maybe just a future update to consider), but might be cleaner if you could save the nested array to $northstar_user and then cast it to an object:

$northstar_user = json_decode($northstar_user->data, true);
$northstar_user = (object) $northstar_user['data'][0];

So then you can get each item similar to how code above retrieves it for consistency:

$this->user = [
  'id' => $data->uid,
  'first_name' => $northstar_user->first_name,
  'last_name' => $northstar_user->last_name,
  'photo' => $northstar_user->photo,
  'country' => $northstar_user->country,
];

Something like that... :)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that looks a lot better, I'll update now, thanks Diego!

$this->user = [
'id' => $data->uid,
'first_name' => $northstar_user->first_name,
'last_name' => $northstar_user->last_name,
'photo' => $northstar_user->photo,
'country' => $northstar_user->country,
];
}

Expand Down