开发者

Can someone help me translate some PHP code into rails?

well I'm trying to make an app using a friends' sites' api. Thing is, it's very new, not many people know about it. And only examples are written in PHP.. This is my first time working with an api of some sort, so not sure where to start. All I need to know is the basics then I can most likely get it off on my own..

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

$dailybooth = new DailyBooth(array(
'c开发者_开发技巧lient_id' => DAILYBOOTH_CLIENT_ID,
'client_secret' => DAILYBOOTH_CLIENT_SECRET,
'redirect_uri' => DAILYBOOTH_REDIRECT_URI,
));

$dailybooth->authorize();

I know what the require the file is, I just need to know how exactly I would make this in rails. (The authorizing the app)




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')


Are you asking how to connect to the DailyBooth API in Ruby/Rails? It's just a REST API it appears, so you could base your work off of something like the Dropbox, Tumblr, Flickraw, or Twilio gem, but it's going to be above your current knowledge I would assume, given what you explained in your question.

Unfortunately, DailyBooth doesn't appear to have their documentation finished, and there isn't a Ruby SDK or gem available for it from what I can find.


It's very easy to create an API client with HTTParty. See the examples directory in the source. The only other piece is OAuth. The Twitter gem uses HTTParty and OAuth, so at least you'd have an example.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜