2011-01-15 7 views
0

私は友達のサイトのAPIを使ってアプリを作ろうとしています。何か、それは非常に新しいです、多くの人々がそれについて知っているわけではありません。そして、例はPHPで書かれています。 これは私の最初の一種のapiで動作するので、どこから始めるべきかわかりません。私が知る必要があるすべては、私はちょうど私がレールでこれを作るだろうかを正確に知っておく必要があり、 誰かがPHPコードをレールに翻訳するのに手伝ってくれますか?

require __DIR__ . '/config.php'; 
require realpath(__DIR__ . '/../src/') . '/dailybooth.php'; 

$dailybooth = new DailyBooth(array(
'client_id' => DAILYBOOTH_CLIENT_ID, 
'client_secret' => DAILYBOOTH_CLIENT_SECRET, 
'redirect_uri' => DAILYBOOTH_REDIRECT_URI, 
)); 

$dailybooth->authorize(); 

は私がファイルである必要が知っている..私は最も可能性の高い自分でそれを得ることができ、その後の基礎であります。 (アプリケーションを許可する)

+1

これはAPIを呼び出すコードです。これをレールに移植するのはあまり意味がありません。API全体を移植する必要があります。 (ただし、APIがRubyで同じ形式で利用できる場合は、これと異なる場合があります) –

+0

@Pekkaこのメソッドを使用する必要はありません.API期間の処理方法を学びたいだけです。 – Rickmasta

+0

@Rickmastaしたがって、Rails APIはありますが、その例はありません。 –

答えて

4


require 'rubygems' 
require 'pp' 
require 'httparty' 

#this is by no means complete. it is just a starting place 
class DailyBooth 

    include HTTParty 

    API_ROOT = 'https://api.dailybooth.com/v1' 

    AUTH_ROOT = 'https://dailybooth.com/oauth' 

    def initialize(options) 
    @oauth_token = options.fetch('oauth_token', nil) 
    @client_id = options.fetch('client_id', nil) 
    @client_secret = options.fetch('client_secret', nil) 
    @redirect_uri = options.fetch('redirect_uri', nil) 
    end 

    def authorize_url 
    AUTH_ROOT + "/authorize?" + {"client_id" => @client_id, "redirect_uri" => @redirect_uri }.to_params 
    end 

    def oauth_token(code) 

    response = token({ 
      'grant_type' => 'authorization_code', 
      'code' => code, 
      'client_id' => @client_id, 
     'client_secret' => @client_secret, 
     'redirect_uri' => @redirect_uri 
    }) 

    @oauth_token = response.fetch('oauth_token', nil)  
    end 


    def token(params) 
    self.class.post(AUTH_ROOT + '/token', {:body => params}); 
    end 

    def get(uri, query = {}) 
    self.class.get(API_ROOT + uri, {:query => {:oauth_token => @oauth_token}.merge(query) }) 
    end 

    def post(uri, params = {}) 
    self.class.post(API_ROOT + uri, {:body => {:oauth_token => @oauth_token}.merge(params) }); 
    end 

end 


dailybooth = DailyBooth.new({ 
    'client_id' => '', 
    'client_secret' => '', 
    'redirect_uri' => '', 
    #'oauth_token' => '' 
}); 

#first redirect the user to the authorize_url 
redirect_to dailybooth.authorize_url 

#on user return grab the code from the query string 
dailybooth.oauth_token(params[:code]) 

#make request to the api 
pp dailybooth.get('/users.json') 
+0

これは素晴らしいスタートです!ありがとう! – Rickmasta

2

Ruby/RailsでDailyBooth APIに接続する方法を聞いていますか? Dropbox、Tumblr、Flickraw、Twilioの宝石のようなものをあなたのもとに置くことができますが、あなたの質問で説明したことを考えれば、あなたの現在の知識よりも上になります。

残念ながら、DailyBoothはドキュメントを完成させていないようだし、見つけられたものからRuby SDKや宝石が入手できない。

0

HTTPartyでAPIクライアントを作成するのは簡単です。ソースのexamplesディレクトリを参照してください。唯一の部分はOAuthです。 Twitterの宝石はHTTPartyとOAuthを使用しているので、少なくともあなたには例があります。

関連する問題