{"id":2304,"date":"2011-02-14T23:58:00","date_gmt":"2011-02-14T14:58:00","guid":{"rendered":"https:\/\/hdmr.org\/d\/?e=1033"},"modified":"2011-02-14T23:58:00","modified_gmt":"2011-02-14T14:58:00","slug":"blogn-1033","status":"publish","type":"post","link":"https:\/\/hdmr.org\/d\/?e=1033","title":{"rendered":"sinatra\u3064\u304b\u3063\u3066\u8df3.jp\u3068p.tl\u3092bit.ly\u3068j.mp\u306b\u5316\u3051\u3055\u305b\u308b"},"content":{"rendered":"<p>\u6628\u65e5\u3001bit.ly\u306eAPI\u306e\u3075\u308a\u3092\u3059\u308b\u77ed\u7e2eURL\u3092\u4f5c\u3063\u3066\u3075\u3068\u6c17\u4ed8\u3044\u305f\u306e\u3060\u304c\u3001<\/p>\n<p>\u3042\u307e\u308a\u30e1\u30b8\u30e3\u30fc\u3067\u306a\u3044\u305f\u3081\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u304c\u5bfe\u5fdc\u3057\u3066\u3044\u306a\u3044\u77ed\u7e2eURL\u3082\u3053\u306e\u65b9\u6cd5\u3067\u4f7f\u3044\u3084\u3059\u304f\u306a\u308b\u306e\u3067\u306f\u306a\u3044\u304b\u3068\u8003\u3048\u305f\u3002<\/p>\n<p>\u3053\u3053\u3067\u30bf\u30fc\u30b2\u30c3\u30c8\u306b\u3057\u305f\u306e\u306f\u3001<a href=\"http:\/\/\u8df3.jp\">\u8df3.jp<\/a> \u3068 <a href=\"http:\/\/p.tl\">p.tl<\/a> \u3060\u3002<\/p>\n<hr \/>\n<p>\u8df3.jp\u306f\u65e5\u672c\u8a9e\u30c9\u30e1\u30a4\u30f3\u3092\u4f7f\u3063\u305f\u77ed\u7e2eURL\u3067\u975e\u5e38\u306b\u77ed\u304f\u306a\u308b\u306e\u304c\u7279\u5fb4\u3002 <\/p>\n<p><a title=\"http:\/\/\u4e01\u55df.\u8df3.jp\" href=\"http:\/\/\u4e01\u55df.\u8df3.jp\">http:\/\/\u4e01\u55df.\u8df3.jp<\/a> \u306e\u3088\u3046\u306b\u306a\u308b\u3002\u78ba\u304b\u306b\u77ed\u3044<\/p>\n<p>p.tl\u306fPixiv\u304c\u63d0\u4f9b\u3057\u3066\u3044\u308b\u77ed\u7e2eURL\u30b5\u30fc\u30d3\u30b9\u3067\u3001\u4e3b\u306bPixiv\u306e\u6295\u7a3f\u753b\u50cf\u306b\u3064\u3044\u3066Twitter\u306bPost\u3059\u308b\u3068\u304d\u306b\u4f7f\u3046\u3002<\/p>\n<p>\u3051\u3069\u305d\u308c\u4ee5\u5916\u306b\u4f7f\u3063\u3066\u3044\u3051\u306a\u3044\u6c7a\u307e\u308a\u306f\u306a\u3044\u3057\u3001\u3061\u3083\u3093\u3068API\u3082\u63d0\u4f9b\u3055\u308c\u3066\u3044\u308b\u3002<\/p>\n<p><a title=\"http:\/\/p.tl\/3WuT\" href=\"http:\/\/p.tl\/3WuT\">http:\/\/p.tl\/3WuT<\/a> \u306e\u3088\u3046\u306b\u306a\u308b\u3002\u5229\u7528\u8005\u304c\u5c11\u306a\u3044\u304b\u3089\u304b\u77ed\u3044\u3002\u306a\u3093\u304b\u6628\u65e5\u4f5c\u3063\u305f\u30b5\u30fc\u30d3\u30b9\u304c\u8ca0\u3051\u305f\u6c17\u306b\u306a\u308b\u3002<\/p>\n<p>\u305f\u3060\u3001\u3069\u3061\u3089\u3082\u6bd4\u8f03\u7684\u65b0\u3057\u3044\u30b5\u30fc\u30d3\u30b9\u306a\u306e\u3067\u306a\u304b\u306a\u304b\u5bfe\u5fdc\u3057\u3066\u3044\u306a\u3044\u3002<\/p>\n<p>\u3068\u3044\u3046\u308f\u3051\u3067bit.ly\u306eAPI\u306e\u3075\u308a\u3092\u3059\u308b\u30b5\u30fc\u30d0\u30fc\u3092localhost\u306b\u4f5c\u308d\u3046\u3068\u3044\u3046\u308f\u3051\u3060\u3002<\/p>\n<p>bit.ly\u3068j.mp\u306f\u540c\u3058API\u3092\u4f7f\u3063\u3066\u3044\u308b\u306e\u3067\u3001\u4f7f\u3044\u5206\u3051\u3082\u3067\u304d\u308b\u304b\u3082\u3057\u308c\u306a\u3044\u3002<\/p>\n<p>Ruby\u306esinatra\u3092\u4f7f\u3063\u3066\u4f5c\u3063\u3066\u307f\u305f\u3002<\/p>\n<hr \/>\n<p> \u307e\u305agem\u3067\u3044\u308d\u3044\u308d\u5165\u308c\u305f\u3002  <\/p>\n<pre>gem install sinatra\ngem install json\ngem install rubygems-update\nupdate_rubygems\ngem install activesupport<\/pre>\n<p>\u305d\u3057\u3066\u3001sinatra\u3067\u30b5\u30fc\u30d0\u30fc\u3092\u4f5c\u3063\u305f\u3002\u305d\u306e\u524d\u306bIIS\u3092\u5207\u3063\u3066\u304a\u304f\u5fc5\u8981\u304c\u3042\u3063\u305f\u304c\u3002<\/p>\n<pre># -*- coding: utf-8 -*-\nrequire 'rubygems'\nrequire 'sinatra\/base'\nrequire 'json'\nrequire 'net\/http'\nrequire 'active_support\/all'\nNet::HTTP.version_1_2\nclass MyApp &lt; Sinatra::Base\n  @@jmp=:tobi; @@bitly=:tobi; #:tobi\u307e\u305f\u306f:ptl\n  @@ptlkey='*******';\n  def tobi(url)\n    Net::HTTP.start('xn--vt3a.jp',80) {|http|\n      response = http.get(\"\/api?url=#{url}\")\n      response.body.force_encoding('utf-8')\n    }\n  end\n  def ptl(url)\n    Net::HTTP.start('p.tl',80) {|http|\n      response = http.get(\"\/api\/api_simple.php?key=#{@@ptlkey}&amp;url=#{url}\")\n      res=JSON::parse(response.body)\n      res['status']=='ok' ? res['short_url'] : nil\n    }\n  end\n  def shorten(params)\n    shorten=request.host=='api.j.mp'?self.__send__(@@jmp,params[:longUrl]):self.__send__(@@bitly,params[:longUrl])\n    p shorten\n    if shorten\n      if params[:version]=='2.0.1'\n        res={'errorCode'=&gt;0,'errorMessage'=&gt;'','result'=&gt;{\n             params[:longUrl]=&gt;{'userHash'=&gt;0,'hash'=&gt;0,'shortUrl'=&gt;shorten,'shortCNAMEUrl'=&gt;shorten,\n                                'shortKeywordUrl'=&gt;''}},'statusCode'=&gt;'OK'};\n      else\n        res={'status_code'=&gt;200,'status_txt'=&gt;'OK','data'=&gt;{'hash'=&gt;0,'global_hash'=&gt;0,'url'=&gt;shorten,\n                                                          'long_url'=&gt;params[:longUrl],'new_hash'=&gt;0}};\n      end\n      if params[:format]=='xml'\n        if params[:login]=='twit'  #twit\u306f\u306a\u305c\u304bShift_JIS\u3067\u89e3\u91c8\u3059\u308b\n          res.to_xml(:root=&gt;'response',:skip_types=&gt;true,:dasherize=&gt;false,:skip_instruct=&gt;true).\n            gsub(\/&amp;#(?:(\\d*?)|(?:[xX]([0-9a-fA-F]{4})));\/){[$1.nil? ? $2.to_i(16) : $1.to_i].pack('U')}.\n            encode('shift_jis')\n        else\n          res.to_xml(:root=&gt;'response',:skip_types=&gt;true,:dasherize=&gt;false).\n            gsub(\/&amp;#(?:(\\d*?)|(?:[xX]([0-9a-fA-F]{4})));\/){[$1.nil? ? $2.to_i(16) : $1.to_i].pack('U')}\n        end\n      else\n        res.to_json.gsub(\/\\\\u(\\h{4})\/){[$1.to_i(16)].pack('U')}.gsub(\/\\\\\\\/\/,'\/').gsub('\":','\": ')\n      end\n    end\n  end\n  get  '\/shorten' do\n    shorten(params);\n  end\n  post '\/shorten' do\n    shorten(params);\n  end\n  get  '\/v3\/shorten' do\n    shorten(params);\n  end\n  post '\/v3\/shorten' do\n    shorten(params);\n  end\nend\nMyApp.run! :host =&gt; 'localhost', :port =&gt; 80<\/pre>\n<p>\u3053\u308c\u3067OK\u3068\u3044\u3046\u306e\u306f\u4e0d\u601d\u8b70\u306a\u611f\u3058\u3060\u304c\u3001\u305d\u308c\u304csinatra\u3060\u3002<\/p>\n<p>\u3042\u3068hosts\u306bbit.ly\u3068j.mp\u3092localhost\u306b\u3057\u3066\u304a\u304f\u3088\u3046\u66f8\u3044\u3066\u304a\u304b\u306a\u3044\u3068\u5b9f\u9a13\u3067\u304d\u3093\u306a\u3002<\/p>\n<pre>127.0.0.1 api.bit.ly api.j.mp<\/pre>\n<hr \/>\n<p>\u4e0b\u306bget &#8216;\/shorten&#8217; \u306a\u3069\u4e26\u3093\u3067\u3044\u308b\u304c\u3001\u3053\u308c1\u30641\u3064\u304c\u30a2\u30af\u30bb\u30b9\u3092\u53d7\u3051\u308b\u30d6\u30ed\u30c3\u30af\u3060\u3002<\/p>\n<p>\u3053\u306e\u30d6\u30ed\u30c3\u30af\u3067\u53d7\u3051\u3066\u3068\u3084\u304b\u304f\u51e6\u7406\u3057\u3066\u30ec\u30b9\u30dd\u30f3\u30b9\u3092\u8fd4\u3059\u3002\u305d\u308c\u3060\u3051\u3067\u4f5c\u308c\u308b\u3002<\/p>\n<p>ruby sinatobi.rb \u306a\u3069\u3059\u308c\u3070\u5f85\u3061\u53d7\u3051\u72b6\u614b\u306b\u306a\u308b\u3002\u305d\u3057\u3066Ctrl+C\u3067\u6b62\u307e\u308b\u3002<\/p>\n<p>\u77ed\u7e2e\u306e\u51e6\u7406\u3060\u304c\u3001api.j.mp\u306b\u30a2\u30af\u30bb\u30b9\u3055\u308c\u305f\u5834\u5408\u3068 api.bit.ly \u306b\u30a2\u30af\u30bb\u30b9\u3055\u308c\u305f\u5834\u5408\u3067\u8df3.jp\u3068p.tl\u3092\u4f7f\u3044\u5206\u3051\u3089\u308c\u308b\u3088\u3046\u306b\u3057\u305f\u3002<\/p>\n<p>@@jmp\u3068@@bitly\u306b\u305d\u308c\u305e\u308c\u8df3.jp\u3092\u4f7f\u3044\u305f\u3051\u308c\u3070:tobi\u3001p.tl\u3092\u4f7f\u3044\u305f\u3044\u6642\u306f:ptl\u3092\u5165\u308c\u3066\u304a\u3051\u3070\u3044\u3044\u3002<\/p>\n<p>p.tl\u3092\u4f7f\u3046\u6642\u306f\u3042\u3089\u304b\u3058\u3081API\u30ad\u30fc\u3092\u53d6\u5f97\u3057\u3066@ptlkey\u306b\u4ee3\u5165\u3057\u3066\u304a\u304f\u3002(cf. <a href=\"http:\/\/p.tl\/api.php\">p.tl Web API<\/a> )<\/p>\n<p>\u5b9f\u969b\u306e\u77ed\u7e2e\u306e\u51e6\u7406\u306ftobi\u30e1\u30bd\u30c3\u30c9\u304bptl\u30e1\u30bd\u30c3\u30c9\u3067\u3084\u3063\u3066\u308b\u3002\u3069\u3063\u3061\u3082HTTP\u3067\u30a2\u30af\u30bb\u30b9\u3057\u3066\u3044\u308b\u3060\u3051\u3002<\/p>\n<p>p.tl\u306e\u5834\u5408\u306fJSON\u306e\u30d1\u30fc\u30b9\u3092\u884c\u3063\u3066\u3044\u308b\u3002\u8df3.jp\u306e\u5834\u5408\u3001\u30de\u30eb\u30c1\u30d0\u30a4\u30c8\u6587\u5b57\u5217\u306e\u554f\u984c\u304c\u3042\u308b\u306e\u3067UTF-8\u3060\u3068\u3059\u308b\u51e6\u7406\u3092\u884c\u3063\u3066\u3044\u308b\u3002<\/p>\n<hr \/>\n<p>\u3042\u3068\u306f\u3053\u306e\u7d50\u679c\u3092bit.ly\u306eAPI\u306e\u51fa\u529b\u306b\u3059\u308c\u3070\u3044\u3044\u3002JSON\u306fHash#to_json\u304cjson\u306b\u5165\u3063\u3066\u305f\u304b\u3089\u554f\u984c\u306a\u304b\u3063\u305f\u304c\u3001<\/p>\n<p>to_xml\u304c\u306a\u304b\u306a\u304b\u898b\u3042\u305f\u3089\u306a\u304b\u3063\u305f\u3002Rails\u95a2\u4fc2\u306eactive_support\u306b\u3042\u308b\u306e\u3092\u4f7f\u3048\u3070\u826f\u304b\u3063\u305f\u3088\u3046\u3060\u3002<\/p>\n<p>\u30a8\u30b9\u30b1\u30fc\u30d7\u3055\u308c\u305f\u6587\u5b57\u5217\u3092\u5143\u306b\u623b\u3059\u3068\u304b\u51e6\u7406\u3092\u3059\u308c\u3070\u3068\u308a\u3042\u3048\u305aOK\u3002<\/p>\n<p>Tween\u3067\u306f\u3061\u3087\u3063\u3068\u8abf\u6574\u3059\u308c\u3070\u3046\u307e\u304f\u3044\u3063\u305f\u306e\u3060\u304c\u3001Twit\u3067\u306f\u8df3.jp\u304c\u306a\u305c\u304b\u6587\u5b57\u5316\u3051\u3059\u308b\u3002<\/p>\n<p>XML\u306e\u30a8\u30f3\u30b3\u30fc\u30c9\u306f\u6b63\u3057\u305d\u3046\u3060\u3051\u3069\u3001\u3069\u3046\u3082Twit\u306f\u305d\u3093\u306a\u306e\u304a\u304b\u307e\u3044\u306a\u3057\u3067Shift_JIS\u3068\u3057\u3066\u89e3\u91c8\u3057\u3066\u3044\u308b\u3088\u3046\u3060\u3002<\/p>\n<p>\u307e\u3041bit.ly\u306f\u30de\u30eb\u30c1\u30d0\u30a4\u30c8\u6587\u5b57\u4f7f\u308f\u3093\u3051\u3069\u3055\u3002\u3068\u3044\u3046\u308f\u3051\u3067Twit\u306e\u5834\u5408\u306fbit.ly\u306eID\u306btwit\u3068\u5165\u308c\u3066\u4f7f\u3063\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<hr \/>\n<p>\u672c\u5f53\u306f\u3053\u308c\u3092\u30b5\u30fc\u30d3\u30b9\u5316\u3057\u305f\u304b\u3063\u305f\u306e\u3060\u304c\u3001Mongrel\u3068\u3044\u3046HTTP\u30b5\u30fc\u30d0\u306e\u5b9f\u884c\u74b0\u5883\u304c\u5165\u3089\u306a\u3044\u306e\u3067\u65ad\u5ff5\u3057\u305f\u3002<\/p>\n<p>\u6a19\u6e96\u3067\u306fWEBrick\u3063\u3066\u306e\u3092\u4f7f\u3063\u3066\u3093\u3060\u3051\u3069\u3001Windows\u3067\u306f\u30c7\u30fc\u30e2\u30f3\u5316\u3067\u304d\u3093\u3089\u3057\u3044\u3002UNIX\u7cfbOS\u3067\u306f\u3001<\/p>\n<pre>$ ruby sinatobi.rb -e production -d<\/pre>\n<p>\u3068\u3059\u308c\u3070\u3044\u3044\u3089\u3057\u3044\u3093\u3067\u3059\u3051\u3069\u306d\u3002\u3069\u3046\u3082\u3046\u307e\u304f\u3044\u304b\u3093\u306e\u3067\u3053\u308c\u306f\u5f8c\u56de\u3057\u3002<\/p>\n<p>\u30d7\u30ed\u30f3\u30d7\u30c8\u304c\u90aa\u9b54\u3060\u3051\u3069\u30b9\u30bf\u30fc\u30c8\u30a2\u30c3\u30d7\u306b\u3067\u3082\u767b\u9332\u3059\u308c\u3070\u3044\u3044\u3093\u3058\u3083\u306a\u3044\u3067\u3059\u304b\u306d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6628\u65e5\u3001bit.ly\u306eAPI\u306e\u3075\u308a\u3092\u3059\u308b\u77ed\u7e2eURL\u3092\u4f5c\u3063\u3066\u3075\u3068\u6c17\u4ed8\u3044\u305f\u306e\u3060\u304c\u3001 \u3042\u307e\u308a\u30e1\u30b8\u30e3\u30fc\u3067\u306a\u3044\u305f\u3081\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u304c\u5bfe\u5fdc\u3057\u3066\u3044\u306a\u3044\u77ed\u7e2eURL\u3082\u3053\u306e\u65b9\u6cd5\u3067\u4f7f\u3044\u3084\u3059\u304f\u306a\u308b\u306e\u3067\u306f\u306a\u3044\u304b\u3068\u8003\u3048\u305f\u3002 \u3053\u3053\u3067\u30bf\u30fc\u30b2\u30c3\u30c8\u306b\u3057\u305f\u306e\u306f\u3001\u8df3 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-2304","post","type-post","status-publish","format-standard","hentry","category-computer"],"_links":{"self":[{"href":"https:\/\/hdmr.org\/d\/index.php?rest_route=\/wp\/v2\/posts\/2304","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hdmr.org\/d\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hdmr.org\/d\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hdmr.org\/d\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hdmr.org\/d\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2304"}],"version-history":[{"count":0,"href":"https:\/\/hdmr.org\/d\/index.php?rest_route=\/wp\/v2\/posts\/2304\/revisions"}],"wp:attachment":[{"href":"https:\/\/hdmr.org\/d\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2304"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hdmr.org\/d\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2304"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hdmr.org\/d\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2304"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}