Created
October 12, 2015 06:20
-
-
Save supersupermomonga/fda74867a633bce1cb23 to your computer and use it in GitHub Desktop.
My7DzK_6fJTPmyz1FbJKEpMLo7O4tWOsV
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%s | |
<entry xmlns="http://www.w3.org/2005/Atom" xmlns:app="http://www.w3.org/2007/app"> | |
<title><?= title ?></title> | |
<author><name><?= author ?></name></author> | |
<content type="text/plain">%s</content> | |
<? if (updateAt) { ?> | |
<updated><?= Utilities.formatDate(updateAt, "JST", "yyyy-MM-dd'T'HH:mm:ss") ?></updated> | |
<? } ?> | |
<? for(var n in categories) { ?> | |
<category term="<?= categories[n] ?>" /> | |
<? } ?> | |
<app:control> | |
<app:draft><?= !draft ? 'no' : 'yes' ?></app:draft> | |
</app:control> | |
</entry> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
function getInstance(service, hatenaId, blogId) { | |
return new HatenaBlog_(service, hatenaId, blogId); | |
} | |
var HatenaBlog_ = (function() { | |
var validates = { | |
entryId: function(value) { | |
if (value == null) throw new Error('Missing required parameter'); | |
} | |
} | |
var getUrl = { | |
get: { | |
self: function(params) { | |
return this.root | |
}, | |
entries: function(params) { | |
var _params = params || {}; | |
return (_params.page !== undefined) ? Utilities.formatString('%s/entry?page=%s', this.root, params.page) : Utilities.formatString('%s/entry', this.root); | |
}, | |
categories: function(params) { | |
return Utilities.formatString('%s/category', this.root) | |
}, | |
entry: function(params) { | |
validates.entryId(params.entryId); | |
return Utilities.formatString('%s/entry/%s', this.root, params.entryId); | |
} | |
}, | |
post: { | |
entries: function(params) { | |
return Utilities.formatString('%s/entry', this.root); | |
} | |
}, | |
put: { | |
entry: function(params) { | |
validates.entryId(params.entryId); | |
return Utilities.formatString('%s/entry/%s', this.root, params.entryId); | |
} | |
}, | |
destroy: { | |
entry: function(params) { | |
validates.entryId(params.entryId); | |
return Utilities.formatString('%s/entry/%s', this.root, params.entryId); | |
} | |
} | |
}; | |
var createXml = function(params) { | |
var declaration = '<?xml version="1.0" encoding="utf-8"?>'; | |
var template = HtmlService.createTemplateFromFile('atom'); | |
template.title = params.title; | |
template.author = params.author || this.author; | |
template.updateAt = params.updateAt; | |
template.categories = params.categories; | |
template.draft = params.draft; | |
var evaluated = template.evaluate().getContent(); | |
return Utilities.formatString(evaluated, declaration, params.content); | |
} | |
function BlogService_(service, hatenaId, blogId) { | |
if (service === undefined) throw new Error('Service is required'); | |
this.service = service.getService(); | |
this.root = Utilities.formatString('https://blog.hatena.ne.jp/%s/%s/atom', hatenaId, blogId); | |
} | |
BlogService_.prototype.get = function(resource, params) { | |
if (!getUrl.get[resource]) throw new Error(JSON.stringify({message: 'undefined resource'})); | |
var response = this.service.fetch(getUrl.get[resource].call(this, params), { muteHttpExceptions: true }); | |
if (response.getResponseCode() !== 200) throw new Error(response); | |
return response; | |
} | |
BlogService_.prototype.post = function(resource, params) { | |
if (!getUrl.post[resource]) throw new Error(JSON.stringify({message: 'undefined resource'})); | |
var response = this.service.fetch(getUrl.post[resource].call(this, params), { | |
method: 'post', | |
contentType: 'application/atom+xml;type=entry', | |
payload: createXml.call(this, params), | |
muteHttpExceptions: true | |
}); | |
if (response.getResponseCode() !== 201) throw new Error(response); | |
return response; | |
} | |
BlogService_.prototype.put = function(resource, params) { | |
if (!getUrl.put[resource]) throw new Error(JSON.stringify({message: 'undefined resource'})); | |
var response = this.service.fetch(getUrl.put[resource].call(this, params), { | |
method: 'put', | |
contentType: 'application/atom+xml;type=entry', | |
payload: createXml.call(this, params), | |
muteHttpExceptions: true | |
}); | |
if (response.getResponseCode() !== 200) throw new Error(response); | |
return response; | |
} | |
BlogService_.prototype.destroy = function(resource, params) { | |
if (!getUrl.destroy[resource]) throw new Error(JSON.stringify({message: 'undefined resource'})); | |
var response = this.service.fetch(getUrl.destroy[resource].call(this, params), { method: 'delete', muteHttpExceptions: true }); | |
if (response.getResponseCode() !== 200) throw new Error(response); | |
return response; | |
} | |
return BlogService_; | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment