Giter Club home page Giter Club logo

grizzly's Introduction

Grizzly

About

Grizzly is a simple API wrapper around the Weibo API V2. The name comes form the awesome Koala gem for facebook.

Installation

Grizzly as a name was already taken on ruby gems. Awww. However grizzly-weibo was not. To install using bundler add the following to your gem file.

gem "grizzly-weibo", :require => "grizzly"

Usage

To start using grizzly you want to create a client object. This client object allows you to interact with weibo api. This can be done creating a new object of type Grizzly::Client. You will need an access token to do this. Currently grizzly does not support authentication with oauth. If you need to do this then I recommend using omniauth. There is already a working strategy for Weibo.

client = Grizzly::Client.new(access_token)

Getting User Information

Once a client object has been created we can use it to download information based on a user id. Just call the user method and pass it a single parameter. This is the uid of the user on Weibo. This method will return a user object with all of the user data.

user = client.user(11223344556677)
user.name             #=> "fredchang"
user.gender           #=> "m"
user.followers_count  #=> 53

Updating Status

First things first. Lets update the users status. This only works after a user has been logged in and has an access token.

client = Grizzly::Client.new(access_token)
status = client.status_update("Dude. Weibo is awesome!")

Status Comments

Weibo supports adding comments to status updates. All comments for a status can be fetched in the following way. Note that we pass the id of the status that we are intrested in

comments = client.comments(status.id)

Note that all method calls made with Grizzly will always return a domain object. More often than not this domain object is a representation of what ever JSON object has been returned by Weibo's API. In this case we get all sorts of handy information on the status we just updated including the user that the status belongs to. So you can do...

status.user.id    #=> "1233344545356356"
status.text       #=> "Dude. Weibo is awesome!"

Friends

You can access a list of friends by supplying a weibo user id

friends = client.friends(user_id)

This method will return an array of user friend objects. Data of these objects can be accessed via methods

friends.first.id            #=> "1233344545356356"
friends.first.screen_name   #=> "Fred Chang"
friends.first.gender        #=> "m"

You can convert a user object to a hash if you need to however use . syntax where you can. Makes the code look cleaner

friends.first.to_h["screen_name"] #=> "Fred Chang"

Friends (Bilateral)

Returns a list of your bilateral friends on Weibo. This list has users who are a fan of you and your are a fan of.

friends = client.bilateral_friends(user_id)

This method returns the same friend object that client.friends returns.

Cursors

Weibo API supports paging. Both the friends and bilateral_friends are paginated. This means that when we call these methods we have a cursor object returned. This cursor object can be iterated over. Iterating in this way will only iterate over the first 50 results

friends = client.friends(user_id)
friends.each do |friend|
  ...
end

For something a little more robust we can iterate over pages as well. This will get all of a users friends iterating over them in blocks of fifties. Note that each time you access friends.each an new request to the weibo api will be generated.

friends = client.friends(user_id)
while friends.next_page? #Loops untill end of collection
  friends.each do |friend|
    ... # Loops 50 times
  end
end

Error Handling

Grizzly has its own set of errors that it can throw. Grizzly::Errors::NoAccessToken is thrown when the client is created with out an access token. Grizzly::Errors::WeiboAPI is thrown when ever there is an error returned by an end point of the API. This will return both a message and a weibo error code. Grizzly::Errors::Timeout will be thrown when the Weibo API is taking too long to respond to a response. There currently is a five second limit on the request time of each request made by Grizzly. Note that when your using a cursor class described above this time out applies to each request the cursor makes not the total time that the cursor takes to build the results of a given GET from the api.

Development

Please feed free to fork, develop and send pull requests. If you plan to do this then you will need a valid weibo access token. Place this token in an access_token.yml file that can be found under the spec folder. Currently this file is in the git ignore list however you can copy the same file. Once you have done this add your access token to the file and specs will pick it up.

Pull Requests

The changes are that what you want to do with the API is not supported. Let me know if that is the case and I will add support for anything you need to do. I will also accept any pull requests with new features included. I did not intend to release this gem full supported with each end point mapped.

Contributors

  • realalien (Zhu Jia Cheng)

License

Copyright (c) 2012 Stewart Matheson

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

grizzly's People

Contributors

realalien avatar stewartmatheson avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

grizzly's Issues

Getting an Access Token

Currently there is no way to get an access token using Grizzly. There are some ways that this could happen.

  1. Using a gem like mechanize to perform the login and then getting the access token back to the user
  2. Look in to integrating with the oAuth2 gem that might allow to do this in an automated way.

Show user end point

Currently there is no way to get a user. There should be a method in the client to fetch user info based on an id.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.