Created
June 26, 2009 15:26
-
-
Save teramako/136559 to your computer and use it in GitHub Desktop.
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
let PLUGIN_INFO = | |
<VimperatorPlugin> | |
<name>{NAME}</name> | |
<description>get expanded or shorten URL with bit.ly</description> | |
<author mail="[email protected]" homepage="http://d.hatena.ne.jp/teramako/">teramako</author> | |
<version>0.6.3</version> | |
<minVersion>2.0</minVersion> | |
<maxVersion>2.2a1pre</maxVersion> | |
<detail lang="ja"><![CDATA[ | |
http://bit.ly/ のAPIを使用して短いURLを得たり、元のURLを得るためのコード | |
今のところコマンドを提供する気がない | |
== 初めに == | |
まず、最初に.vimperatorrc等に以下のような定義をしてください。 | |
>|| | |
let g:bitly_user = "user id" | |
let g:bitly_apiKey = "API Key" | |
||< | |
http://bit.ly/ でアカウントを取ることで得ることが可能です。 | |
== 使い方 == | |
>|| | |
:echo bitly.shorten(buffer.URL) | |
:echo bitly.expand("http://bit.lyt/XXXX") | |
||< | |
== 応用例 == | |
copy.js のテンプレートに | |
>|| | |
liberator.globalVariables.copy_templates = [ | |
{ label: 'bitly', value: 'Get bitly URL', cutom: function(){ return bitly.shorten(buffer.URL); } } | |
] | |
||< | |
などと入れておけば、:copy bitly で短いURLをクリップボードにコピーすることが可能 | |
]]></detail> | |
</VimperatorPlugin>; | |
if (liberator.globalVariables.bitly_apiKey && liberator.globalVariables.bitly_user){ | |
modules.bitly = (function(){ | |
const baseURL = "http://api.bit.ly/"; | |
const version = "2.0.1"; | |
let JSON = Cc["@mozilla.org/dom/json;1"].getService(Ci.nsIJSON); | |
function getURL(method, url){ | |
let user = self.user, key = self.apiKey; | |
if (!user || !key){ | |
throw new Error("user or apiKey is not defined"); | |
} | |
let params = [ | |
["version", version], | |
["login", user], | |
["apiKey", key] | |
]; | |
switch(method){ | |
case "shorten": | |
params.push(["longUrl", url]); | |
break; | |
case "expand": | |
params.push(["shortUrl", url]); | |
break; | |
default: | |
throw new Error(method + " is not support"); | |
} | |
return baseURL + method + "?" + params.map(function(param) param.join("=")).join("&"); | |
} | |
function validate(obj){ | |
return (obj.statusCode == "OK") ? true : false; | |
} | |
let self = { | |
shorten: function(url){ | |
let res = JSON.decode(util.httpGet(getURL("shorten", url)).responseText); | |
if (validate(res)){ | |
return res.results[url].shortUrl; | |
} | |
liberator.echoerr("bit.ly shorten: " + res.errorCode + ", " + res.errorMessage); | |
return url; | |
}, | |
expand: function(url){ | |
let res = JSON.decode(util.httpGet(getURL("expand", url)).responseText); | |
if (validate(res)){ | |
return res.results[url.split("/").pop()].longUrl; | |
} | |
liberator.echoerr("bit.ly expand: " + res.errorCode + ", " + res.errorMessage); | |
return url; | |
}, | |
get apiKey() liberator.globalVariables.bitly_apiKey, | |
set apiKey(key) { | |
liberator.globalVariables.bitly_apiKey = key; | |
return key; | |
}, | |
get user() liberator.globalVariables.bitly_user, | |
set user(name){ | |
liberator.globalVariables.bitly_user = name; | |
return name; | |
}, | |
}; | |
return self; | |
})(); | |
} else { | |
liberator.echoerr('Please `let g:bitly_user = "xxx" and let g:bitly_apiKey = "xxxx"\''); | |
} | |
// vim: sw=2 ts=2 et fdm=marker: |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment