Giter VIP home page Giter VIP logo

disqus_api's Introduction

Disqus API for Ruby

Gem Version Build Status Code Climate

Provides clean Disqus REST API for your Ruby app. Currently supported API version: 3.0.

Rails >3.0 is also supported.

Install

gem install disqus_api

Configure

If you are not using Rails:

require 'disqus_api'

DisqusApi.config = {api_secret: 'secret key',
                    api_key: 'public key',
                    access_token: 'token from app settings'}

For Rails users:

Put in your config/disqus_api.yml:

development:
  api_secret: development_secret_key
  api_key: 'public key'
  access_token: 'token from app settings'

production:
  api_secret: production_secret_key
  api_key: 'public key'
  access_token: 'token from app settings'

# ... any other env

Enjoy

DisqusApi.v3.users.details
# => {"code" => 0, "response" => {
#                     "isFollowing"=>false,
#                     "isFollowedBy"=>false, "connections"=>{}, "isPrimary"=>true, "id"=>"84792962"
#                     ...
#                  }

Use response to get response body

DisqusApi.v3.users.details.response
# => {
#       "isFollowing"=>false,
#       "isFollowedBy"=>false, "connections"=>{}, "isPrimary"=>true, "id"=>"84792962"
#       ...
#    }

Alias #body for response body

DisqusApi.v3.users.details.body
# => {
#       "isFollowing"=>false,
#       "isFollowedBy"=>false, "connections"=>{}, "isPrimary"=>true, "id"=>"84792962"
#       # ...
#    }

Setting additional parameters to a query

DisqusApi.v3.posts.list(forum: 'my_form')

Fetching full collections

By default Disqus API limits returning collections by 25 records per requests. The maximum limit you can set is 100. In order to fetch all records from a collection use #all method:

DisqusApi.v3.posts.list(forum: 'my_form').all

Pagination

Step by step:

first_page  = DisqusApi.v3.posts.list(forum: 'my_forum', limit: 10)

second_page = first_page.next
third_page  = second_page.next
first_page  = third_page.prev.prev
# ...

It is useful to go through all records. This way you will pass every page in batches by 10:

DisqusApi.v3.posts.list(limit: 10).each_resource do |comment|
  puts comment.inspect
end

You can also iterate collection page by page.

DisqusApi.v3.posts.each_page do |comments|
  comments.each do |comment|
    puts comment.inspect
  end
end

You can also move on next page:

response = DisqusApi.v3.posts
response.next!

Or on previous one:

response.prev!

Performing custom requests

DisqusApi.v3.get('posts/list.json', forum: 'my_forum')
DisqusApi.v3.post('posts/create.json', forum: 'my_forum')

Handling exceptions

Just catch DisqusApi::InvalidApiRequestError. It has code to identify problems with a request.

begin
  DisqusApi.v3.posts.list(forum: 'something-wrong')
rescue DisqusApi::InvalidApiRequestError => e
  e.response.inspect
end

#=> {"code"=>2, "response"=>"Invalid argument, 'forum': Unable to find forum 'something-wrong'"}

Using in test environment

before :all do
  # You can move this block in RSpec initializer or `spec_helper.rb`

  DisqusApi.stub_requests do |stub|
    stub.get('/api/3.0/users/details.json') { [200, {}, {code: 0, body: {response: :whatever}}.to_json] }
  end
end

it 'performs requests' do
  DisqusApi.v3.users.details['code'].should == 0
end

Disqus API uses Faraday gem, refer to its documentation for details.

Running specs

Use any of the following commands from the project directory:

rspec
rake # rake gem must be installed

In order to test on a real Disqus account

  • specify spec/config/disqus.yml (see spec/config/disqus.yml.example for details)
  • run specs passing USE_DISQUS_ACCOUNT environment variable:
USE_DISQUS_ACCOUNT=1 rspec

Contributing to disqus_api

  • Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
  • Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
  • Fork the project
  • Start a feature/bugfix branch
  • Commit and push until you are happy with your contribution
  • Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
  • Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.

This project is licensed under the terms of the MIT license.

disqus_api's People

Contributors

cfiorini avatar einzige avatar matteeyah avatar nicolasleger avatar vmaxv avatar yatish27 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

disqus_api's Issues

Bump the gem version

Can you publish a new gem version to rubygems, please?

The changes after the last release allow embedded ruby to be used in the configuration file.

cc @einzige

Performing writes

Hi, is there a way with disqus_api to perform writes, like for example moderating comments? By looking at the documentation and the tests I couldn't find anything.

Automatically Detect Rate Limiting

It would be highly convenient if this gem had the ability to automatically detect rate limiting and be configured to either stop or wait until API calls will be usable again.

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.