Skip to content

Instantly share code, notes, and snippets.

@supersupermomonga
Created October 11, 2015 16:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save supersupermomonga/ccf41065f36fce7ed125 to your computer and use it in GitHub Desktop.
Save supersupermomonga/ccf41065f36fce7ed125 to your computer and use it in GitHub Desktop.
'use strict';
function getInstance(consumer_key, consumer_secret, scope) {
return new HatenaBookmark_(consumer_key, consumer_secret, scope);
}
var HatenaBookmark_ = (function() {
var root = 'http://api.b.hatena.ne.jp';
var validates = {
url: function(value) {
var regexp = /^(https?|ftp)(:\/\/[-_.!~*\'()a-zA-Z0-9;\/?:\@&=+\$,%#]+)$/;
if (!regexp.test(value)) throw new Error(JSON.stringify({message: 'url is invalid'}));
}
}
var getUrl = {
get: {
bookmark: function(params) {
validates.url(params.url);
return root + Utilities.formatString('/1/my/bookmark?url=%s', encodeURIComponent(params.url));
},
entry: function(params) {
validates.url(params.url);
return root + Utilities.formatString('/1/entry?url=%s', encodeURIComponent(params.url));
},
tags: function(params) {
return root + '/1/my/tags';
},
my: function(params) {
return root + '/1/my';
}
},
post: {
bookmark: function(params) {
validates.url(params.url);
return root + '/1/my/bookmark';
}
},
destroy: {
bookmark: function(params) {
validates.url(params.url);
return root + Utilities.formatString('/1/my/bookmark?url=%s', encodeURIComponent(params.url));
}
}
}
function InnerHatenaBookmark(consumer_key, consumer_secret, scope) {
this.consumer_key = consumer_key;
this.consumer_secret = consumer_secret;
this.scope = scope;
}
InnerHatenaBookmark.prototype.getService = function() {
return OAuth1.createService('Hatena')
.setAccessTokenUrl('https://www.hatena.com/oauth/token')
.setRequestTokenUrl('https://www.hatena.com/oauth/initiate?scope='+this.scope)
.setAuthorizationUrl('https://www.hatena.ne.jp/oauth/authorize')
.setConsumerKey(this.consumer_key)
.setConsumerSecret(this.consumer_secret)
.setCallbackFunction('authCallback')
.setPropertyStore(PropertiesService.getUserProperties())
}
InnerHatenaBookmark.prototype.authorize = function() {
var service = this.getService();
if (service.hasAccess()) {
Logger.log('Already authorized');
} else {
var authorizationUrl = service.authorize();
Logger.log('Open the following URL and re-run the script: %s', authorizationUrl);
}
}
InnerHatenaBookmark.prototype.reset = function() {
var service = this.getService();
service.reset();
}
InnerHatenaBookmark.prototype.authCallback = function(request) {
var service = this.getService();
var isAuthorized = service.handleCallback(request);
var mimeType = ContentService.MimeType.TEXT;
if (isAuthorized) {
return ContentService.createTextOutput('Success').setMimeType(mimeType);
} else {
return ContentService.createTextOutput('Denied').setMimeType(mimeType);
}
}
InnerHatenaBookmark.prototype.get = function(resource, params) {
if (!getUrl.get[resource]) throw new Error(JSON.stringify({message: 'undefined resource'}));
var service = this.getService();
var response = service.fetch(getUrl.get[resource](params), { muteHttpExceptions: true });
if (response.getResponseCode() !== 200) throw new Error(response);
return response;
}
InnerHatenaBookmark.prototype.post = function(resource, params) {
if (!getUrl.post[resource]) throw new Error(JSON.stringify({message: 'undefined resource'}));
var service = this.getService();
var response = service.fetch(getUrl.post[resource](params), { method: 'post', payload: params, muteHttpExceptions: true });
if (response.getResponseCode() !== 200) throw new Error(response);
return response;
}
InnerHatenaBookmark.prototype.destroy = function(resource, params, success, failure) {
if (!getUrl.destroy[resource]) throw new Error(JSON.stringify({message: 'undefined resource'}));
var service = this.getService();
var response = service.fetch(getUrl.destroy[resource](params), { method: 'delete', muteHttpExceptions: true });
if (response.getResponseCode() !== 204) throw new Error(response);
return JSON.stringify({message: 'success'});
}
return InnerHatenaBookmark;
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment