├── .gitignore ├── .travis.yml ├── Gemfile ├── LICENSE.txt ├── README.md ├── Rakefile ├── disqus_rails.gemspec ├── lib ├── disqus_rails.rb └── disqus_rails │ ├── active_record │ ├── acts_as_disqusable.rb │ └── acts_as_disquser.rb │ ├── api.rb │ ├── api.yml │ ├── category.rb │ ├── collection.rb │ ├── forum.rb │ ├── helpers.rb │ ├── model.rb │ ├── post.rb │ ├── thread.rb │ ├── user.rb │ └── version.rb ├── spec ├── disqus_rails │ ├── active_record │ │ ├── acts_as_disqusable_spec.rb │ │ └── acts_as_disquser_spec.rb │ ├── api_spec.rb │ ├── category_spec.rb │ ├── collection_spec.rb │ ├── forum_spec.rb │ ├── model_spec.rb │ ├── post_spec.rb │ ├── thread_spec.rb │ └── user_spec.rb ├── disqus_rails_spec.rb └── spec_helper.rb └── vendor └── assets └── javascripts └── disqus_rails.js.coffee /.gitignore: -------------------------------------------------------------------------------- 1 | *.gem 2 | *.rbc 3 | .bundle 4 | .config 5 | .yardoc 6 | Gemfile.lock 7 | InstalledFiles 8 | _yardoc 9 | coverage 10 | doc/ 11 | lib/bundler/man 12 | pkg 13 | rdoc 14 | spec/reports 15 | test/tmp 16 | test/version_tmp 17 | tmp 18 | .rspec 19 | .idea 20 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: ruby 2 | rvm: 3 | - "1.9.3" 4 | - "2.1.0" 5 | 6 | script: bundle exec rspec -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | # Specify your gem's dependencies in disqus_rails.gemspec 4 | gemspec 5 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2013 Anton Kirichenko 2 | 3 | MIT License 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 20 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 21 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 22 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # DisqusRails 2 | 3 | [![Build Status](https://travis-ci.org/sandric/disqus_rails.svg?branch=master)](http://travis-ci.org/sandric/disqus_rails) 4 | [![Code Climate](https://codeclimate.com/github/sandric/disqus_rails.png)](https://codeclimate.com/github/sandric/disqus_rails) 5 | [![Dependency Status](https://gemnasium.com/sandric/disqus_rails.png)](https://gemnasium.com/sandric/disqus_rails) 6 | 7 | DisqusRails is a gem for including [Disqus](http://disqus.com/) service into Ruby on Rails application. 8 | 9 | ## Not Maintained 10 | 11 | ## Installation 12 | 13 | Add this line to your application's Gemfile: 14 | 15 | `gem 'disqus_rails'` 16 | 17 | And then execute: 18 | 19 | `$ bundle` 20 | 21 | Or install it yourself as: 22 | 23 | `$ gem install disqus_rails` 24 | 25 | And add to your javascript manifest file: 26 | 27 | `//= require disqus_rails` 28 | 29 | ## Usage 30 | ### Getting started 31 | 32 | Create new initializer, lets say 'disqus_rails.rb' in config/initializers directory with this: 33 | ```ruby 34 | DisqusRails.setup do |config| 35 | config::SHORT_NAME = "your_short_name" 36 | config::SECRET_KEY = "your_secret_disqus_key" #leave blank if not used 37 | config::PUBLIC_KEY = "your public_disqus_key" #leave blank if not used 38 | config::ACCESS_TOKEN = "your_access_token" #you got it, right? ;-) 39 | end 40 | ``` 41 | In your layout file place 'disqus_init' helper: 42 | ```erb 43 | <%= disqus_init %> 44 | ``` 45 | In your view, where you want to display Disqus thread, place 'disqus_thread' helper: 46 | ```erb 47 | <%- disqus_thread %> 48 | ``` 49 | And you are ready to go. 50 | 51 | ####additional params: 52 | 53 | You can omit creating initializer if you want - you can pass all those params right into 'disqus_init' helper as hash: 54 | ```erb 55 | <%= disqus_init :short_name => "short_name", :public_key => "public key", :secret_key => "secret_key", :access_token => "access_token" %> 56 | ``` 57 | also you can pass reset option to 'disqus_init' to invoke 'DISQUS.reset' function to be triggered on 'onReady' disqus event: 58 | ```erb 59 | <%= disqus_init :reset => true %> 60 | ``` 61 | This is for ajax-heavy sites, read more [here](http://help.disqus.com/customer/portal/articles/472107-using-disqus-on-ajax-sites) 62 | 63 | 'disqus_thread' has two params - first is 'ident' identifier and the second is title: 64 | ```erb 65 | <%- disqus_thread 1, "some title, that will preferred to document.title" %> 66 | ``` 67 | 68 | ###Api 69 | 70 | ####Api calls 71 | 72 | 'DisqusRails::Api' stands for [Disqus API](http://disqus.com/api/docs/) calls. Each entity of Disqus API (Posts, Users, Forums, etc...) 73 | has its own class in 'DisqusRails::Api' module. Lets say you want to get all reactions for some forum with limit of 50 results, with desc order: 74 | ```ruby 75 | DisqusRails::Api::Reactions.list(:forum => "forum_id", :limit => 50, :order => "desc") 76 | ``` 77 | or, for example to update some post: 78 | ```ruby 79 | DisqusRails::Api::Posts.update(:post => post_id, :message => "some updated message") 80 | ``` 81 | Disqus return data as json, 'DisqusRails::Api' requests translates it to hash with symbolized keys. 82 | All methods with required and optional params you can see in 'api.yml' file. If any of required params not passed - it will generate 83 | exeption, so as if passed param neither required nor optional. Also, you have to initialize access_token for methods that has 84 | 'require authentication' option set to true. 85 | 86 | ####Models 87 | 88 | For more flexibility and using Disqus entities as an ActiveRecord model there is 'DisqusRails::Model' class that is inherited by 89 | 'DisqusRails::Forum', 'DisqusRails::Category', 'DisqusRails::Thread', 'DisqusRails::Post' and 'DisqusRails::User'. 90 | Lets take previous example and turn it into model's presentation: 91 | ```ruby 92 | new_post = DisqusRails::Post.create(:message => "initial message") 93 | new_post.update(:message => "updated message") 94 | new_post.author.checkUsername("John Doe") 95 | new_post.remove 96 | new_post.restore 97 | ``` 98 | If Disqus entity has 'details' api method, than model has 'find' singleton method 99 | ```ruby 100 | user = DisqusRails::User.find(:user => "user_id") 101 | user_posts.follow() 102 | ``` 103 | Each 'DisqusRails::Model' has number of attributes that you can find in model's code. For example, here is Posts: 104 | ```ruby 105 | class Post < Model 106 | attr_accessor :id, 107 | :isJuliaFlagged, 108 | :isFlagged, 109 | :parent, 110 | :media, 111 | :isApproved, 112 | :dislikes, 113 | :raw_message, 114 | :points, 115 | :createdAt, 116 | :isEdited, 117 | :message, 118 | :isHighlighted, 119 | :ipAddress, 120 | :isSpam, 121 | :isDeleted, 122 | :likes, 123 | 124 | :author, 125 | :thread, 126 | :forum 127 | ``` 128 | 129 | ####Collections 130 | 131 | Collections, as you may guess, is a list of similar Models. Its inherited from Enumerable. There is similar to models 132 | number of collection - 'DisqusRails::Forums', 'DisqusRails::Categories', 'DisqusRails::Threads', 'DisqusRails::Posts' and 'DisqusRails::Users'. 133 | Here some examples: 134 | ```ruby 135 | user = DisqusRails::User.find(:user => "user_id") 136 | user.active_threads(:limit => 50)[15].posts.each do |post| 137 | post.update(:message => "my post is nothing comparing to #{user.username} writings...") 138 | end 139 | ``` 140 | If there is 'list' method in Disqus entity - it transforms to 'where' singleton method of model that creates corresponding collection. For example: 141 | ```ruby 142 | forum_categories = DisqusRails::Category.where(:forum => "forum_id") 143 | ``` 144 | Disqus API is designed in such way, that you can only get maximum 100 results for list query, and by default its 25. To get more 145 | results you are given with 'cursor' object that have 'next' and 'prev' values, passing which into query you can walk through 146 | results as if it was a list data structure. To define if there is more values to get, Disqus provides 'hasNext' and 'hasPrev' boolean 147 | values. For example, to get first 225 posts you can use this code: 148 | ```ruby 149 | posts = DisqusRails::Post.where(:limit => 75) 150 | 2.times do 151 | if posts.has_cursor_next? 152 | previous_items = posts.items 153 | posts.cursor_next! 154 | posts.items = previous_items + posts.items 155 | end 156 | end 157 | ``` 158 | In future I, may be, will rewrite this to handle common cases for :limit attribute to be set to any number. As you saw, in 159 | previous example were used 'cursor_next!' method. There is both 'cursor_next', 'cursor_next!' and 'cursor_prev', 'cursor_prev!' methods. 160 | The difference is in returned values - method with bang in the end initializes new collection right in invoked instance, 161 | when method without it - just returns new collection. 162 | Also, each collection has singleton method 'find_all_#collection_class_name#!' that will get all results for query: 163 | ```ruby 164 | threads = DisqusRails::Thread.where(:forum => "forum_name", :limit => 100).find_all_threads! 165 | ``` 166 | 167 | ###Connection to ActiveRecord models 168 | 169 | ####acts_as_disqusable and disqus_thread 170 | 171 | Lets say you have a 'Content' ActiveRecord model that implements logic for displaying some content information, and you add to that displaying 172 | Disqus thread. Then you may want to match threads info with different Content model instances. For example you may want to know 173 | how many comments and what is the last comment for each model instance. 174 | For this you need to wright 'acts_as_disqusable' in models definition: 175 | ```ruby 176 | class Content < ActiveRecord::Base 177 | acts_as_disqusable 178 | ... 179 | end 180 | ``` 181 | And then all your model instances will be populated with 'disqus_thread' method that will return 'DisqusRails::Thread' instance 182 | that is found by Disqus 'ident' identifier which you can pass to 'disqus_thread' helper in your view. Here is full example: 183 | 184 | Your model: 185 | ```ruby 186 | class Content < ActiveRecord::Base 187 | acts_as_disqusable 188 | ... 189 | end 190 | ``` 191 | Your model's details view 192 | ```erb 193 | <%- disqus_thread @content.id %> 194 | 195 | ``` 196 | And now, when you run this 197 | ```ruby 198 | disqus_thread = Content.first.disqus_thread #It will be thanslated to DisqusRails::Thread.find(:'thread:ident' => Content.first.id) 199 | disqus_thread.posts_count #number of posts 200 | disqus_thread.posts(:limit => 1).createdAt #last comment date 201 | ``` 202 | This work also in opposite direction - after you include 'acts_as_disqusable' in your model definition, all 'DisqusRails::Thread' 203 | instances you will have method 'disqusable' what will return your model instance that is linked to Disqus thread via 'ident' identificator. 204 | As an example lets say that we want to get all threads from Disqus service and update comments_count attribute in Content model: 205 | ```ruby 206 | DisqusRails::Thread.where().find_all_threads!.each do |thread| 207 | thread.disqusable.comments_count = thread.posts_count 208 | thread.disqusable.save() 209 | end 210 | ``` 211 | 212 | ####acts_as_disquser and Single Sign On 213 | 214 | Disqus provides [SSO service](http://help.disqus.com/customer/portal/articles/236206-integrating-single-sign-on) which gives 215 | ability to link your local users info to Disqus users, read more in Disqus tutorial. To do this, as and for linking model to 216 | Disqus thread - you have to add 'acts_as_disquser' line in your users model. You need pass there four attributes: 217 | 'id', 'username', 'email' and 'avatar'(avatar is an optional field, so you can omit this). Here is example: 218 | ```ruby 219 | class User < ActiveRecord::Base 220 | acts_as_disquser :username => :full_name, :email => :email, :avatar => Proc.new{ avatar.url } 221 | ... 222 | end 223 | ``` 224 | As you see, you can pass there or symbols, or procs. First will try to get instance variable with such name from model's instance, 225 | second will evaluate code inside Proc with context of model's instance. Important - only Proc are available for second way of 226 | defining attribute, no lambdas. 227 | Also, you may not implicitly pass `acts_as_disquser :id => :id` - it will try to get id automatically if it is not defined. 228 | Next, you need to specify in disqus_init helper attributes 'disquser' with current user instance, and 'sso' as 229 | boolean to enable or disable SSO. 230 | ```erb 231 | <%= disqus_init :disquser => current_user, :sso => true %> 232 | ``` 233 | After this is done, when users will post comments via Disqus, their username, email and avatar will be taken from your site. 234 | 235 | ###Javascript events 236 | 237 | Disqus provides developer with set of events which could be used to implement some logic that depends on it. The problem is 238 | that instead of triggering events we have to append function definitions in array for each of this events - for example look 239 | at [this article](http://help.disqus.com/customer/portal/articles/466258-how-can-i-capture-disqus-commenting-activity-in-my-own-analytics-tool-). 240 | I found that it might be a little bit more useful to set event listener for this, so I defined separate event for every Disqus event 241 | that developer can implicitly create listener: 242 | ```coffeescript 243 | @callbacks.afterRender = [-> 244 | $(document).trigger "disqus:after_render" 245 | ] 246 | @callbacks.onInit = [-> 247 | $(document).trigger "disqus:on_init" 248 | ] 249 | @callbacks.onNewComment = [-> 250 | $(document).trigger "disqus:on_new_comment" 251 | ] 252 | @callbacks.onPaginate = [-> 253 | $(document).trigger "disqus:on_paginate" 254 | ] 255 | @callbacks.onReady = [-> 256 | $(document).trigger "disqus:on_ready" 257 | ] 258 | @callbacks.preData = [-> 259 | $(document).trigger "disqus:pre_data" 260 | ] 261 | @callbacks.preInit = [-> 262 | $(document).trigger "disqus:pre_init" 263 | ] 264 | @callbacks.preReset = [-> 265 | $(document).trigger "disqus:pre_reset" 266 | ] 267 | ``` 268 | For more information about coffeescript global class 'DisqusRails' look into 'disqus_rails.js.coffee' file. 269 | 270 | ### Keeping data up to date 271 | It is equally little hard to do that with Disqus for now, as for me. The problem is that you can not set some callback for user 272 | actions - all you can is to set event listener for 'disqus:on_new_comment', but that will not be valid for all circumstances. 273 | Lets say user deleted or created new post from his users admin page in Disqus site. Disqus does not provide any callback for setting url 274 | where should query go, or some other futuristic way like web socket channel (sarcasm tag). So we should create some cron task for keeping data 275 | that we need up to date. For example, lets go back to problem of getting comments count and last comment for each Disqus thread. 276 | Here is example of such rake task that could be scheduled with whenever (or any else) gem: 277 | 278 | ```ruby 279 | require 'resque/tasks' 280 | 281 | namespace :disqus do 282 | desc "Refreshing local data about remote disqus comments" 283 | task :refresh => :environment do 284 | threads = DisqusRails::Thread.where(:forum => "forum_name", :limit => 100).find_all_threads! 285 | threads.each do |thread| 286 | if thread.disqusable_id && Content.find_by_id(thread.disqusable_id) 287 | content = thread.disqusable 288 | if (content.comments_count != thread.posts_count) || (thread.posts_count > 0 && thread.posts(:limit => 1).first.createdAt != content.last_comment_at) 289 | content.comments_count = thread.posts_count 290 | if thread.posts_count > 0 291 | content.last_comment_at = DateTime.parse(thread.posts(:limit => 1).first.createdAt) 292 | else 293 | content.last_comment_at = nil 294 | end 295 | content.save 296 | end 297 | end 298 | end 299 | end 300 | end 301 | 302 | ``` 303 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require "bundler/gem_tasks" 2 | -------------------------------------------------------------------------------- /disqus_rails.gemspec: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | lib = File.expand_path('../lib', __FILE__) 3 | $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) 4 | require 'disqus_rails/version' 5 | 6 | Gem::Specification.new do |spec| 7 | spec.name = "disqus_rails" 8 | spec.version = DisqusRails::VERSION 9 | spec.authors = ["Anton Kyrychenko"] 10 | spec.email = ["kyrychenkoanton@gmail.com"] 11 | spec.description = "Disqus 2012 Ruby on Rails wrapper" 12 | spec.summary = "Integrates Disqus service into your Ruby on Rails application" 13 | spec.homepage = "https://github.com/sandric/disqus_rails" 14 | spec.license = "MIT" 15 | 16 | spec.files = `git ls-files`.split($/) 17 | spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } 18 | spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) 19 | spec.require_paths = ["lib"] 20 | 21 | spec.add_development_dependency "bundler" 22 | spec.add_development_dependency "rake" 23 | spec.add_development_dependency "rspec" 24 | spec.add_development_dependency 'sqlite3' 25 | 26 | spec.add_development_dependency 'travis-lint' 27 | 28 | spec.add_dependency "rails" 29 | end 30 | -------------------------------------------------------------------------------- /lib/disqus_rails.rb: -------------------------------------------------------------------------------- 1 | require "disqus_rails/version" 2 | 3 | %w[api helpers collection model user post thread forum category].each do |file| 4 | require File.join(File.dirname(__FILE__), "disqus_rails", file) 5 | end 6 | 7 | %w[acts_as_disqusable acts_as_disquser].each do |file| 8 | require File.join(File.dirname(__FILE__), "disqus_rails/active_record", file) 9 | end 10 | 11 | module DisqusRails 12 | module Rails 13 | class Engine < ::Rails::Engine 14 | initializer 'acts_as_disqusable.extend_active_record' do 15 | ::ActiveRecord::Base.extend DisqusRails::Disqusable::ActiveRecordMethods 16 | end 17 | 18 | initializer 'acts_as_disquser.extend_active_record' do 19 | ::ActiveRecord::Base.extend DisqusRails::Disquser::ActiveRecordMethods 20 | end 21 | end 22 | end 23 | 24 | def self.setup 25 | yield self 26 | end 27 | end 28 | 29 | module ApplicationHelper 30 | include DisqusRails::Helpers 31 | end 32 | -------------------------------------------------------------------------------- /lib/disqus_rails/active_record/acts_as_disqusable.rb: -------------------------------------------------------------------------------- 1 | module DisqusRails 2 | module Disqusable 3 | module ActiveRecordMethods 4 | def acts_as_disqusable 5 | include ActsAsDisqusable 6 | end 7 | end 8 | 9 | module ActsAsDisqusable 10 | 11 | def disqus_thread 12 | DisqusRails::Thread.find_by_ident(self.id) 13 | end 14 | 15 | def self.included(disqusable) 16 | Thread.class_exec do 17 | define_method :disqusable do 18 | disqusable.find(@disqusable_id) 19 | end 20 | end 21 | end 22 | end 23 | end 24 | end 25 | -------------------------------------------------------------------------------- /lib/disqus_rails/active_record/acts_as_disquser.rb: -------------------------------------------------------------------------------- 1 | module DisqusRails 2 | module Disquser 3 | module ActiveRecordMethods 4 | def acts_as_disquser(attributes={}) 5 | 6 | define_method :disqus_params do 7 | data = {} 8 | 9 | if attributes.has_key?(:id) 10 | if attributes[:id].is_a? Proc 11 | data[:id] = instance_eval &attributes[:id] 12 | else 13 | data[:id] = send attributes[:id] 14 | end 15 | else 16 | data[:id] = self.id 17 | end 18 | 19 | if attributes.has_key?(:username) 20 | if attributes[:username].is_a? Proc 21 | data[:username] = instance_eval &attributes[:username] 22 | else 23 | data[:username] = send attributes[:username] 24 | end 25 | end 26 | 27 | if attributes.has_key?(:email) 28 | if attributes[:email].is_a? Proc 29 | data[:email] = instance_eval &attributes[:email] 30 | else 31 | data[:email] = send attributes[:email] 32 | end 33 | end 34 | 35 | if attributes.has_key?(:avatar) 36 | if attributes[:avatar].is_a? Proc 37 | data[:avatar] = instance_eval &attributes[:avatar] 38 | else 39 | data[:avatar] = send attributes[:avatar] 40 | end 41 | end 42 | 43 | data 44 | end 45 | end 46 | end 47 | end 48 | end 49 | -------------------------------------------------------------------------------- /lib/disqus_rails/api.rb: -------------------------------------------------------------------------------- 1 | require 'open-uri' 2 | require 'json' 3 | require 'net/http' 4 | require 'uri' 5 | require 'yaml' 6 | require 'active_support/core_ext/hash' 7 | require 'active_support/core_ext/string' 8 | 9 | module DisqusRails 10 | module Api 11 | requests = ActiveSupport::HashWithIndifferentAccess.new YAML::load(File.open(File.join(File.dirname(__FILE__), "api.yml"))) 12 | 13 | requests.each do |section_name, section| 14 | const = const_set(section_name, Class.new) 15 | 16 | section.each do |method_name, method| 17 | const.define_singleton_method(method_name) do |*arguments| 18 | arguments[0] ||= {} 19 | 20 | method[:params][:required].each do |required_method_name| 21 | unless arguments[0].has_key?(required_method_name.to_sym) 22 | raise "Required parameter '#{required_method_name}' not passed to DisqusRails::Api::#{section_name}.#{method_name}" 23 | end 24 | end 25 | 26 | unless (unknown_params = arguments[0].stringify_keys!.keys - (method[:params][:required] + method[:params][:optional])).empty? 27 | raise "Unknown #{"param".pluralize unknown_params.size} '#{unknown_params.join " "}' passed to DisqusRails::Api::#{section_name}.#{method_name}" 28 | end 29 | 30 | arguments[0][:api_key] = PUBLIC_KEY 31 | if method[:requires_authentication] 32 | arguments[0][:access_token] = ACCESS_TOKEN 33 | end 34 | response = DisqusRails::Api.send method[:method].downcase, arguments[0], method[:url] 35 | 36 | arguments[0].delete(:api_key) 37 | arguments[0].delete(:access_token) 38 | 39 | response 40 | end 41 | end 42 | end 43 | 44 | class << self 45 | 46 | def parse_response(response) 47 | if response[:code] > 0 48 | raise "Error, Disqus responsed with code #{response[:code]} - #{response[:response]}" 49 | else 50 | response.delete(:code) 51 | response 52 | end 53 | end 54 | 55 | def get(args, url) 56 | populated_url = url + '?' 57 | args.each{ |k, v| populated_url += "#{k}=#{v}&" } 58 | url.chomp('&') 59 | 60 | parse_response JSON.parse( open(populated_url){ |u| u.read } ).symbolize_keys 61 | end 62 | 63 | def post(args, url) 64 | uri = URI.parse(url) 65 | http = Net::HTTP.new(uri.host, uri.port) 66 | http.use_ssl = true 67 | http.verify_mode = OpenSSL::SSL::VERIFY_NONE 68 | 69 | request = Net::HTTP::Post.new(url) 70 | request.add_field('Content-Type', 'application/json') 71 | params_string = args.inject(""){|params_string, (param_name, param_value)| params_string += "#{param_name}=#{param_value}&" } 72 | request.body = params_string 73 | 74 | parse_response JSON.parse(http.request(request).body).symbolize_keys 75 | end 76 | 77 | end 78 | end 79 | end 80 | -------------------------------------------------------------------------------- /lib/disqus_rails/api.yml: -------------------------------------------------------------------------------- 1 | --- 2 | Applications: 3 | listUsage: 4 | params: 5 | required: [] 6 | optional: 7 | - application 8 | - days 9 | requires_authentication: true 10 | url: https://disqus.com/api/3.0/applications/listUsage.json 11 | method: GET 12 | Blacklists: 13 | add: 14 | params: 15 | required: 16 | - forum 17 | optional: 18 | - domain 19 | - word 20 | - retroactive 21 | - notes 22 | - ip 23 | - user 24 | - email 25 | requires_authentication: true 26 | url: https://disqus.com/api/3.0/blacklists/add.json 27 | method: POST 28 | list: 29 | params: 30 | required: 31 | - forum 32 | optional: 33 | - since 34 | - related 35 | - cursor 36 | - limit 37 | - since_id 38 | - query 39 | - type 40 | - order 41 | requires_authentication: true 42 | url: https://disqus.com/api/3.0/blacklists/list.json 43 | method: GET 44 | remove: 45 | params: 46 | required: 47 | - forum 48 | optional: 49 | - domain 50 | - word 51 | - ip 52 | - user 53 | - email 54 | requires_authentication: true 55 | url: https://disqus.com/api/3.0/blacklists/remove.json 56 | method: POST 57 | Categories: 58 | create: 59 | params: 60 | required: 61 | - forum 62 | - title 63 | optional: 64 | - default 65 | requires_authentication: true 66 | url: https://disqus.com/api/3.0/categories/create.json 67 | method: POST 68 | details: 69 | params: 70 | required: 71 | - category 72 | optional: [] 73 | requires_authentication: false 74 | url: https://disqus.com/api/3.0/categories/details.json 75 | method: GET 76 | list: 77 | params: 78 | required: [] 79 | optional: 80 | - forum 81 | - since_id 82 | - cursor 83 | - limit 84 | - order 85 | requires_authentication: false 86 | url: https://disqus.com/api/3.0/categories/list.json 87 | method: GET 88 | listPosts: 89 | params: 90 | required: 91 | - category 92 | optional: 93 | - since 94 | - related 95 | - cursor 96 | - limit 97 | - query 98 | - include 99 | - order 100 | requires_authentication: false 101 | url: https://disqus.com/api/3.0/categories/listPosts.json 102 | method: GET 103 | listThreads: 104 | params: 105 | required: 106 | - category 107 | optional: 108 | - since 109 | - related 110 | - cursor 111 | - limit 112 | - order 113 | requires_authentication: false 114 | url: https://disqus.com/api/3.0/categories/listThreads.json 115 | method: GET 116 | Discovery: 117 | listAds: 118 | params: 119 | required: [] 120 | optional: 121 | - active 122 | requires_authentication: false 123 | url: https://disqus.com/api/3.0/discovery/listAds.json 124 | method: GET 125 | Exports: 126 | exportForum: 127 | params: 128 | required: 129 | - forum 130 | optional: 131 | - format 132 | requires_authentication: true 133 | url: https://disqus.com/api/3.0/exports/exportForum.json 134 | method: POST 135 | Forums: 136 | addModerator: 137 | params: 138 | required: 139 | - user 140 | - forum 141 | optional: [] 142 | requires_authentication: true 143 | url: https://disqus.com/api/3.0/forums/addModerator.json 144 | method: POST 145 | create: 146 | params: 147 | required: 148 | - website 149 | - name 150 | - short_name 151 | optional: [] 152 | requires_authentication: true 153 | url: https://disqus.com/api/3.0/forums/create.json 154 | method: POST 155 | details: 156 | params: 157 | required: 158 | - forum 159 | optional: 160 | - related 161 | requires_authentication: false 162 | url: https://disqus.com/api/3.0/forums/details.json 163 | method: GET 164 | listCategories: 165 | params: 166 | required: 167 | - forum 168 | optional: 169 | - since_id 170 | - cursor 171 | - limit 172 | - order 173 | requires_authentication: false 174 | url: https://disqus.com/api/3.0/forums/listCategories.json 175 | method: GET 176 | listModerators: 177 | params: 178 | required: 179 | - forum 180 | optional: [] 181 | requires_authentication: true 182 | url: https://disqus.com/api/3.0/forums/listModerators.json 183 | method: GET 184 | listMostActiveUsers: 185 | params: 186 | required: 187 | - forum 188 | optional: 189 | - cursor 190 | - limit 191 | - order 192 | requires_authentication: false 193 | url: https://disqus.com/api/3.0/forums/listMostActiveUsers.json 194 | method: GET 195 | listMostLikedUsers: 196 | params: 197 | required: 198 | - forum 199 | optional: 200 | - cursor 201 | - limit 202 | - order 203 | requires_authentication: false 204 | url: https://disqus.com/api/3.0/forums/listMostLikedUsers.json 205 | method: GET 206 | listPosts: 207 | params: 208 | required: 209 | - forum 210 | optional: 211 | - since 212 | - related 213 | - cursor 214 | - limit 215 | - query 216 | - include 217 | - order 218 | requires_authentication: false 219 | url: https://disqus.com/api/3.0/forums/listPosts.json 220 | method: GET 221 | listThreads: 222 | params: 223 | required: 224 | - forum 225 | optional: 226 | - thread 227 | - since 228 | - related 229 | - cursor 230 | - limit 231 | - include 232 | - order 233 | requires_authentication: false 234 | url: https://disqus.com/api/3.0/forums/listThreads.json 235 | method: GET 236 | listUsers: 237 | params: 238 | required: 239 | - forum 240 | optional: 241 | - since_id 242 | - cursor 243 | - limit 244 | - order 245 | requires_authentication: false 246 | url: https://disqus.com/api/3.0/forums/listUsers.json 247 | method: GET 248 | removeModerator: 249 | params: 250 | required: 251 | - moderator 252 | optional: [] 253 | requires_authentication: true 254 | url: https://disqus.com/api/3.0/forums/removeModerator.json 255 | method: POST 256 | Imports: 257 | details: 258 | params: 259 | required: 260 | - group 261 | - forum 262 | optional: [] 263 | requires_authentication: true 264 | url: https://disqus.com/api/3.0/imports/details.json 265 | method: GET 266 | list: 267 | params: 268 | required: 269 | - forum 270 | optional: 271 | - cursor 272 | requires_authentication: true 273 | url: https://disqus.com/api/3.0/imports/list.json 274 | method: GET 275 | Posts: 276 | approve: 277 | params: 278 | required: 279 | - post 280 | optional: [] 281 | requires_authentication: true 282 | url: https://disqus.com/api/3.0/posts/approve.json 283 | method: POST 284 | create: 285 | params: 286 | required: 287 | - message 288 | optional: 289 | - parent 290 | - thread 291 | - author_email 292 | - author_name 293 | - state 294 | - author_url 295 | - date 296 | - ip_address 297 | requires_authentication: true 298 | url: https://disqus.com/api/3.0/posts/create.json 299 | method: POST 300 | details: 301 | params: 302 | required: 303 | - post 304 | optional: 305 | - related 306 | requires_authentication: false 307 | url: https://disqus.com/api/3.0/posts/details.json 308 | method: GET 309 | getContext: 310 | params: 311 | required: 312 | - post 313 | optional: 314 | - depth 315 | - related 316 | requires_authentication: false 317 | url: https://disqus.com/api/3.0/posts/getContext.json 318 | method: GET 319 | highlight: 320 | params: 321 | required: 322 | - post 323 | optional: [] 324 | requires_authentication: true 325 | url: https://disqus.com/api/3.0/posts/highlight.json 326 | method: POST 327 | list: 328 | params: 329 | required: [] 330 | optional: 331 | - category 332 | - thread 333 | - forum 334 | - since 335 | - related 336 | - cursor 337 | - limit 338 | - offset 339 | - query 340 | - include 341 | - order 342 | requires_authentication: false 343 | url: https://disqus.com/api/3.0/posts/list.json 344 | method: GET 345 | listPopular: 346 | params: 347 | required: [] 348 | optional: 349 | - category 350 | - interval 351 | - thread 352 | - forum 353 | - since 354 | - related 355 | - limit 356 | - offset 357 | - query 358 | - include 359 | - order 360 | requires_authentication: false 361 | url: https://disqus.com/api/3.0/posts/listPopular.json 362 | method: GET 363 | remove: 364 | params: 365 | required: 366 | - post 367 | optional: [] 368 | requires_authentication: true 369 | url: https://disqus.com/api/3.0/posts/remove.json 370 | method: POST 371 | report: 372 | params: 373 | required: 374 | - post 375 | optional: [] 376 | requires_authentication: false 377 | url: https://disqus.com/api/3.0/posts/report.json 378 | method: POST 379 | restore: 380 | params: 381 | required: 382 | - post 383 | optional: [] 384 | requires_authentication: true 385 | url: https://disqus.com/api/3.0/posts/restore.json 386 | method: POST 387 | spam: 388 | params: 389 | required: 390 | - post 391 | optional: [] 392 | requires_authentication: true 393 | url: https://disqus.com/api/3.0/posts/spam.json 394 | method: POST 395 | unhighlight: 396 | params: 397 | required: 398 | - post 399 | optional: [] 400 | requires_authentication: true 401 | url: https://disqus.com/api/3.0/posts/unhighlight.json 402 | method: POST 403 | update: 404 | params: 405 | required: 406 | - message 407 | - post 408 | optional: [] 409 | requires_authentication: true 410 | url: https://disqus.com/api/3.0/posts/update.json 411 | method: POST 412 | vote: 413 | params: 414 | required: 415 | - vote 416 | - post 417 | optional: [] 418 | requires_authentication: false 419 | url: https://disqus.com/api/3.0/posts/vote.json 420 | method: POST 421 | Reactions: 422 | details: 423 | params: 424 | required: 425 | - reaction 426 | - forum 427 | optional: [] 428 | requires_authentication: false 429 | url: https://disqus.com/api/3.0/reactions/details.json 430 | method: GET 431 | list: 432 | params: 433 | required: 434 | - forum 435 | optional: 436 | - since_id 437 | - cursor 438 | - limit 439 | - related 440 | - order 441 | requires_authentication: false 442 | url: https://disqus.com/api/3.0/reactions/list.json 443 | method: POST, GET 444 | remove: 445 | params: 446 | required: 447 | - reaction 448 | - forum 449 | optional: [] 450 | requires_authentication: true 451 | url: https://disqus.com/api/3.0/reactions/remove.json 452 | method: POST 453 | restore: 454 | params: 455 | required: 456 | - reaction 457 | - forum 458 | optional: [] 459 | requires_authentication: true 460 | url: https://disqus.com/api/3.0/reactions/restore.json 461 | method: POST 462 | Reports: 463 | domains: 464 | params: 465 | required: [] 466 | optional: 467 | - limit 468 | - page 469 | - forum 470 | requires_authentication: false 471 | url: https://disqus.com/api/3.0/reports/domains.json 472 | method: GET 473 | threads: 474 | params: 475 | required: [] 476 | optional: 477 | - limit 478 | - page 479 | - forum 480 | requires_authentication: false 481 | url: https://disqus.com/api/3.0/reports/threads.json 482 | method: GET 483 | Threads: 484 | close: 485 | params: 486 | required: 487 | - thread 488 | optional: 489 | - forum 490 | requires_authentication: true 491 | url: https://disqus.com/api/3.0/threads/close.json 492 | method: POST 493 | create: 494 | params: 495 | required: 496 | - forum 497 | - title 498 | optional: 499 | - category 500 | - url 501 | - date 502 | - message 503 | - identifier 504 | - slug 505 | requires_authentication: true 506 | url: https://disqus.com/api/3.0/threads/create.json 507 | method: POST 508 | details: 509 | params: 510 | required: 511 | - thread 512 | optional: 513 | - related 514 | - forum 515 | requires_authentication: false 516 | url: https://disqus.com/api/3.0/threads/details.json 517 | method: GET 518 | list: 519 | params: 520 | required: [] 521 | optional: 522 | - category 523 | - forum 524 | - thread 525 | - author 526 | - since 527 | - related 528 | - cursor 529 | - limit 530 | - include 531 | - order 532 | - thread:ident 533 | requires_authentication: false 534 | url: https://disqus.com/api/3.0/threads/list.json 535 | method: GET 536 | listHot: 537 | params: 538 | required: [] 539 | optional: 540 | - category 541 | - forum 542 | - author 543 | - related 544 | - limit 545 | - include 546 | requires_authentication: false 547 | url: https://disqus.com/api/3.0/threads/listHot.json 548 | method: GET 549 | listPopular: 550 | params: 551 | required: [] 552 | optional: 553 | - category 554 | - interval 555 | - forum 556 | - author 557 | - since 558 | - related 559 | - limit 560 | - with_top_post 561 | - include 562 | requires_authentication: false 563 | url: https://disqus.com/api/3.0/threads/listPopular.json 564 | method: GET 565 | listPosts: 566 | params: 567 | required: 568 | - thread 569 | optional: 570 | - forum 571 | - since 572 | - related 573 | - cursor 574 | - limit 575 | - query 576 | - include 577 | - order 578 | requires_authentication: false 579 | url: https://disqus.com/api/3.0/threads/listPosts.json 580 | method: GET 581 | listReactions: 582 | params: 583 | required: 584 | - thread 585 | optional: 586 | - forum 587 | - cursor 588 | - limit 589 | requires_authentication: false 590 | url: https://disqus.com/api/3.0/threads/listReactions.json 591 | method: GET 592 | open: 593 | params: 594 | required: 595 | - thread 596 | optional: 597 | - forum 598 | requires_authentication: true 599 | url: https://disqus.com/api/3.0/threads/open.json 600 | method: POST 601 | remove: 602 | params: 603 | required: 604 | - thread 605 | optional: 606 | - forum 607 | requires_authentication: true 608 | url: https://disqus.com/api/3.0/threads/remove.json 609 | method: POST 610 | restore: 611 | params: 612 | required: 613 | - thread 614 | optional: 615 | - forum 616 | requires_authentication: true 617 | url: https://disqus.com/api/3.0/threads/restore.json 618 | method: POST 619 | set: 620 | params: 621 | required: 622 | - thread 623 | optional: 624 | - related 625 | - forum 626 | requires_authentication: false 627 | url: https://disqus.com/api/3.0/threads/set.json 628 | method: GET 629 | subscribe: 630 | params: 631 | required: 632 | - thread 633 | optional: 634 | - email 635 | requires_authentication: true 636 | url: https://disqus.com/api/3.0/threads/subscribe.json 637 | method: POST 638 | unsubscribe: 639 | params: 640 | required: 641 | - thread 642 | optional: 643 | - email 644 | requires_authentication: true 645 | url: https://disqus.com/api/3.0/threads/unsubscribe.json 646 | method: POST 647 | update: 648 | params: 649 | required: 650 | - thread 651 | optional: 652 | - category 653 | - forum 654 | - title 655 | - url 656 | - author 657 | - message 658 | - identifier 659 | - slug 660 | requires_authentication: true 661 | url: https://disqus.com/api/3.0/threads/update.json 662 | method: POST 663 | vote: 664 | params: 665 | required: 666 | - vote 667 | - thread 668 | optional: 669 | - forum 670 | requires_authentication: false 671 | url: https://disqus.com/api/3.0/threads/vote.json 672 | method: POST 673 | Trends: 674 | listThreads: 675 | params: 676 | required: [] 677 | optional: 678 | - limit 679 | - related 680 | - forum 681 | requires_authentication: false 682 | url: https://disqus.com/api/3.0/trends/listThreads.json 683 | method: GET 684 | Users: 685 | checkUsername: 686 | params: 687 | required: [] 688 | optional: 689 | - username 690 | requires_authentication: true 691 | url: https://disqus.com/api/3.0/users/checkUsername.json 692 | method: POST 693 | details: 694 | params: 695 | required: [] 696 | optional: 697 | - user 698 | requires_authentication: false 699 | url: https://disqus.com/api/3.0/users/details.json 700 | method: GET 701 | follow: 702 | params: 703 | required: 704 | - target 705 | optional: [] 706 | requires_authentication: true 707 | url: https://disqus.com/api/3.0/users/follow.json 708 | method: POST 709 | listActiveForums: 710 | params: 711 | required: [] 712 | optional: 713 | - since_id 714 | - cursor 715 | - limit 716 | - user 717 | - order 718 | requires_authentication: false 719 | url: https://disqus.com/api/3.0/users/listActiveForums.json 720 | method: GET 721 | listActiveThreads: 722 | params: 723 | required: [] 724 | optional: 725 | - forum 726 | - since 727 | - related 728 | - cursor 729 | - limit 730 | - user 731 | - include 732 | - order 733 | requires_authentication: false 734 | url: https://disqus.com/api/3.0/users/listActiveThreads.json 735 | method: GET 736 | listActivity: 737 | params: 738 | required: [] 739 | optional: 740 | - since 741 | - related 742 | - cursor 743 | - limit 744 | - user 745 | - query 746 | - include 747 | - anon_user 748 | requires_authentication: false 749 | url: https://disqus.com/api/3.0/users/listActivity.json 750 | method: GET 751 | listFollowers: 752 | params: 753 | required: [] 754 | optional: 755 | - since_id 756 | - cursor 757 | - limit 758 | - user 759 | - order 760 | requires_authentication: false 761 | url: https://disqus.com/api/3.0/users/listFollowers.json 762 | method: GET 763 | listFollowing: 764 | params: 765 | required: [] 766 | optional: 767 | - since_id 768 | - cursor 769 | - limit 770 | - user 771 | - order 772 | requires_authentication: false 773 | url: https://disqus.com/api/3.0/users/listFollowing.json 774 | method: GET 775 | listForums: 776 | params: 777 | required: [] 778 | optional: 779 | - since_id 780 | - cursor 781 | - limit 782 | - user 783 | - order 784 | requires_authentication: false 785 | url: https://disqus.com/api/3.0/users/listForums.json 786 | method: GET 787 | listMostActiveForums: 788 | params: 789 | required: [] 790 | optional: 791 | - limit 792 | - user 793 | requires_authentication: false 794 | url: https://disqus.com/api/3.0/users/listMostActiveForums.json 795 | method: GET 796 | listPosts: 797 | params: 798 | required: [] 799 | optional: 800 | - since 801 | - related 802 | - cursor 803 | - limit 804 | - user 805 | - include 806 | - order 807 | requires_authentication: false 808 | url: https://disqus.com/api/3.0/users/listPosts.json 809 | method: GET 810 | unfollow: 811 | params: 812 | required: 813 | - target 814 | optional: [] 815 | requires_authentication: true 816 | url: https://disqus.com/api/3.0/users/unfollow.json 817 | method: POST 818 | Whitelists: 819 | add: 820 | params: 821 | required: 822 | - forum 823 | optional: 824 | - notes 825 | - email 826 | - user 827 | requires_authentication: true 828 | url: https://disqus.com/api/3.0/whitelists/add.json 829 | method: POST 830 | list: 831 | params: 832 | required: 833 | - forum 834 | optional: 835 | - since 836 | - related 837 | - cursor 838 | - limit 839 | - since_id 840 | - query 841 | - type 842 | - order 843 | requires_authentication: true 844 | url: https://disqus.com/api/3.0/whitelists/list.json 845 | method: GET 846 | remove: 847 | params: 848 | required: 849 | - forum 850 | optional: 851 | - email 852 | - user 853 | requires_authentication: true 854 | url: https://disqus.com/api/3.0/whitelists/remove.json 855 | method: POST 856 | -------------------------------------------------------------------------------- /lib/disqus_rails/category.rb: -------------------------------------------------------------------------------- 1 | module DisqusRails 2 | class Category < Model 3 | attr_accessor :id 4 | :title 5 | :order 6 | :isDefault 7 | 8 | :category 9 | 10 | def threads(attributes={}) 11 | attributes[:category] = self.id 12 | Threads.new :Categories, :listThreads, attributes 13 | end 14 | 15 | def posts(attributes={}) 16 | attributes[:category] = self.id 17 | Posts.new :Categories, :listPosts, attributes 18 | end 19 | end 20 | end 21 | -------------------------------------------------------------------------------- /lib/disqus_rails/collection.rb: -------------------------------------------------------------------------------- 1 | module DisqusRails 2 | class Collection 3 | 4 | include Enumerable 5 | 6 | attr_accessor :items 7 | 8 | def self.inherited(subclass) 9 | subclass.class_exec do 10 | define_method "find_all_#{subclass.name.split(/::/).last.downcase}!".to_sym do 11 | while has_cursor_next? 12 | previous_items = @items 13 | cursor_next! 14 | @items = previous_items + @items 15 | end 16 | self 17 | end 18 | end 19 | end 20 | 21 | def initialize(api_class_name, method = :list, attributes = {}) 22 | @api_class_name = api_class_name 23 | @method = method 24 | @attributes = attributes 25 | 26 | api_class = Api.const_get api_class_name 27 | results = api_class.send method, attributes 28 | @cursor = results[:cursor].symbolize_keys 29 | 30 | @items = [] 31 | results[:response].each do |item| 32 | @items << DisqusRails.const_get(self.class.name.singularize.split(/::/).last).new(item) 33 | end 34 | end 35 | 36 | def each(&block) 37 | @items.each do |item| 38 | if block_given? 39 | block.call item 40 | else 41 | yield item 42 | end 43 | end 44 | end 45 | 46 | def has_cursor_next? 47 | @cursor[:hasNext] 48 | end 49 | 50 | def has_cursor_prev? 51 | @cursor[:hasPrev] 52 | end 53 | 54 | def cursor_next 55 | if has_cursor_next? 56 | @attributes[:cursor] = @cursor[:next] 57 | self.class.new(@api_class_name, @method, @attributes) 58 | end 59 | self 60 | end 61 | 62 | def cursor_next! 63 | if has_cursor_next? 64 | @attributes[:cursor] = @cursor[:next] 65 | initialize(@api_class_name, @method, @attributes) 66 | end 67 | self 68 | end 69 | 70 | def cursor_prev 71 | if has_cursor_prev? 72 | @attributes[:cursor] = @cursor[:prev] 73 | self.class.new(@api_class_name, @method, @attributes) 74 | end 75 | self 76 | end 77 | 78 | def cursor_prev! 79 | if has_cursor_prev? 80 | @attributes[:cursor] = @cursor[:prev] 81 | initialize(@api_class_name, @method, @attributes) 82 | end 83 | self 84 | end 85 | end 86 | 87 | %w[Posts Threads Categories Forums Users].each do |subclass| 88 | instance_eval "class DisqusRails::#{subclass} < DisqusRails::Collection; end" 89 | end 90 | end 91 | -------------------------------------------------------------------------------- /lib/disqus_rails/forum.rb: -------------------------------------------------------------------------------- 1 | module DisqusRails 2 | class Forum < Model 3 | attr_accessor :id, 4 | :name, 5 | :favicon, 6 | 7 | :founder 8 | 9 | class << self 10 | def popular(attributes={}) 11 | Forums.new :Forums, :listPopular, attributes 12 | end 13 | end 14 | 15 | def categories(attributes={}) 16 | attributes[:forum] = self.id 17 | Categories.new :Forums, :listCategories, attributes 18 | end 19 | 20 | def threads(attributes={}) 21 | attributes[:forum] = self.id 22 | Threads.new :Forums, :listThreads, attributes 23 | end 24 | 25 | def posts(attributes={}) 26 | attributes[:forum] = self.id 27 | Posts.new :Forums, :listPosts, attributes 28 | end 29 | 30 | def users(attributes={}) 31 | attributes[:forum] = self.id 32 | Users.new :Forums, :listUsers, attributes 33 | end 34 | 35 | def most_active_users(attributes={}) 36 | attributes[:forum] = self.id 37 | Users.new :Forums, :listMostActiveUsers, attributes 38 | end 39 | 40 | def most_liked_users(attributes={}) 41 | attributes[:forum] = self.id 42 | Users.new :Forums, :listMostLikedUsers, attributes 43 | end 44 | 45 | def add_moderator(user_id) 46 | update_attributes Api::Forums.add_moderator(:forum => self.id, :user => user_id) 47 | end 48 | 49 | def remove_moderator(user_id) 50 | update_attributes Api::Forums.remove_moderator(:user => user_id) 51 | end 52 | 53 | end 54 | end 55 | -------------------------------------------------------------------------------- /lib/disqus_rails/helpers.rb: -------------------------------------------------------------------------------- 1 | require 'json' 2 | require 'base64' 3 | 4 | module DisqusRails 5 | module Helpers 6 | 7 | def disqus_init(attributes={}) 8 | if attributes.has_key?(:short_name) 9 | DisqusRails.const_set('SHORT_NAME', attributes[:short_name]) 10 | else 11 | attributes[:short_name] = DisqusRails::SHORT_NAME 12 | end 13 | 14 | if attributes.has_key?(:public_key) 15 | DisqusRails.const_set('PUBLIC_KEY', attributes[:public_key]) 16 | else 17 | attributes[:public_key] = DisqusRails::PUBLIC_KEY 18 | end 19 | 20 | if attributes.has_key?(:secret_key) 21 | DisqusRails.const_set('SECRET_KEY', attributes[:secret_key]) 22 | end 23 | 24 | if attributes.has_key?(:access_token) 25 | DisqusRails.const_set('ACCESS_TOKEN', attributes[:access_token]) 26 | end 27 | 28 | if attributes.has_key?(:disquser) && attributes[:disquser].respond_to?(:disqus_params) 29 | attributes[:remote_auth_s3] = disqus_sso_script attributes[:disquser].disqus_params() 30 | end 31 | 32 | attributes.delete :disquser 33 | attributes.delete :secret_key 34 | attributes.delete :access_token 35 | 36 | javascript_tag %Q"$(document).ready(function(){ 37 | window.disqus_rails = new DisqusRails(#{attributes.to_json}); 38 | });" 39 | end 40 | 41 | 42 | def disqus_sso_script(disquser) 43 | # encode the data to base64 44 | message = Base64.strict_encode64(disquser.to_json) 45 | 46 | # generate a timestamp for signing the message 47 | timestamp = Time.now.to_i 48 | 49 | # generate our hmac signature 50 | digest = OpenSSL::Digest.new('sha1') 51 | signature = OpenSSL::HMAC.hexdigest(digest, SECRET_KEY, "#{message} #{timestamp}") 52 | 53 | # return a script tag to insert the sso message 54 | "#{message} #{signature} #{timestamp}" 55 | end 56 | 57 | 58 | def disqus_thread(disqusable_id=nil, disqusable_title=nil) 59 | concat content_tag(:div, "", :id => "disqus_thread") 60 | concat javascript_tag %Q"$(document).ready(function(){ 61 | disqus_rails.draw_thread(\"#{disqusable_id}\", \"#{disqusable_title}\"); 62 | });" 63 | end 64 | end 65 | end 66 | -------------------------------------------------------------------------------- /lib/disqus_rails/model.rb: -------------------------------------------------------------------------------- 1 | module DisqusRails 2 | class Model 3 | 4 | def self.inherited(subclass) 5 | subclass_name = subclass.name.split(/::/).last 6 | api_name = Api.const_get subclass_name.pluralize 7 | collection_name = DisqusRails.const_get subclass_name.pluralize 8 | 9 | subclass.class_exec do 10 | 11 | define_singleton_method :find do |id| 12 | new(api_name.details(subclass_name.downcase.to_sym => id)[:response]) 13 | end 14 | 15 | if api_name.respond_to?(:list) 16 | define_singleton_method :where do |attributes={}| 17 | collection_name.new subclass_name.pluralize.to_sym, :list, attributes 18 | end 19 | end 20 | 21 | if api_name.respond_to?(:create) 22 | define_singleton_method :create do |attributes={}| 23 | subclass.new(api_name.create(attributes)[:response]) 24 | end 25 | end 26 | 27 | define_method :reload do 28 | subclass.new(api_name.details(subclass_name.downcase.to_sym => self.id)[:response]) 29 | end 30 | end 31 | end 32 | 33 | def initialize(attributes={}) 34 | attributes.each do |attr_name, attr_value| 35 | instance_variable_set("@#{attr_name}".to_sym, attr_value) 36 | end 37 | end 38 | 39 | def update_attributes(attributes={}) 40 | attributes.each do |attr_name, attr_value| 41 | send("#{attr_name}=", attr_value) 42 | end 43 | end 44 | end 45 | end 46 | -------------------------------------------------------------------------------- /lib/disqus_rails/post.rb: -------------------------------------------------------------------------------- 1 | module DisqusRails 2 | class Post < Model 3 | attr_accessor :id, 4 | :isJuliaFlagged, 5 | :isFlagged, 6 | :parent, 7 | :media, 8 | :isApproved, 9 | :dislikes, 10 | :raw_message, 11 | :points, 12 | :createdAt, 13 | :isEdited, 14 | :message, 15 | :isHighlighted, 16 | :ipAddress, 17 | :isSpam, 18 | :isDeleted, 19 | :likes, 20 | 21 | :author, 22 | :thread, 23 | :forum 24 | 25 | class << self 26 | 27 | def popular(attributes={}) 28 | Posts.new :Posts, :listPopular, attributes 29 | end 30 | 31 | end 32 | 33 | def initialize(attributes={}) 34 | attributes.each do |attr_name, attr_value| 35 | if attr_name == "author" 36 | @author = DisqusRails::User.new(attr_value) 37 | else 38 | instance_variable_set("@#{attr_name}".to_sym, attr_value) 39 | end 40 | end 41 | end 42 | 43 | def update(message) 44 | update_attributes Api::Posts.update(:post => self.id, :message => message)[:response] 45 | end 46 | 47 | def remove 48 | if Api::Posts.remove(:post => self.id) 49 | update_attributes :isDeleted => true 50 | end 51 | end 52 | 53 | def restore 54 | if Api::Posts.restore(:post => self.id) 55 | update_attributes :isDeleted => false 56 | end 57 | end 58 | 59 | def approve 60 | if Api::Posts.approve(:post => self.id) 61 | update_attributes :isApproved => true 62 | end 63 | end 64 | 65 | def getContext(depth = nil, related = nil) 66 | update_attributes Api::Posts.getContext(:post => self.id, :depth => depth, :related => related) 67 | end 68 | 69 | def spam 70 | if Api::Posts.spam(:post => self.id) 71 | update_attributes :isSpam => true 72 | end 73 | end 74 | 75 | def report 76 | if Api::Posts.report(:post => self.id) 77 | update_attributes :isFlagged => true 78 | end 79 | end 80 | 81 | def highlight 82 | if Api::Posts.highlight(:post => self.id) 83 | update_attributes :isHighlighted => true 84 | end 85 | end 86 | 87 | def unhighlight 88 | if Api::Posts.unhighlight(:post => self.id) 89 | update_attributes :isHighlighted => false 90 | end 91 | end 92 | 93 | def vote=(vote) 94 | @vote = vote 95 | end 96 | def vote(*args) 97 | if args.empty? 98 | @vote 99 | else 100 | if Api::Posts.vote(:post => self.id, :vote => args.first) 101 | update_attributes :vote => args.first 102 | end 103 | end 104 | end 105 | end 106 | end 107 | -------------------------------------------------------------------------------- /lib/disqus_rails/thread.rb: -------------------------------------------------------------------------------- 1 | module DisqusRails 2 | class Thread < Model 3 | attr_accessor :id, 4 | :title, 5 | :identifiers, 6 | :dislikes, 7 | :isDeleted, 8 | :userScore, 9 | :isClosed, 10 | :posts_count, 11 | :link, 12 | :likes, 13 | :message, 14 | :ipAddress, 15 | :slug, 16 | :createdAt, 17 | :disqusable_id, 18 | :feed, 19 | :userSubscription, 20 | 21 | :category, 22 | :forum, 23 | :reactions, 24 | :author 25 | 26 | def initialize(attributes={}) 27 | attributes.each do |attr_name, attr_value| 28 | if attr_name == "identifiers" 29 | unless attr_value.empty? 30 | instance_variable_set("@disqusable_id".to_sym, attr_value[0]) 31 | end 32 | elsif attr_name == "posts" 33 | instance_variable_set("@posts_count".to_sym, attr_value) 34 | else 35 | instance_variable_set("@#{attr_name}".to_sym, attr_value) 36 | end 37 | end 38 | end 39 | 40 | def update_attributes(attributes={}) 41 | attributes.each do |attr_name, attr_value| 42 | if attr_name == "identifiers" 43 | unless attr_value.empty? 44 | @disqusable_id = attr_value[0] 45 | end 46 | elsif attr_name == "posts" 47 | @posts_count = attr_value 48 | else 49 | send("#{attr_name}=", attr_value) 50 | end 51 | end 52 | end 53 | 54 | class << self 55 | 56 | def popular(attributes={}) 57 | attributes[:forum] = @forum 58 | Threads.new :Threads, :listPopular, attributes 59 | end 60 | 61 | def hot(attributes={}) 62 | attributes[:forum] = @forum 63 | Threads.new :Threads, :listHot, attributes 64 | end 65 | 66 | def find_by_ids(attributes={}) 67 | Threads.new :Threads, :set, attributes 68 | end 69 | 70 | def find_by_ident(disqusable_id) 71 | Thread.where(:forum => @forum, :"thread:ident" => disqusable_id).first 72 | end 73 | end 74 | 75 | def posts(attributes={}) 76 | attributes[:thread] = self.id 77 | Posts.new :Threads, :listPosts, attributes 78 | end 79 | 80 | def create(attributes={}) 81 | update_attributes Api::Threads.create(attributes) 82 | end 83 | 84 | def open 85 | if Api::Threads.open(:thread => self.id) 86 | update_attributes :isClosed => false 87 | end 88 | end 89 | 90 | def close 91 | if Api::Threads.close(:thread => self.id) 92 | update_attributes :isClosed => true 93 | end 94 | end 95 | 96 | def subscribe(email=nil) 97 | Api::Threads.subscribe(:thread => self.id, :email => email) 98 | end 99 | 100 | def unsubscribe(email=nil) 101 | Api::Threads.unsubscribe(:thread => self.id, :email => email) 102 | end 103 | 104 | def update(attributes={}) 105 | attributes[:thread] = self.id 106 | update_attributes Api::Threads.update(attributes)[:response] 107 | end 108 | 109 | def remove 110 | update_attributes Api::Threads.remove(:thread => self.id)[:response][0] 111 | end 112 | 113 | def restore 114 | update_attributes Api::Threads.restore(:thread => self.id) 115 | end 116 | 117 | def vote=(vote) 118 | @vote = vote 119 | end 120 | def vote(*args) 121 | if args.empty? 122 | @vote 123 | else 124 | if Api::Threads.vote(:thread => self.id, :vote => args.first) 125 | update_attributes :vote => args.first 126 | end 127 | end 128 | end 129 | end 130 | end 131 | -------------------------------------------------------------------------------- /lib/disqus_rails/user.rb: -------------------------------------------------------------------------------- 1 | module DisqusRails 2 | class User < Model 3 | attr_accessor :id, 4 | :username, 5 | :about, 6 | :name, 7 | :url, 8 | :isFollowing, 9 | :isFollowedBy, 10 | :profileUrl, 11 | :emailHash, 12 | :avatar, 13 | :isAnonymous, 14 | :email, 15 | :disquser_id 16 | 17 | class << self 18 | 19 | def popular(attributes={}) 20 | Users.new :Threads, :listPopular, attributes 21 | end 22 | 23 | end 24 | 25 | def change_name(name) 26 | Api::Users.checkUsername(:user => self.id, :username => name) 27 | end 28 | 29 | def follow(target_id) 30 | Api::Users.follow(:user => self.id, :target => target_id) 31 | end 32 | 33 | def unfollow(target_id) 34 | Api::Users.unfollow(:user => self.id, :target => target_id) 35 | end 36 | 37 | 38 | def forums(attributes={}) 39 | attributes[:user] = self.id 40 | Forums.new :Users, :listForums, attributes 41 | end 42 | 43 | def active_forums(attributes={}) 44 | attributes[:user] = self.id 45 | Forums.new :Users, :listActiveForums, attributes 46 | end 47 | 48 | def active_threads(attributes={}) 49 | attributes[:user] = self.id 50 | Threads.new :Users, :listActiveThreads, attributes 51 | end 52 | 53 | def posts(attributes={}) 54 | attributes[:user] = self.id 55 | Posts.new :Users, :listPosts, attributes 56 | end 57 | 58 | def followers(attributes={}) 59 | attributes[:user] = self.id 60 | Users.new :Users, :listFollowers, attributes 61 | end 62 | 63 | def following(attributes={}) 64 | attributes[:user] = self.id 65 | Users.new :Users, :listFollowing, attributes 66 | end 67 | end 68 | end 69 | -------------------------------------------------------------------------------- /lib/disqus_rails/version.rb: -------------------------------------------------------------------------------- 1 | module DisqusRails 2 | VERSION = "0.0.7" 3 | end 4 | -------------------------------------------------------------------------------- /spec/disqus_rails/active_record/acts_as_disqusable_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | 3 | describe DisqusRails::Disqusable do 4 | 5 | before :all do 6 | ::ActiveRecord::Base.extend DisqusRails::Disqusable::ActiveRecordMethods 7 | 8 | m = ActiveRecord::Migration.new 9 | m.verbose = false 10 | m.create_table :stubbed_disqusables 11 | end 12 | 13 | after :all do 14 | m = ActiveRecord::Migration.new 15 | m.verbose = false 16 | m.drop_table :stubbed_disqusables 17 | end 18 | 19 | let(:stubbed_disqusable_instance) do 20 | class StubbedDisqusable < ActiveRecord::Base 21 | acts_as_disqusable 22 | end 23 | 24 | StubbedDisqusable.new() 25 | end 26 | 27 | let!(:thread_hash) do 28 | { 29 | :code => 0, 30 | :response => { 31 | "category" => "1", 32 | "reactions" => 0, 33 | "identifiers" => [ 34 | 2 35 | ], 36 | "forum" => "bobross", 37 | "title" => "Hello World", 38 | "dislikes" => 0, 39 | "isDeleted" => false, 40 | "author" => "1", 41 | "userScore" => 0, 42 | "id" => "3", 43 | "isClosed" => false, 44 | "posts" => 0, 45 | "link" => "null", 46 | "likes" => 0, 47 | "message" => "", 48 | "ipAddress" => "127.0.0.1", 49 | "slug" => "hello_world", 50 | "createdAt" => "2011-11-02T02:22:41" 51 | } 52 | } 53 | end 54 | 55 | let(:thread) do 56 | DisqusRails::Thread.new thread_hash[:response] 57 | end 58 | 59 | let(:one_entrance_collection_hash) do 60 | { 61 | :cursor => { 62 | "prev" => "null", 63 | "hasNext" => true, 64 | "next" => "1368581473195442:0:0", 65 | "hasPrev" => false, 66 | "total" => "null", 67 | "id" => "1368581473195442:0:0", 68 | "more" => true 69 | }, 70 | :code => 0, 71 | :response => [ 72 | { 73 | "category" => "1", 74 | "reactions" => 0, 75 | "identifiers" => [ 76 | 1 77 | ], 78 | "forum" => "bobross", 79 | "title" => "Hello World", 80 | "dislikes" => 0, 81 | "isDeleted" => false, 82 | "author" => "1", 83 | "userScore" => 0, 84 | "id" => "3", 85 | "isClosed" => false, 86 | "posts" => 0, 87 | "link" => "null", 88 | "likes" => 0, 89 | "message" => "", 90 | "ipAddress" => "127.0.0.1", 91 | "slug" => "hello_world", 92 | "createdAt" => "2011-11-02T02:22:41" 93 | } 94 | ] 95 | } 96 | end 97 | 98 | it "should create disqus_thread method on active record model that ran acts_as_disqusable method in class definition" do 99 | stubbed_disqusable_instance.should respond_to("disqus_thread") 100 | end 101 | 102 | it "should return threads as DisqusRails::Threads class invoking disqus_thread method" do 103 | stubbed_disqusable_instance.save() 104 | 105 | DisqusRails::Api::Threads 106 | .should_receive(:list) 107 | .with(:forum=>nil, :"thread:ident"=>stubbed_disqusable_instance.id) 108 | .and_return(one_entrance_collection_hash) 109 | 110 | stubbed_disqusable_instance.disqus_thread.should be_a_kind_of(DisqusRails::Thread) 111 | end 112 | 113 | it "should return disqusable model instance that linked to that disqus thread" do 114 | stubbed_disqusable_instance.save() 115 | thread.disqusable.should be_a_kind_of(StubbedDisqusable) 116 | end 117 | 118 | end 119 | -------------------------------------------------------------------------------- /spec/disqus_rails/active_record/acts_as_disquser_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | 3 | describe DisqusRails::Disquser do 4 | 5 | before :all do 6 | ::ActiveRecord::Base.extend DisqusRails::Disquser::ActiveRecordMethods 7 | 8 | m = ActiveRecord::Migration.new 9 | m.verbose = false 10 | m.create_table :stubbed_disqusers 11 | end 12 | 13 | after :all do 14 | m = ActiveRecord::Migration.new 15 | m.verbose = false 16 | m.drop_table :stubbed_disqusers 17 | end 18 | 19 | let(:stubbed_disquser_instance) do 20 | class StubbedDisquser < ActiveRecord::Base 21 | acts_as_disquser :username => :fullname, :email => :email, :avatar => Proc.new{ "pic.jpg" } 22 | 23 | def fullname 24 | "stubbed_users_fullname" 25 | end 26 | 27 | def email 28 | "stubbed_users_email" 29 | end 30 | end 31 | 32 | StubbedDisquser.new() 33 | end 34 | 35 | it "should create disqus_params method on active record disquser model" do 36 | stubbed_disquser_instance.should respond_to("disqus_params") 37 | end 38 | 39 | it "should return disqus params for user to use in disqus_init helper for sso" do 40 | stubbed_disquser_instance.save() 41 | stubbed_disquser_instance.disqus_params[:username].should == "stubbed_users_fullname" 42 | end 43 | 44 | end 45 | -------------------------------------------------------------------------------- /spec/disqus_rails/api_spec.rb: -------------------------------------------------------------------------------- 1 | #require 'spec_helper' 2 | # 3 | #describe DisqusRails::Api do 4 | # 5 | # before :all do 6 | # DisqusRails.setup do |config| 7 | # config::SHORT_NAME = "stubbed_short_name" 8 | # config::SECRET_KEY = "stubbed_secret_key" 9 | # config::PUBLIC_KEY = "stubbed_public_key" 10 | # config::ACCESS_TOKEN = "stubbed_access_token" 11 | # end 12 | # end 13 | # 14 | # let(:forum){:stubbed_forum_name} 15 | # describe DisqusRails::Api::Posts do 16 | # 17 | # it "lists posts" do 18 | # DisqusRails::Api::Posts.list 19 | # end 20 | # 21 | # #when spec was written disqus returned 500 and apparently got same server problems 22 | # #it "lists popular posts" do 23 | # # DisqusRails::Api::Posts.listPopular 24 | # #end 25 | # 26 | # it "create post" do 27 | # response = DisqusRails::Api::Posts.create(:message => "test message9...", :thread => 1329721966) 28 | # post = response[:response]["id"] 29 | # 30 | # describe "in context of created post" do 31 | # it "show created post details" do 32 | # DisqusRails::Api::Posts.details(:post => post) 33 | # end 34 | # 35 | # it "update created post" do 36 | # DisqusRails::Api::Posts.update(:post => post, :message => "new updated message...") 37 | # end 38 | # 39 | # it "approve post" do 40 | # DisqusRails::Api::Posts.approve(:post => post) 41 | # end 42 | # 43 | # it "highlight post" do 44 | # DisqusRails::Api::Posts.highlight(:post => post) 45 | # end 46 | # 47 | # it "unhighlight post" do 48 | # DisqusRails::Api::Posts.unhighlight(:post => post) 49 | # end 50 | # 51 | # it "vote for post" do 52 | # DisqusRails::Api::Posts.vote(:post => post, :vote => 1) 53 | # end 54 | # 55 | # it "report a post" do 56 | # DisqusRails::Api::Posts.report(:post => post) 57 | # end 58 | # 59 | # it "mark post as spam" do 60 | # DisqusRails::Api::Posts.spam(:post => post) 61 | # end 62 | # 63 | # it "remove a post" do 64 | # DisqusRails::Api::Posts.remove(:post => post) 65 | # end 66 | # 67 | # it "restore a post" do 68 | # DisqusRails::Api::Posts.remove(:post => post) 69 | # end 70 | # 71 | # it "remove a post" do 72 | # DisqusRails::Api::Posts.remove(:post => post) 73 | # end 74 | # end 75 | # end 76 | # end 77 | # 78 | # describe DisqusRails::Api::Threads do 79 | # it "lists threads" do 80 | # DisqusRails::Api::Threads.list 81 | # end 82 | # 83 | # it "lists popular threads" do 84 | # DisqusRails::Api::Threads.listPopular(:forum => forum) 85 | # end 86 | # 87 | # it "lists popular threads" do 88 | # DisqusRails::Api::Threads.listPopular(:forum => forum) 89 | # end 90 | # 91 | # it "lists hot threads" do 92 | # DisqusRails::Api::Threads.listHot(:forum => forum) 93 | # end 94 | # 95 | # it "create new thread" do 96 | # result = DisqusRails::Api::Threads.create(:forum => forum, :title => "new title") 97 | # thread = result[:response]["id"] 98 | # 99 | # describe "in context of new created thread" do 100 | # 101 | # it "show thread details" do 102 | # DisqusRails::Api::Threads.details :thread => thread 103 | # end 104 | # 105 | # it "list threads posts" do 106 | # DisqusRails::Api::Threads.listPosts :thread => thread 107 | # end 108 | # 109 | # it "list threads reactions" do 110 | # DisqusRails::Api::Threads.listReactions :thread => thread 111 | # end 112 | # 113 | # it "subscribe to thread" do 114 | # DisqusRails::Api::Threads.subscribe :thread => thread 115 | # end 116 | # 117 | # it "unsubscribe from thread" do 118 | # DisqusRails::Api::Threads.unsubscribe :thread => thread 119 | # end 120 | # 121 | # it "vote for thread" do 122 | # DisqusRails::Api::Threads.vote :thread => thread, :vote => 1 123 | # end 124 | # 125 | # it "updates thread" do 126 | # DisqusRails::Api::Threads.update(:thread => thread, :title => "new updated thread") 127 | # end 128 | # 129 | # it "close thread" do 130 | # DisqusRails::Api::Threads.close(:thread => thread) 131 | # end 132 | # 133 | # it "open thread" do 134 | # DisqusRails::Api::Threads.open(:thread => thread) 135 | # end 136 | # 137 | # it "remove thread" do 138 | # DisqusRails::Api::Threads.remove(:thread => thread) 139 | # end 140 | # 141 | # it "restore thread" do 142 | # DisqusRails::Api::Threads.restore(:thread => thread) 143 | # end 144 | # 145 | # it "remove thread" do 146 | # DisqusRails::Api::Threads.remove(:thread => thread) 147 | # end 148 | # end 149 | # end 150 | # end 151 | # 152 | # describe DisqusRails::Api::Users do 153 | # let(:user){15211395} 154 | # 155 | # it "show user details" do 156 | # DisqusRails::Api::Users.details(:user => user) 157 | # end 158 | # 159 | # it "follow" do 160 | # DisqusRails::Api::Users.follow(:target => 2) 161 | # end 162 | # 163 | # it "unfollow" do 164 | # DisqusRails::Api::Users.unfollow(:target => 2) 165 | # end 166 | # 167 | # it "change username" do 168 | # DisqusRails::Api::Users.checkUsername(:username => "stubbed_username") 169 | # end 170 | # 171 | # it "list user active forums" do 172 | # DisqusRails::Api::Users.listActiveForums(:user => user) 173 | # end 174 | # 175 | # it "list user forums" do 176 | # DisqusRails::Api::Users.listForums(:user => user) 177 | # end 178 | # 179 | # it "list user most active forums" do 180 | # DisqusRails::Api::Users.listMostActiveForums(:user => user) 181 | # end 182 | # 183 | # it "list user active threads" do 184 | # DisqusRails::Api::Users.listActiveThreads(:user => user) 185 | # end 186 | # 187 | # it "list user activity" do 188 | # DisqusRails::Api::Users.listActivity(:user => user) 189 | # end 190 | # 191 | # it "list user followers" do 192 | # DisqusRails::Api::Users.listFollowers(:user => user) 193 | # end 194 | # 195 | # it "list user following" do 196 | # DisqusRails::Api::Users.listFollowing(:user => user) 197 | # end 198 | # 199 | # it "list user posts" do 200 | # DisqusRails::Api::Users.listPosts(:user => user) 201 | # end 202 | # end 203 | # 204 | # describe DisqusRails::Api::Forums do 205 | # it "show forum details" do 206 | # DisqusRails::Api::Forums.details(:forum => forum) 207 | # end 208 | # 209 | # it "list forums categories" do 210 | # DisqusRails::Api::Forums.listCategories(:forum => forum) 211 | # end 212 | # 213 | # it "list forum threads" do 214 | # DisqusRails::Api::Forums.listThreads(:forum => forum) 215 | # end 216 | # 217 | # it "list forum users" do 218 | # DisqusRails::Api::Forums.listUsers(:forum => forum) 219 | # end 220 | # 221 | # it "list forums moderators" do 222 | # DisqusRails::Api::Forums.listModerators(:forum => forum) 223 | # end 224 | # 225 | # it "list forums most active users" do 226 | # DisqusRails::Api::Forums.listMostActiveUsers(:forum => forum) 227 | # end 228 | # 229 | # it "list forums most liked users" do 230 | # DisqusRails::Api::Forums.listMostLikedUsers(:forum => forum) 231 | # end 232 | # 233 | # it "list forum posts" do 234 | # DisqusRails::Api::Forums.listPosts(:forum => forum) 235 | # end 236 | # end 237 | # 238 | # describe DisqusRails::Api::Categories do 239 | # let(:category){2388690} 240 | # 241 | # it "list categories" do 242 | # DisqusRails::Api::Categories.list(:forum => forum) 243 | # end 244 | # 245 | # it "create new category" do 246 | # result = DisqusRails::Api::Categories.create(:forum => forum, :title => "new category") 247 | # category = result[:response]["id"] 248 | # 249 | # describe "in context of new created category" do 250 | # 251 | # it "show category details" do 252 | # DisqusRails::Api::Categories.details(:category => category) 253 | # end 254 | # 255 | # it "list category posts" do 256 | # DisqusRails::Api::Categories.listPosts(:category => category) 257 | # end 258 | # 259 | # it "list category threads" do 260 | # DisqusRails::Api::Categories.listThreads(:category => category) 261 | # end 262 | # end 263 | # end 264 | # end 265 | # 266 | #end 267 | -------------------------------------------------------------------------------- /spec/disqus_rails/category_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | 3 | describe DisqusRails::Category do 4 | let!(:category_hash) do 5 | { 6 | :code => 0, 7 | :response => { 8 | "id" => "0", 9 | "forum" => "stubbed_forum_name", 10 | "order" => 0, 11 | "isDefault" => true, 12 | "title" => "stubbed_title" 13 | } 14 | } 15 | end 16 | 17 | let(:category){ DisqusRails::Category.new(category_hash[:response]) } 18 | 19 | let(:empty_collection_hash) do 20 | { 21 | :cursor => { 22 | "prev" => "null", 23 | "hasNext" => true, 24 | "next" => "1368581473195442:0:0", 25 | "hasPrev" => false, 26 | "total" => "null", 27 | "id" => "1368581473195442:0:0", 28 | "more" => true 29 | }, 30 | :code => 0, 31 | :response => [] 32 | } 33 | end 34 | 35 | it "should get category details information with find method" do 36 | DisqusRails::Api::Categories 37 | .should_receive(:details) 38 | .with(:category => 0) 39 | .and_return(category_hash) 40 | 41 | DisqusRails::Category.find(0).should be_a_kind_of(DisqusRails::Category) 42 | end 43 | 44 | 45 | it "should get category threads as DisqusRails::Threads class instance" do 46 | DisqusRails::Api::Categories 47 | .should_receive(:listThreads) 48 | .and_return(empty_collection_hash) 49 | 50 | category.threads.should be_a_kind_of(DisqusRails::Threads) 51 | end 52 | 53 | it "should get category posts as DisqusRails::Posts class instance" do 54 | DisqusRails::Api::Categories 55 | .should_receive(:listPosts) 56 | .and_return(empty_collection_hash) 57 | 58 | category.posts.should be_a_kind_of(DisqusRails::Posts) 59 | end 60 | 61 | it "should create category" do 62 | DisqusRails::Api::Categories 63 | .should_receive(:create) 64 | .with(:forum => "stubbed_forum_name", :title => "stubbed_title") 65 | .and_return(category_hash) 66 | 67 | DisqusRails::Category.create(:forum => "stubbed_forum_name", :title => "stubbed_title").should be_a_kind_of DisqusRails::Category 68 | end 69 | 70 | it "should return list of categories" do 71 | DisqusRails::Api::Categories 72 | .should_receive(:list) 73 | .and_return(empty_collection_hash) 74 | 75 | DisqusRails::Category.where().should be_a_kind_of(DisqusRails::Categories) 76 | end 77 | end 78 | -------------------------------------------------------------------------------- /spec/disqus_rails/collection_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | 3 | describe DisqusRails do 4 | %w(Posts Threads Categories Forums Users).each do |subclass| 5 | it "should create #{subclass} class inherited from collection class" do 6 | should be_const_defined(subclass) 7 | end 8 | end 9 | end 10 | -------------------------------------------------------------------------------- /spec/disqus_rails/forum_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | 3 | describe DisqusRails::Forum do 4 | let!(:forum_hash) do 5 | { 6 | :code => 0, 7 | :response => { 8 | "id" => "bobross", 9 | "name" => "Bob Ross", 10 | "founder" => "1", 11 | "favicon" => { 12 | "permalink" => "http://disqus.com/api/forums/favicons/bobross.jpg", 13 | "cache" => "http://mediacdn.disqus.com/1091/images/favicon-default.png" 14 | } 15 | } 16 | } 17 | end 18 | 19 | let(:forum){ DisqusRails::Forum.new(forum_hash[:response]) } 20 | 21 | let(:empty_collection_hash) do 22 | { 23 | :cursor => { 24 | "prev" => "null", 25 | "hasNext" => true, 26 | "next" => "1368581473195442:0:0", 27 | "hasPrev" => false, 28 | "total" => "null", 29 | "id" => "1368581473195442:0:0", 30 | "more" => true 31 | }, 32 | :code => 0, 33 | :response => [] 34 | } 35 | end 36 | 37 | it "should get forum details information with find method" do 38 | DisqusRails::Api::Forums 39 | .should_receive(:details) 40 | .with(:forum => "bobross") 41 | .and_return(forum_hash) 42 | 43 | DisqusRails::Forum.find("bobross").should be_a_kind_of(DisqusRails::Forum) 44 | end 45 | 46 | it "should get forum categories as DisqusRails::Categories class instance" do 47 | DisqusRails::Api::Forums 48 | .should_receive(:listCategories) 49 | .and_return(empty_collection_hash) 50 | 51 | forum.categories.should be_a_kind_of(DisqusRails::Categories) 52 | end 53 | 54 | it "should get forum threads as DisqusRails::Threads class instance" do 55 | DisqusRails::Api::Forums 56 | .should_receive(:listThreads) 57 | .and_return(empty_collection_hash) 58 | 59 | forum.threads.should be_a_kind_of(DisqusRails::Threads) 60 | end 61 | 62 | it "should get forum posts as DisqusRails::Posts class instance" do 63 | DisqusRails::Api::Forums 64 | .should_receive(:listPosts) 65 | .and_return(empty_collection_hash) 66 | 67 | forum.posts.should be_a_kind_of(DisqusRails::Posts) 68 | end 69 | 70 | it "should get forum users as DisqusRails::Users class instance" do 71 | DisqusRails::Api::Forums 72 | .should_receive(:listUsers) 73 | .and_return(empty_collection_hash) 74 | 75 | forum.users.should be_a_kind_of(DisqusRails::Users) 76 | end 77 | 78 | it "should get forum most active users as DisqusRails::Users class instance" do 79 | DisqusRails::Api::Forums 80 | .should_receive(:listMostActiveUsers) 81 | .and_return(empty_collection_hash) 82 | 83 | forum.most_active_users.should be_a_kind_of(DisqusRails::Users) 84 | end 85 | 86 | it "should get forum most liked users as DisqusRails::Users class instance" do 87 | DisqusRails::Api::Forums 88 | .should_receive(:listMostLikedUsers) 89 | .and_return(empty_collection_hash) 90 | 91 | forum.most_liked_users.should be_a_kind_of(DisqusRails::Users) 92 | end 93 | 94 | it "should create forum" do 95 | DisqusRails::Api::Forums 96 | .should_receive(:create) 97 | .with(:website => "stubbed_website", :name => "stubbed_name", :short_name => "stubbed_short_name") 98 | .and_return({:code => 0, :response => forum_hash}) 99 | 100 | DisqusRails::Forum.create(:website => "stubbed_website", :name => "stubbed_name", :short_name => "stubbed_short_name").should be_a_kind_of DisqusRails::Forum 101 | end 102 | end 103 | -------------------------------------------------------------------------------- /spec/disqus_rails/model_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | 3 | describe DisqusRails::Model do 4 | %w(Category Forum Post Thread User).each do |klass| 5 | it "should create singleton method find in #{klass} class" do 6 | klass_instance = DisqusRails.const_get(klass).new() 7 | klass_instance.singleton_class.should respond_to(:find) 8 | end 9 | 10 | it "should create singleton method where if #{klass} api class has list method" do 11 | if DisqusRails::Api.const_get(klass.pluralize).respond_to?(:list) 12 | klass_instance = DisqusRails.const_get(klass).new() 13 | klass_instance.singleton_class.should respond_to(:where) 14 | end 15 | end 16 | 17 | %w(update_attributes reload).each do |method| 18 | it "should create method #{method} in #{klass} class" do 19 | klass_instance = DisqusRails.const_get(klass).new() 20 | klass_instance.should respond_to(method) 21 | end 22 | end 23 | end 24 | end 25 | -------------------------------------------------------------------------------- /spec/disqus_rails/post_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | 3 | describe DisqusRails::Post do 4 | let!(:post_hash) do 5 | { 6 | :code => 0, 7 | :response => { 8 | "isJuliaFlagged" => true, 9 | "isFlagged" => false, 10 | "forum" => "bobross", 11 | "parent" => "null", 12 | "author" => { 13 | "username" => "disqus_api", 14 | "about" => "", 15 | "name" => "disqus_api", 16 | "url" => "", 17 | "isFollowing" => false, 18 | "isFollowedBy" => false, 19 | "profileUrl" => "http://disqus.com/disqus_api/", 20 | "emailHash" => "67f79ed8e10b74abf07a8dfe101bbab2", 21 | "avatar" => { 22 | "permalink" => "http://disqus.com/api/users/avatars/disqus_api.jpg", 23 | "cache" => "http://mediacdn.disqus.com/1091/images/noavatar92.png" 24 | }, 25 | "id" => "1", 26 | "isAnonymous" => false, 27 | "email" => "example@disqus.com" 28 | }, 29 | "media" => [], 30 | "isApproved" => false, 31 | "dislikes" => 0, 32 | "raw_message" => "Hello There", 33 | "id" => "4", 34 | "thread" => "1", 35 | "points" => 0, 36 | "createdAt" => "2011-11-02T02:22:51", 37 | "isEdited" => false, 38 | "message" => "Hello There", 39 | "isHighlighted" => false, 40 | "ipAddress" => "127.0.0.1", 41 | "isSpam" => false, 42 | "isDeleted" => false, 43 | "likes" => 0 44 | } 45 | } 46 | end 47 | 48 | let(:updated_post_hash) do 49 | updated_post_hash = post_hash 50 | updated_post_hash[:response]["raw_message"] = "updated message" 51 | updated_post_hash[:response]["message"] = "updated message" 52 | updated_post_hash 53 | end 54 | 55 | let(:post) do 56 | DisqusRails::Post.new post_hash[:response] 57 | end 58 | 59 | let(:simple_result) do 60 | { 61 | :code => 0, 62 | :response => [{"id" => "4"}] 63 | } 64 | end 65 | 66 | let(:empty_collection_hash) do 67 | { 68 | :cursor => { 69 | "prev" => "null", 70 | "hasNext" => true, 71 | "next" => "1368581473195442:0:0", 72 | "hasPrev" => false, 73 | "total" => "null", 74 | "id" => "1368581473195442:0:0", 75 | "more" => true 76 | }, 77 | :code => 0, 78 | :response => [] 79 | } 80 | end 81 | 82 | it "should get post details information with find method" do 83 | DisqusRails::Api::Posts 84 | .should_receive(:details) 85 | .with(:post => 1) 86 | .and_return(post_hash) 87 | 88 | DisqusRails::Post.find(1).should be_a_kind_of(DisqusRails::Post) 89 | end 90 | 91 | it "should create post" do 92 | DisqusRails::Api::Posts 93 | .should_receive(:create) 94 | .with(:message => "stubbed_message") 95 | .and_return(post_hash) 96 | 97 | DisqusRails::Post.create(:message => "stubbed_message").author.should be_a_kind_of DisqusRails::User 98 | end 99 | 100 | 101 | it "should highlight post" do 102 | DisqusRails::Api::Posts 103 | .should_receive(:highlight) 104 | .with(:post => post.id) 105 | .and_return(simple_result) 106 | 107 | post.highlight 108 | 109 | post.isHighlighted.should be_true 110 | end 111 | 112 | it "should unhighlight post" do 113 | DisqusRails::Api::Posts 114 | .should_receive(:unhighlight) 115 | .with(:post => post.id) 116 | .and_return(simple_result) 117 | 118 | post.unhighlight 119 | 120 | post.isHighlighted.should be_false 121 | end 122 | 123 | it "should vote for post" do 124 | DisqusRails::Api::Posts 125 | .should_receive(:vote) 126 | .with(:post => post.id, :vote => 1) 127 | .and_return(simple_result) 128 | 129 | post.vote(1) 130 | 131 | post.vote.should == 1 132 | end 133 | 134 | it "should report post" do 135 | DisqusRails::Api::Posts 136 | .should_receive(:report) 137 | .with(:post => post.id) 138 | .and_return(simple_result) 139 | 140 | post.report 141 | 142 | post.isFlagged.should be_true 143 | end 144 | 145 | it "should approve post" do 146 | DisqusRails::Api::Posts 147 | .should_receive(:approve) 148 | .with(:post => post.id) 149 | .and_return(simple_result) 150 | 151 | post.approve 152 | 153 | post.isApproved.should be_true 154 | end 155 | 156 | it "should mark post as spam" do 157 | DisqusRails::Api::Posts 158 | .should_receive(:spam) 159 | .with(:post => post.id) 160 | .and_return(simple_result) 161 | 162 | post.spam 163 | 164 | post.isSpam.should be_true 165 | end 166 | 167 | it "should remove post" do 168 | DisqusRails::Api::Posts 169 | .should_receive(:remove) 170 | .with(:post => post.id) 171 | .and_return(simple_result) 172 | 173 | post.remove 174 | 175 | post.isDeleted.should be_true 176 | end 177 | 178 | it "should restore post" do 179 | post.isDeleted = true 180 | 181 | DisqusRails::Api::Posts 182 | .should_receive(:restore) 183 | .with(:post => post.id) 184 | .and_return(simple_result) 185 | 186 | post.restore 187 | 188 | post.isDeleted.should be_false 189 | end 190 | 191 | it "should update post" do 192 | 193 | DisqusRails::Api::Posts 194 | .should_receive(:update) 195 | .with(:post => post.id, :message => "updated message") 196 | .and_return(updated_post_hash) 197 | 198 | post.update("updated message") 199 | 200 | post.message.should == "updated message" 201 | end 202 | 203 | it "should return list of posts" do 204 | DisqusRails::Api::Posts 205 | .should_receive(:list) 206 | .and_return(empty_collection_hash) 207 | 208 | DisqusRails::Post.where().should be_a_kind_of(DisqusRails::Posts) 209 | end 210 | 211 | it "should return list of popular posts" do 212 | DisqusRails::Api::Posts 213 | .should_receive(:listPopular) 214 | .and_return(empty_collection_hash) 215 | 216 | DisqusRails::Post.popular.should be_a_kind_of(DisqusRails::Posts) 217 | end 218 | end 219 | -------------------------------------------------------------------------------- /spec/disqus_rails/thread_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | 3 | describe DisqusRails::Thread do 4 | let!(:thread_hash) do 5 | { 6 | :code => 0, 7 | :response => { 8 | "category" => "1", 9 | "reactions" => 0, 10 | "identifiers" => [], 11 | "forum" => "bobross", 12 | "title" => "Hello World", 13 | "dislikes" => 0, 14 | "isDeleted" => false, 15 | "author" => "1", 16 | "userScore" => 0, 17 | "id" => "3", 18 | "isClosed" => false, 19 | "posts" => 0, 20 | "link" => "null", 21 | "likes" => 0, 22 | "message" => "", 23 | "ipAddress" => "127.0.0.1", 24 | "slug" => "hello_world", 25 | "createdAt" => "2011-11-02T02:22:41" 26 | } 27 | } 28 | end 29 | 30 | let(:thread) do 31 | DisqusRails::Thread.new thread_hash[:response] 32 | end 33 | 34 | let(:empty_collection_hash) do 35 | { 36 | :cursor => { 37 | "prev" => "null", 38 | "hasNext" => true, 39 | "next" => "1368581473195442:0:0", 40 | "hasPrev" => false, 41 | "total" => "null", 42 | "id" => "1368581473195442:0:0", 43 | "more" => true 44 | }, 45 | :code => 0, 46 | :response => [] 47 | } 48 | end 49 | 50 | let(:simple_result) do 51 | { 52 | :code => 0, 53 | :response => [{"id" => "4"}] 54 | } 55 | end 56 | 57 | let(:updated_thread_hash) do 58 | updated_thread_hash = thread_hash 59 | updated_thread_hash[:response]["title"] = "updated thread title" 60 | updated_thread_hash 61 | end 62 | 63 | it "should get threads details information with find method" do 64 | DisqusRails::Api::Threads 65 | .should_receive(:details) 66 | .with(:thread => 3) 67 | .and_return(thread_hash) 68 | 69 | DisqusRails::Thread.find(3).should be_a_kind_of(DisqusRails::Thread) 70 | end 71 | 72 | it "should get thread posts as DisqusRails::Posts class instance" do 73 | DisqusRails::Api::Threads 74 | .should_receive(:listPosts) 75 | .and_return(empty_collection_hash) 76 | 77 | thread.posts.should be_a_kind_of(DisqusRails::Posts) 78 | end 79 | 80 | it "should create thread" do 81 | DisqusRails::Api::Threads 82 | .should_receive(:create) 83 | .with(:forum => "stubbed_forum", :title => "stubbed_title") 84 | .and_return(thread_hash) 85 | 86 | DisqusRails::Thread.create(:forum => "stubbed_forum", :title => "stubbed_title").should be_a_kind_of DisqusRails::Thread 87 | end 88 | 89 | it "should open thread" do 90 | DisqusRails::Api::Threads 91 | .should_receive(:open) 92 | .with(:thread => thread.id) 93 | .and_return(simple_result) 94 | 95 | thread.open 96 | 97 | thread.isClosed.should be_false 98 | end 99 | 100 | it "should close thread" do 101 | DisqusRails::Api::Threads 102 | .should_receive(:close) 103 | .with(:thread => thread.id) 104 | .and_return(simple_result) 105 | 106 | thread.close 107 | 108 | thread.isClosed.should be_true 109 | end 110 | 111 | it "should subscribe for a thread" do 112 | DisqusRails::Api::Threads 113 | .should_receive(:subscribe) 114 | .with(:thread => thread.id, :email => nil) 115 | .and_return(simple_result) 116 | 117 | thread.subscribe 118 | end 119 | 120 | it "should unsubscribe from a thread" do 121 | DisqusRails::Api::Threads 122 | .should_receive(:unsubscribe) 123 | .with(:thread => thread.id, :email => nil) 124 | .and_return(simple_result) 125 | 126 | thread.unsubscribe 127 | end 128 | 129 | it "should vote for a thread" do 130 | DisqusRails::Api::Threads 131 | .should_receive(:vote) 132 | .with(:thread => thread.id, :vote => 1) 133 | .and_return(simple_result) 134 | 135 | thread.vote(1) 136 | 137 | thread.vote.should == 1 138 | end 139 | 140 | it "should update thread" do 141 | DisqusRails::Api::Threads 142 | .should_receive(:update) 143 | .with(:title => "updated thread title", :thread => thread.id) 144 | .and_return(updated_thread_hash) 145 | 146 | thread.update(:title => "updated thread title") 147 | 148 | thread.title.should == "updated thread title" 149 | end 150 | 151 | it "should return list of threads" do 152 | DisqusRails::Api::Threads 153 | .should_receive(:list) 154 | .and_return(empty_collection_hash) 155 | 156 | DisqusRails::Thread.where().should be_a_kind_of(DisqusRails::Threads) 157 | end 158 | 159 | it "should return list of popular threads" do 160 | DisqusRails::Api::Threads 161 | .should_receive(:listPopular) 162 | .and_return(empty_collection_hash) 163 | 164 | DisqusRails::Thread.popular.should be_a_kind_of(DisqusRails::Threads) 165 | end 166 | 167 | it "should return list of hot threads" do 168 | DisqusRails::Api::Threads 169 | .should_receive(:listHot) 170 | .and_return(empty_collection_hash) 171 | 172 | DisqusRails::Thread.hot.should be_a_kind_of(DisqusRails::Threads) 173 | end 174 | end 175 | -------------------------------------------------------------------------------- /spec/disqus_rails/user_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | 3 | describe DisqusRails::User do 4 | let!(:user_hash) do 5 | { 6 | :code => 0, 7 | :response => { 8 | "username" => "disqus_api", 9 | "about" => "", 10 | "name" => "disqus_api", 11 | "url" => "", 12 | "isFollowing" => false, 13 | "isFollowedBy" => false, 14 | "profileUrl" => "http://disqus.com/disqus_api/", 15 | "emailHash" => "67f79ed8e10b74abf07a8dfe101bbab2", 16 | "avatar" => { 17 | "permalink" => "http://disqus.com/api/users/avatars/disqus_api.jpg", 18 | "cache" => "http://mediacdn.disqus.com/1091/images/noavatar92.png" 19 | } 20 | }, 21 | :id => "1", 22 | :isAnonymous => false, 23 | :email => "example@disqus.com" 24 | } 25 | end 26 | 27 | let(:user){ DisqusRails::User.new(user_hash[:response]) } 28 | 29 | let(:empty_collection_hash) do 30 | { 31 | :cursor => { 32 | "prev" => "null", 33 | "hasNext" => true, 34 | "next" => "1368581473195442:0:0", 35 | "hasPrev" => false, 36 | "total" => "null", 37 | "id" => "1368581473195442:0:0", 38 | "more" => true 39 | }, 40 | :code => 0, 41 | :response => [] 42 | } 43 | end 44 | 45 | it "should get user details information with find method" do 46 | DisqusRails::Api::Users 47 | .should_receive(:details) 48 | .with(:user => 1) 49 | .and_return(user_hash) 50 | 51 | DisqusRails::User.find(1).should be_a_kind_of(DisqusRails::User) 52 | end 53 | 54 | it "should get user forums as DisqusRails::Forums class instance" do 55 | DisqusRails::Api::Users 56 | .should_receive(:listForums) 57 | .and_return(empty_collection_hash) 58 | 59 | user.forums.should be_a_kind_of(DisqusRails::Forums) 60 | end 61 | 62 | it "should get user active forums as DisqusRails::Forums class instance" do 63 | DisqusRails::Api::Users 64 | .should_receive(:listActiveForums) 65 | .and_return(empty_collection_hash) 66 | 67 | user.active_forums.should be_a_kind_of(DisqusRails::Forums) 68 | end 69 | 70 | it "should get user active threads as DisqusRails::Threads class instance" do 71 | DisqusRails::Api::Users 72 | .should_receive(:listActiveThreads) 73 | .and_return(empty_collection_hash) 74 | 75 | user.active_threads.should be_a_kind_of(DisqusRails::Threads) 76 | end 77 | 78 | it "should get user posts as DisqusRails::Posts class instance" do 79 | DisqusRails::Api::Users 80 | .should_receive(:listPosts) 81 | .and_return(empty_collection_hash) 82 | 83 | user.posts.should be_a_kind_of(DisqusRails::Posts) 84 | end 85 | 86 | it "should get user followers as DisqusRails::Users class instance" do 87 | DisqusRails::Api::Users 88 | .should_receive(:listFollowers) 89 | .and_return(empty_collection_hash) 90 | 91 | user.followers.should be_a_kind_of(DisqusRails::Users) 92 | end 93 | 94 | it "should get users following users as DisqusRails::Users class instance" do 95 | DisqusRails::Api::Users 96 | .should_receive(:listFollowing) 97 | .and_return(empty_collection_hash) 98 | 99 | user.following.should be_a_kind_of(DisqusRails::Users) 100 | end 101 | end 102 | -------------------------------------------------------------------------------- /spec/disqus_rails_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | 3 | describe DisqusRails do 4 | 5 | before :all do 6 | DisqusRails.setup do |config| 7 | config::SHORT_NAME = "stubbed_short_name" 8 | config::SECRET_KEY = "stubbed_secret_key" 9 | config::PUBLIC_KEY = "stubbed_public_key" 10 | config::ACCESS_TOKEN = "stubbed_access_token" 11 | end 12 | end 13 | 14 | %w(SHORT_NAME SECRET_KEY PUBLIC_KEY ACCESS_TOKEN).each do |config_const| 15 | it { should be_const_defined config_const } 16 | end 17 | end 18 | -------------------------------------------------------------------------------- /spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | require 'rubygems' 2 | require 'bundler/setup' 3 | 4 | require 'rails/all' 5 | require 'disqus_rails' 6 | 7 | ActiveRecord::Base.establish_connection( 8 | :adapter => 'sqlite3', 9 | :database => ':memory:' 10 | ) 11 | 12 | RSpec.configure do |config| 13 | end 14 | -------------------------------------------------------------------------------- /vendor/assets/javascripts/disqus_rails.js.coffee: -------------------------------------------------------------------------------- 1 | class @DisqusRails 2 | 3 | constructor: (attributes)-> 4 | @short_name = attributes['short_name'] 5 | @public_key = attributes['public_key'] 6 | @sso = attributes['sso'] 7 | 8 | if attributes['remote_auth_s3'] then @remote_auth_s3 = attributes['remote_auth_s3'] 9 | if attributes['reset'] then reset = attributes['reset'] 10 | 11 | @registerDisqusConfig() 12 | 13 | if reset 14 | @isResetting = false 15 | @registerResetFunction() 16 | 17 | reset: -> 18 | unless @isResetting 19 | @isResetting = true 20 | that = this 21 | DISQUS.reset 22 | reload: true 23 | config: -> 24 | this.page.identifier = that.disqusable_id 25 | this.page.title = that.disqusable_title 26 | 27 | registerResetFunction: -> 28 | $(document).on "disqus:on_ready", => 29 | @reset() 30 | 31 | registerDisqusConfig: -> 32 | that = this 33 | window.disqus_config = -> 34 | if that.remote_auth_s3 && that.sso 35 | this.page.remote_auth_s3 = that.remote_auth_s3 36 | this.page.api_key = that.public_key 37 | this.sso = that.sso 38 | 39 | @callbacks.afterRender = [-> 40 | $(document).trigger "disqus:after_render" 41 | ] 42 | @callbacks.onInit = [-> 43 | $(document).trigger "disqus:on_init" 44 | ] 45 | @callbacks.onNewComment = [-> 46 | $(document).trigger "disqus:on_new_comment" 47 | ] 48 | @callbacks.onPaginate = [-> 49 | $(document).trigger "disqus:on_paginate" 50 | ] 51 | @callbacks.onReady = [-> 52 | $(document).trigger "disqus:on_ready" 53 | ] 54 | @callbacks.preData = [-> 55 | $(document).trigger "disqus:pre_data" 56 | ] 57 | @callbacks.preInit = [-> 58 | $(document).trigger "disqus:pre_init" 59 | ] 60 | @callbacks.preReset = [-> 61 | $(document).trigger "disqus:pre_reset" 62 | ] 63 | 64 | draw_thread: (disqusable_id, disqusable_title)-> 65 | 66 | @disqusable_id = disqusable_id 67 | @disqusable_title = disqusable_title 68 | 69 | window.disqus_shortname = @short_name 70 | window.disqus_title = @disqusable_title || document.title 71 | 72 | (-> 73 | dsq = document.createElement("script") 74 | dsq.type = "text/javascript" 75 | dsq.async = true 76 | dsq.src = "//" + disqus_shortname + ".disqus.com/embed.js" 77 | (document.getElementsByTagName("head")[0] or document.getElementsByTagName("body")[0]).appendChild dsq 78 | )() 79 | --------------------------------------------------------------------------------