{"id":164941,"date":"2013-01-07T01:05:03","date_gmt":"2013-01-06T21:05:03","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=164941"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=164941","title":{"rendered":"<span class=\"post_title\">\u041f\u0440\u043e\u0441\u0442\u043e\u0439 RESTful api \u0441\u0435\u0440\u0432\u0435\u0440 \u043d\u0430 Ruby (Sinatra)<\/span>"},"content":{"rendered":"<div class=\"content html_format\">   \t\u0414\u043e\u043f\u0443\u0441\u0442\u0438\u043c \u0432\u043e\u0437\u043d\u0438\u043a\u043b\u0430 \u0437\u0430\u0434\u0430\u0447\u0430 \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043f\u0440\u043e\u0441\u0442\u043e\u0439 RESTful api \u0441\u0435\u0440\u0432\u0435\u0440 \u0434\u043b\u044f iOS\/Android \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432.<\/p>\n<p>  <i>\u0427\u0442\u043e \u0434\u0430\u043d\u043e:<\/i><br \/>  \u0411\u0430\u0437\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 \u0438\u043d\u0442\u0435\u0440\u043d\u0435\u0442-\u043c\u0430\u0433\u0430\u0437\u0438\u043d\u0430 (\u0431\u0430\u0437\u0443 \u0442\u0440\u043e\u0433\u0430\u0442\u044c \u043d\u0435\u043b\u044c\u0437\u044f)<br \/>  <i>\u0418\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0439:<\/i><br \/>  <a href=\"http:\/\/www.sinatrarb.com\/\">Sinatra<\/a>, <a href=\"https:\/\/github.com\/brianmario\/mysql2\">mysql2<\/a>, <a href=\"https:\/\/github.com\/flori\/json\">json<\/a><\/p>\n<p>  <b>\u041a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0441\u0442\u0440\u043e\u043a:<\/b> 47<br \/>  <b>\u0412\u0440\u0435\u043c\u044f:<\/b> ~ 5 \u043c\u0438\u043d<\/p>\n<p>  <a name=\"habracut\"><\/a>  <\/p>\n<pre><code class=\"ruby\"># Simple api server require 'rubygems' require 'sinatra' require 'mysql2' require 'json'  db = Mysql2::Client.new( \t:host =&gt; 'localhost', \t:username =&gt; 'root', \t:password =&gt; 'root', \t:database =&gt; 'example', \t:encoding =&gt; 'utf8' )  before do \tcontent_type 'application\/json' end  # \u0433\u043b\u0430\u0432\u043d\u0430\u044f \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0430 get '\/' do \t{ :app =&gt; 'simple api server', :version =&gt; 'v.01', \t\t:date =&gt; Time.new \t}.to_json end  # \u0432\u0435\u0440\u043d\u0435\u0442 \u0441\u043f\u0438\u0441\u043e\u043a \u043a\u0430\u0442\u0435\u0433\u043e\u0440\u0438\u0439 get '\/categories' do \tdb.query(&quot;SELECT * FROM `s_categories`&quot;, :as =&gt; :json) end  # \u0432\u0435\u0440\u043d\u0435\u0442 \u0441\u043f\u0438\u0441\u043e\u043a \u0442\u043e\u0432\u0430\u0440\u043e\u0432 \u0438\u0437 \u043a\u0430\u0442\u0435\u0433\u043e\u0440\u0438\u0438 get '\/categories\/:id' do \tdb.query(&quot;SELECT * FROM `s_products` \t\tLEFT JOIN `s_products_categories` ON `s_products`.`id` = `s_products_categories`.`product_id` \t\tLEFT JOIN `s_categories` ON `s_products_categories`.`category_id` = `s_categories`.`id` \t\tWHERE `s_categories`.`id` = 1 LIMIT 10&quot;, :as =&gt; :json) end  # \u0432\u0435\u0440\u043d\u0435\u0442 \u0441\u043f\u0438\u0441\u043e\u043a \u0442\u043e\u0432\u0430\u0440\u043e\u0432 get '\/products' do \tdb.query(&quot;SELECT * FROM `s_products` LIMIT 10&quot;, :as =&gt; :json) end  # \u0432\u0435\u0440\u043d\u0435\u0442 \u0442\u043e\u0432\u0430\u0440 get '\/products\/:id' do \tdb.query(&quot;SELECT * FROM `s_products` WHERE id = #{params[:id]}&quot;, :as =&gt; :json) end <\/code><\/pre>\n<p>  \u0421 \u0440\u043e\u0436\u0434\u0435\u0441\u0442\u0432\u043e\u043c \ud83d\ude42    \t \t\t   \t<\/p>\n<div class=\"clear\"><\/div>\n<\/p><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"http:\/\/habrahabr.ru\/post\/164941\/\"> http:\/\/habrahabr.ru\/post\/164941\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"content html_format\">   \t\u0414\u043e\u043f\u0443\u0441\u0442\u0438\u043c \u0432\u043e\u0437\u043d\u0438\u043a\u043b\u0430 \u0437\u0430\u0434\u0430\u0447\u0430 \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043f\u0440\u043e\u0441\u0442\u043e\u0439 RESTful api \u0441\u0435\u0440\u0432\u0435\u0440 \u0434\u043b\u044f iOS\/Android \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432.<\/p>\n<p>  <i>\u0427\u0442\u043e \u0434\u0430\u043d\u043e:<\/i><br \/>  \u0411\u0430\u0437\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 \u0438\u043d\u0442\u0435\u0440\u043d\u0435\u0442-\u043c\u0430\u0433\u0430\u0437\u0438\u043d\u0430 (\u0431\u0430\u0437\u0443 \u0442\u0440\u043e\u0433\u0430\u0442\u044c \u043d\u0435\u043b\u044c\u0437\u044f)<br \/>  <i>\u0418\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0439:<\/i><br \/>  <a href=\"http:\/\/www.sinatrarb.com\/\">Sinatra<\/a>, <a href=\"https:\/\/github.com\/brianmario\/mysql2\">mysql2<\/a>, <a href=\"https:\/\/github.com\/flori\/json\">json<\/a><\/p>\n<p>  <b>\u041a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0441\u0442\u0440\u043e\u043a:<\/b> 47<br \/>  <b>\u0412\u0440\u0435\u043c\u044f:<\/b> ~ 5 \u043c\u0438\u043d<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-164941","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/164941","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=164941"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/164941\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=164941"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=164941"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=164941"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}