0.0
Repository is archived
No commit activity in last 3 years
No release in over 3 years
A Ruby Client for diandian API
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

~> 3.2.6
~> 0.8.0
 Project Readme

diandian-oauth

diandian-oauth is a Ruby client for diandian API

Usage

## Initialize client ##

client ||= DiandianOAuth::Client.new config.client_id, config.client_secret, config.client_options

## Get authorization url ##

client.authorization_url

## Get access token with responded code ##

client.access_token CODE

## or Assign an existing access_token ##

client.access_token = {
          :access_token => '312e7a48-8d05-4cd9-a3a9-044d2f47e2af',
          :refresh_token => "79852349-5f2e-4e3b-93b8-845e239e61ff",
          :token_type => "bearer",
          :expires_in => 604799,
          :expires_at => 1345201711,
          :scope => "write read",
          :uid => "11449"
        }

## Basic usage ##

client.user_info # Get user_info

client.user_info! # Get user_info and refresh access_token if it has expired

client.create_post!  :blogCName => BLOG_CNAME,
      :type => 'text',
      :state => 'published',
      :title => 'Hello from diandian ruby client' # create a text post

client.create_post! :blogCName => BLOG_CNAME,
      :type =>  'photo',
      :state => 'published',
      :data => [Faraday::UploadIO.new(JPEG_IMAGE_PATH_1, 'image/jpeg'), Faraday::UploadIO.new(JPEG_IMAGE_PATH_2, 'image/jpeg')],
      :itemDesc => %w[test test]
      :caption => "<p>test</p>" # create a photo post

## Register a callback after the access_token is refreshed ##

DiandianOAuth::Client.token_refreshed (lambda{|client, uid, token|
  puts "token_refreshed: '#{uid}': #{token}'"
})

## Handle the response ##

response = client.an_interface! params # response is an instance of DiandianOAuth::Response

unless response.success?
  ## handle errors here
  puts response.error
  puts response.meta
else
  puts response.response # the response part of the server response
end

begin
  response.validate! #this invocation will raise corresponding error for an error response
rescue APIException => e
  #handle errors here
end

response.internal # the original response, an instance of Faraday::Response

Supported Interfaces

All the interfaces supported(/registerd) can be found at lib/diandian_oauth/api.rb which include

  • interface :user_info, Interface::UserInfo
  • interface :user_likes, Interface::UserLikes
  • interface :user_followings, Interface::UserFollowings
  • interface :my_tags, Interface::MyTags
  • interface :blog_info, Interface::BlogInfo
  • interface :blog_avatar, Interface::BlogAvatar
  • interface :blog_followers, Interface::BlogFollowers
  • interface :posts, Interface::Posts
  • interface :post_info, Interface::PostInfo
  • interface :create_post, Interface::CreatePost
  • interface :delete_post, Interface::DeletePost
  • interface :reblog_post, Interface::ReblogPost
  • interface :home_feeds, Interface::HomeFeeds
  • interface :tag_feeds, Interface::TagFeeds
  • interface :follow, Interface::Follow
  • interface :unfollow, Interface::Unfollow
  • interface :watch_tag, Interface::WatchTag
  • interface :unwatch_tag, Interface::UnwatchTag
  • interface :submissions, Interface::Submissions
  • interface :submit, Interface::Submit
  • interface :reject_submission, Interface::RejectSubmission

Extend Interface

  1. Extend DiandianOAuth::API::Interface::Base

    class NewInterface < DiandianOAuth::API::Interface::Base
    end
  2. Declare the request verb( default is get) and params

    verb :get
    param :limit, :required => false
    param :offset, :required => false
  3. Override request_url to generate request_url

  4. Register interface to the API object

    API.interface :new_interface, NewInterface

TODO

  1. group param declarations to enable a refined parameters checking
  2. make the error processing more robust
  3. give more advises to diandain to help them make their API more feasible