disqus_rails 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,20 @@
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
@@ -0,0 +1,90 @@
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
@@ -0,0 +1,54 @@
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
@@ -0,0 +1,64 @@
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::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
+ javascript_tag %Q"$(document).ready(function(){
60
+ disqus_rails.draw_thread(\"#{disqusable_id}\", \"#{disqusable_title}\");
61
+ });"
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,45 @@
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
@@ -0,0 +1,106 @@
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
@@ -0,0 +1,130 @@
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