2011-01-15 10 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 gem과 같은 것을 기반으로 작업을 할 수 있도록 REST API 일 뿐이지 만, 질문에서 설명한 내용을 감안할 때 현재 알고있는 지식보다 뛰어납니다.

불행히도 DailyBooth는 설명서를 완성하지 못했고 Ruby SDK 또는 사용할 수있는 보석이 없습니다.

0

HTTParty으로 API 클라이언트를 만드는 것은 매우 쉽습니다. 소스의 examples 디렉토리를 참조하십시오. 유일한 다른 작품은 OAuth입니다. 트위터 젬은 HTTParty와 OAuth를 사용하기 때문에 적어도 예제가있을 것입니다.

관련 문제