Skip to content

Commit

Permalink
Merge pull request #133 from elwayman02/br-serial
Browse files Browse the repository at this point in the history
DRY up how attributes are deserialized
  • Loading branch information
Dhaulagiri committed Feb 10, 2018
2 parents d162bd1 + e30a11d commit cec7646
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 127 deletions.
17 changes: 2 additions & 15 deletions addon/serializers/github-blob.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,3 @@
import GithubSerializer from './github';
import GithubSerializer from 'ember-data-github/serializers/github';

export default GithubSerializer.extend({
normalize(modelClass, resourceHash, prop) {
let normalizedHash = {
id: resourceHash.sha,
sha: resourceHash.sha,
url: resourceHash.url,
content: resourceHash.content,
encoding: resourceHash.encoding,
size: resourceHash.size
};

return this._super(modelClass, normalizedHash, prop);
}
});
export default GithubSerializer.extend({});
14 changes: 5 additions & 9 deletions addon/serializers/github-branch.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import GithubSerializer from './github';
import GithubSerializer from 'ember-data-github/serializers/github';

export default GithubSerializer.extend({
normalize(type, hash, prop) {
hash = {
id: hash.recordId || hash.commit.url.replace('https://api.github.com/repos/', '').replace(/\/commits\/.+/, `/branches/${hash.name}`),
name: hash.name,
commit: hash.commit,
protected: hash.protected
};
return this._super(type, hash, prop);
normalize(modelClass, resourceHash, prop) {
resourceHash.id = resourceHash.recordId || resourceHash.commit.url.replace('https://api.github.com/repos/', '').replace(/\/commits\/.+/, `/branches/${resourceHash.name}`);

return this._super(modelClass, resourceHash, prop);
}
});
19 changes: 7 additions & 12 deletions addon/serializers/github-organization.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
import GithubSerializer from './github';
import GithubSerializer from 'ember-data-github/serializers/github';

export default GithubSerializer.extend({
normalize(type, hash, prop) {
hash = {
id: hash.recordId || hash.login,
login: hash.login,
name: hash.name,
avatarUrl: hash.avatar_url,
links: {
users: hash.members_url.replace(/\{\/member\}/, ''),
repositories: hash.repos_url
}
normalize(modelClass, resourceHash, prop) {
resourceHash.id = resourceHash.recordId || resourceHash.login;
resourceHash.links = {
users: resourceHash.members_url.replace(/\{\/member\}/, ''),
repositories: resourceHash.repos_url
};
return this._super(type, hash, prop);
return this._super(modelClass, resourceHash, prop);
}
});
24 changes: 6 additions & 18 deletions addon/serializers/github-pull.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,12 @@
import GithubSerializer from './github';
import GithubSerializer from 'ember-data-github/serializers/github';

export default GithubSerializer.extend({
normalize(modelClass, resourceHash, prop) {
let hash = {
id: resourceHash.id,
number: resourceHash.number,
title: resourceHash.title,
state: resourceHash.state,
htmlUrl: resourceHash.html_url,
body: resourceHash.body,
createdAt: resourceHash.created_at,
updatedAt: resourceHash.updated_at,
closedAt: resourceHash.closed_at,
mergedAt: resourceHash.merged_at,
userAvatarUrl: resourceHash.user.avatar_url,
userLogin: resourceHash.user.login,
links: {
user: resourceHash.user.url
}
resourceHash.user_avatar_url = resourceHash.user.avatar_url;
resourceHash.user_login = resourceHash.user.login,
resourceHash.links = {
user: resourceHash.user.url
};
return this._super(modelClass, hash, prop);
return this._super(modelClass, resourceHash, prop);
}
});
7 changes: 1 addition & 6 deletions addon/serializers/github-release.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import GithubSerializer from './github';
import { decamelize } from '@ember/string';
import GithubSerializer from 'ember-data-github/serializers/github';

export default GithubSerializer.extend({
keyForAttribute(attr) {
return decamelize(attr);
},

normalize(modelClass, resourceHash, prop) {
resourceHash.links = {
user: resourceHash.author.url
Expand Down
30 changes: 9 additions & 21 deletions addon/serializers/github-repository.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,15 @@
import GithubSerializer from './github';
import GithubSerializer from 'ember-data-github/serializers/github';

export default GithubSerializer.extend({
normalize(modelClass, resourceHash, prop) {
let hash = {
id: resourceHash.recordId || resourceHash.full_name,
fullName: resourceHash.full_name,
name: resourceHash.name,
description: resourceHash.description,
htmlUrl: resourceHash.html_url,
language: resourceHash.language,
fork: resourceHash.fork,
private: resourceHash.private,
createdAt: resourceHash.created_at,
updatedAt: resourceHash.updated_at,
pushedAt: resourceHash.pushed_at,
links: {
owner: resourceHash.owner.url,
defaultBranch: `${resourceHash.url}/branches/${resourceHash.default_branch}`,
branches: `${resourceHash.url}/branches`,
pulls: `${resourceHash.url}/pulls`,
releases: `${resourceHash.url}/releases`
}
resourceHash.id = resourceHash.recordId || resourceHash.full_name,
resourceHash.links = {
owner: resourceHash.owner.url,
defaultBranch: `${resourceHash.url}/branches/${resourceHash.default_branch}`,
branches: `${resourceHash.url}/branches`,
pulls: `${resourceHash.url}/pulls`,
releases: `${resourceHash.url}/releases`
};
return this._super(modelClass, hash, prop);
return this._super(modelClass, resourceHash, prop);
}
});
36 changes: 16 additions & 20 deletions addon/serializers/github-tree.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import GithubSerializer from './github';
import GithubSerializer from 'ember-data-github/serializers/github';

export default GithubSerializer.extend({
normalize(modelClass, resourceHash, prop) {
Expand All @@ -7,24 +7,20 @@ export default GithubSerializer.extend({
let treeItems = resourceHash.tree
.filter(item => item.type === 'tree');

let normalizedHash = {
id: resourceHash.sha,
sha: resourceHash.sha,
url: resourceHash.url,
files: blobItems.reduce((files, blob) => {
files[blob.path] = blob.sha;
return files;
}, {}),
directories: treeItems.reduce((files, tree) => {
files[tree.path] = tree.sha;
return files;
}, {}),
truncated: resourceHash.truncated,
links: {
blobs: blobItems.map(blob => blob.url),
trees: treeItems.map(tree => tree.url)
}
};
return this._super(modelClass, normalizedHash, prop);
resourceHash.id = resourceHash.sha;
resourceHash.files = blobItems.reduce((files, blob) => {
files[blob.path] = blob.sha;
return files;
}, {});
resourceHash.directories = treeItems.reduce((files, tree) => {
files[tree.path] = tree.sha;
return files;
}, {});
resourceHash.links = {
blobs: blobItems.map(blob => blob.url),
trees: treeItems.map(tree => tree.url)
}

return this._super(modelClass, resourceHash, prop);
}
});
32 changes: 7 additions & 25 deletions addon/serializers/github-user.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import GithubSerializer from './github';
import GithubSerializer from 'ember-data-github/serializers/github';

export default GithubSerializer.extend({
normalizeResponse(store, primaryModelClass, payload, id, requestType) {
Expand All @@ -9,29 +9,11 @@ export default GithubSerializer.extend({
},

normalize(modelClass, resourceHash, prop) {
let normalizedHash = {
id: resourceHash.recordId || resourceHash.login,
login: resourceHash.login,
name: resourceHash.name,
type: resourceHash.type,
avatarUrl: resourceHash.avatar_url,
htmlUrl: resourceHash.html_url,
publicRepos: resourceHash.public_repos,
publicGists: resourceHash.public_gists,
followers: resourceHash.followers,
following: resourceHash.following,
createdAt: resourceHash.created_at,
updatedAt: resourceHash.updated_at,
url: resourceHash.url,
company: resourceHash.company,
blog: resourceHash.blog,
location: resourceHash.location,
email: resourceHash.email,
bio: resourceHash.bio,
links: {
repositories: resourceHash.repos_url
}
};
return this._super(modelClass, normalizedHash, prop);
resourceHash.id = resourceHash.recordId || resourceHash.login;
resourceHash.links = {
repositories: resourceHash.repos_url
}

return this._super(modelClass, resourceHash, prop);
}
});
4 changes: 4 additions & 0 deletions addon/serializers/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ import { get } from '@ember/object';
import { isNone } from '@ember/utils';
import { pluralize } from 'ember-inflector';
import DS from 'ember-data';
import { decamelize } from '@ember/string';

export default DS.RESTSerializer.extend({
keyForAttribute(attr) {
return decamelize(attr);
},

normalizeResponse(store, primaryModelClass, payload, id, requestType) {
payload.recordId = id;
Expand Down
3 changes: 2 additions & 1 deletion tests/dummy/mirage/factories/github-user.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ export default Factory.extend({
name: function(i) {
return `User ${i}`;
},
type: 'User',
// needs to be the actual related model name for Ember Data reasons? 🤔
type: 'github-user',
avatar_url: function(i) {
return `user${i}-avatar.gif`;
},
Expand Down

0 comments on commit cec7646

Please sign in to comment.