disqussed 0.0.2

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.
data/.gitignore ADDED
@@ -0,0 +1,22 @@
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
+ .idea/*
19
+ .rvmrc
20
+ .DS_Store
21
+ !.gitkeep
22
+ .jhw-cache
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in disqussed.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Michael Carey
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.
data/README.md ADDED
@@ -0,0 +1,84 @@
1
+ # Disqussed
2
+
3
+ A simple wrapper around the Disqus V3 API.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'disqussed'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install disqussed
18
+
19
+ ## Configuration
20
+
21
+ Edit lib/disqussed.rb and add your api_key and access_token. These can be found on the application page for your app
22
+ by going to http://disqus.com/api/applications and choosing your application.
23
+
24
+ ## Usage
25
+
26
+ ### Threads
27
+
28
+ #### Create
29
+
30
+ Required - forum, title
31
+
32
+ Options - none
33
+
34
+ Disqussed::Threads.create(forum, title)
35
+
36
+ #### Details
37
+
38
+ Required - thread
39
+
40
+ Options - none
41
+
42
+ Disqussed::Threads.details(thread)
43
+
44
+ #### Post Count
45
+
46
+ Required - thread
47
+
48
+ Options - none
49
+
50
+ Disqussed::Threads.post_count(thread)
51
+
52
+
53
+ #### Remove
54
+
55
+ Required - thread
56
+
57
+ Options - none
58
+
59
+ Disqussed::Threads.remove(thread)
60
+
61
+ ### Posts
62
+
63
+ #### Create
64
+
65
+ Required - message
66
+
67
+ Options - thread, author_email, author_name,
68
+
69
+ Disqussed::Post.create("message", { :thread => thread }).
70
+
71
+ #### List
72
+
73
+ Required - none
74
+ Options - category, thread, forum, since, related, limit, offset, include, order
75
+
76
+ Disqussed::Post.list({ :thread => thread })
77
+
78
+ ## Contributing
79
+
80
+ 1. Fork it
81
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
82
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
83
+ 4. Push to the branch (`git push origin my-new-feature`)
84
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
data/disqussed.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/disqussed/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Michael Carey"]
6
+ gem.email = ["mcarey@stippleit.com"]
7
+ gem.description = %q{Lightweight wrapper around the Disqus V3 API}
8
+ gem.summary = %q{Disqus V3 API Wrapper}
9
+ gem.homepage = ""
10
+
11
+ gem.add_dependency "httparty"
12
+ gem.add_dependency "oj"
13
+ gem.add_dependency "multi_json"
14
+ gem.add_dependency "activesupport"
15
+
16
+ gem.add_development_dependency "rspec"
17
+ gem.add_development_dependency "vcr"
18
+ gem.add_development_dependency "fakeweb"
19
+
20
+ gem.files = `git ls-files`.split($\)
21
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
22
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
23
+ gem.name = "disqussed"
24
+ gem.require_paths = ["lib"]
25
+ gem.version = Disqussed::VERSION
26
+ end
data/lib/disqussed.rb ADDED
@@ -0,0 +1,19 @@
1
+ require 'disqussed/version'
2
+ require 'disqussed/api'
3
+ require 'disqussed/threads'
4
+ require 'disqussed/posts'
5
+ require 'active_support/core_ext/hash'
6
+
7
+ module Disqussed
8
+ @defaults = {
9
+ :api_key => "",
10
+ :secret_key => "", # only required if sso is set to true
11
+ :forum => "",
12
+ :access_token => "",
13
+ :sso => false
14
+ }
15
+
16
+ def self.defaults
17
+ @defaults
18
+ end
19
+ end
@@ -0,0 +1,44 @@
1
+ require 'httparty'
2
+ require 'digest/sha1'
3
+
4
+ module Disqussed
5
+ class Api
6
+ ROOT = 'https://disqus.com/api'
7
+ API_VERSION = '3.0'
8
+
9
+ class << self
10
+ def request(method, endpoint, action, opts = {}, authenticate_as_self = false, user = {})
11
+ throw "Missing API Key" if Disqussed::defaults[:api_key].nil?
12
+
13
+ opts[:api_key] = Disqussed::defaults[:api_key]
14
+ opts[:api_secret] = Disqussed::defaults[:secret_key]
15
+
16
+ if authenticate_as_self
17
+ opts[:access_token] = Disqussed::defaults[:access_token]
18
+ elsif Disqussed::defaults[:sso]
19
+ throw "Missing API Secret" if Disqussed::defaults[:secret_key].nil?
20
+
21
+ user.slice!(:id, :username, :email, :avatar, :url)
22
+
23
+ opts[:remote_auth] = remote_auth_s3(user)
24
+ end
25
+
26
+ if method === "post"
27
+ HTTParty.post([ROOT, API_VERSION, endpoint, action + '.json'].join('/'), { :body => opts })
28
+ elsif method === "get"
29
+ HTTParty.get([ROOT, API_VERSION, endpoint, action + '.json'].join('/'), { :query => opts })
30
+ end
31
+ end
32
+
33
+ def remote_auth_s3(data)
34
+ digest = OpenSSL::Digest::Digest.new('sha1')
35
+ data = Base64.urlsafe_encode64(MultiJson.dump(data))
36
+ timestamp = Time.now.to_i
37
+
38
+ sha1 = OpenSSL::HMAC.hexdigest(digest, Disqussed::defaults[:secret_key], "#{data} #{timestamp}")
39
+
40
+ "#{data} #{sha1} #{timestamp}"
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,23 @@
1
+ module Disqussed
2
+ class Posts
3
+ ENDPOINT = 'posts'
4
+
5
+ class << self
6
+ def create(message = nil, opts = {}, user = {})
7
+ opts[:message] = message
8
+
9
+ opts.slice!(:thread, :author_email, :author_name, :message, :forum)
10
+
11
+ Disqussed::Api.request('post', ENDPOINT, 'create', opts, false, user)
12
+ end
13
+
14
+ def list(opts = {})
15
+ opts.slice!(:category, :thread, :forum, :since,
16
+ :related, :limit, :offset, :include, :order
17
+ )
18
+
19
+ Disqussed::Api.request('get', ENDPOINT, 'list', opts, false)
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,64 @@
1
+ module Disqussed
2
+ class Threads
3
+ ENDPOINT = 'threads'
4
+
5
+ class << self
6
+ def create(forum = nil, title = nil, opts = {})
7
+ opts[:forum] = forum ? forum : Disqussed::defaults[:forum]
8
+ opts[:title] = title
9
+
10
+ opts.slice!(:forum, :title, :identifier, :slug)
11
+
12
+ Disqussed::Api.request('post', ENDPOINT, 'create', opts, true)
13
+ end
14
+
15
+ def details(thread = nil)
16
+ opts = {}
17
+ opts[:thread] = thread
18
+
19
+ opts.slice!(:thread)
20
+
21
+ Disqussed::Api.request('get', ENDPOINT, 'details', opts, true)
22
+ end
23
+
24
+ def get_thread_id_by_ident(identifier, opts = {})
25
+ opts[:forum] = opts[:forum] ? opts[:forum] : Disqussed::defaults[:forum]
26
+
27
+ opts.slice!(:forum)
28
+
29
+ opts["thread:ident"] = identifier
30
+ Disqussed::Api.request('get', ENDPOINT, 'details', opts, true)
31
+ end
32
+
33
+ def post_count(thread = nil)
34
+ opts = {}
35
+ opts[:thread] = thread
36
+
37
+ opts.slice!(:thread)
38
+
39
+ details = Disqussed::Api.request('get', ENDPOINT, 'details', opts, true)
40
+
41
+ details["response"]["posts"]
42
+ end
43
+
44
+ def remove(thread = nil)
45
+ opts = {}
46
+ opts[:thread] = thread
47
+
48
+ opts.slice!(:thread)
49
+
50
+ Disqussed::Api.request('post', ENDPOINT, 'remove', opts, true)
51
+ end
52
+
53
+ def remove_thread_by_ident(identifier, opts = {})
54
+ opts[:forum] = opts[:forum] ? opts[:forum] : Disqussed::defaults[:forum]
55
+
56
+ opts.slice!(:forum)
57
+
58
+ opts["thread:ident"] = identifier
59
+ Disqussed::Api.request('post', ENDPOINT, 'remove', opts, true)
60
+ end
61
+
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,3 @@
1
+ module Disqussed
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,90 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helper'))
2
+
3
+ describe Disqussed::Api do
4
+ before :each do
5
+ Disqussed::defaults[:sso] = false
6
+ end
7
+
8
+ describe "post" do
9
+ it "builds a simple Disqus Post Request" do
10
+ HTTParty
11
+ .should_receive(:post)
12
+ .with("https://disqus.com/api/3.0/endpoint/action.json",
13
+ :body => { :api_key => Disqussed::defaults[:api_key],
14
+ :api_secret => Disqussed::defaults[:secret_key] })
15
+
16
+ Disqussed::Api.request('post', 'endpoint', 'action')
17
+ end
18
+
19
+ it "passes on options" do
20
+ HTTParty
21
+ .should_receive(:post)
22
+ .with("https://disqus.com/api/3.0/endpoint/action.json",
23
+ :body => { :api_key => Disqussed::defaults[:api_key],
24
+ :api_secret => Disqussed::defaults[:secret_key],
25
+ :pants => "none" })
26
+
27
+ Disqussed::Api.request('post', 'endpoint', 'action', { :pants => "none" })
28
+ end
29
+
30
+ it "authenticates as the access_token user" do
31
+ HTTParty
32
+ .should_receive(:post)
33
+ .with("https://disqus.com/api/3.0/endpoint/action.json",
34
+ :body => { :api_key => Disqussed::defaults[:api_key],
35
+ :api_secret => Disqussed::defaults[:secret_key],
36
+ :access_token => Disqussed::defaults[:access_token] })
37
+
38
+ Disqussed::Api.request('post', 'endpoint', 'action', {}, true)
39
+ end
40
+ end
41
+
42
+ describe "get" do
43
+ it "builds a simple Disqus Get Request" do
44
+ HTTParty
45
+ .should_receive(:get)
46
+ .with("https://disqus.com/api/3.0/endpoint/action.json",
47
+ :query => { :api_key => Disqussed::defaults[:api_key],
48
+ :api_secret => Disqussed::defaults[:secret_key] })
49
+
50
+ Disqussed::Api.request('get', 'endpoint', 'action')
51
+ end
52
+
53
+ it "passes on options" do
54
+ HTTParty
55
+ .should_receive(:get)
56
+ .with("https://disqus.com/api/3.0/endpoint/action.json",
57
+ :query => { :api_key => Disqussed::defaults[:api_key],
58
+ :api_secret => Disqussed::defaults[:secret_key],
59
+ :pants => "none" })
60
+
61
+ Disqussed::Api.request('get', 'endpoint', 'action', { :pants => "none" })
62
+ end
63
+
64
+ it "authenticates as the access_token user" do
65
+ HTTParty
66
+ .should_receive(:get)
67
+ .with("https://disqus.com/api/3.0/endpoint/action.json",
68
+ :query => { :api_key => Disqussed::defaults[:api_key],
69
+ :api_secret => Disqussed::defaults[:secret_key],
70
+ :access_token => Disqussed::defaults[:access_token] })
71
+
72
+ Disqussed::Api.request('get', 'endpoint', 'action', {}, true)
73
+ end
74
+ end
75
+
76
+ describe "remote_auth_s3" do
77
+ before :each do
78
+ @unix_timestamp = 1347468657
79
+ Time.any_instance.stub(:to_i).and_return(@unix_timestamp)
80
+ end
81
+
82
+ it "generates an auth string" do
83
+ data = { :id => 1234, :username => "shlappy", :email => "test@stipple-test.com", :avatar => "linkified", :url => "fanks" }
84
+ encoded = "eyJpZCI6MTIzNCwidXNlcm5hbWUiOiJzaGxhcHB5IiwiZW1haWwiOiJ0ZXN0QHN0aXBwbGUtdGVzdC5jb20iLCJhdmF0YXIiOiJsaW5raWZpZWQiLCJ1cmwiOiJmYW5rcyJ9"
85
+ sha1 = "5d75f738e7c1c6da7359f9870a06d2d12406b1ec"
86
+
87
+ Disqussed::Api.remote_auth_s3(data).should == "#{encoded} #{sha1} #@unix_timestamp"
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,114 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helper'))
2
+
3
+ describe Disqussed::Posts do
4
+ before :each do
5
+ Disqussed::defaults[:sso] = true
6
+
7
+ @user = { :username => "Tester", :id => "1", :email => "test_account1@stipple.com" }
8
+ @user2 = { :username => "Tester 2", :id => "2", :email => "test_account2@stipple.com" }
9
+ end
10
+
11
+ describe "create" do
12
+ describe "failure" do
13
+ context "no message" do
14
+ use_vcr_cassette
15
+
16
+ before :each do
17
+ @resp = Disqussed::Posts.create
18
+ end
19
+
20
+ it "returns with a 400 HTTP status code" do
21
+ @resp.code.should == 400
22
+ end
23
+
24
+ it "returns with a response code 2" do
25
+ @resp['code'].should == 2
26
+ end
27
+ end
28
+
29
+ context "no thread" do
30
+ use_vcr_cassette
31
+
32
+ before :each do
33
+ @resp = Disqussed::Posts.create("missing thread", {}, @user)
34
+ end
35
+
36
+ it "returns with a 400 HTTP status code" do
37
+ @resp.code.should == 400
38
+ end
39
+
40
+ it "returns with a response code 2" do
41
+ @resp['code'].should == 2
42
+ end
43
+ end
44
+
45
+ context "no privileges" do
46
+ use_vcr_cassette
47
+
48
+ before :each do
49
+ @resp = Disqussed::Posts.create("no privileges", { :thread => "808" })
50
+ end
51
+
52
+ it "returns with a 400 HTTP status code" do
53
+ @resp.code.should == 400
54
+ end
55
+
56
+ it "returns with a response code 2" do
57
+ @resp['code'].should == 2
58
+ end
59
+ end
60
+ end
61
+
62
+ context "success" do
63
+ use_vcr_cassette
64
+
65
+ before :each do
66
+ @thread = Disqussed::Threads.create(nil, Time.now.to_f)
67
+ @resp = Disqussed::Posts.create(SecureRandom.hex(10), { :thread => @thread['response']['id'] }, @user)
68
+ end
69
+
70
+ it "returns with a 200 HTTP Status code" do
71
+ @resp.code.should == 200
72
+ end
73
+ end
74
+ end
75
+
76
+ describe "list" do
77
+ describe "success" do
78
+ context "all thread posts" do
79
+ use_vcr_cassette :match_requests_on => [:host, :path]
80
+
81
+ before :each do
82
+ @resp = Disqussed::Posts.list
83
+ end
84
+
85
+ it "returns a list of posts" do
86
+ @resp['response'].should_not be_empty
87
+ end
88
+
89
+ it "returns with a 200 HTTP status code" do
90
+ @resp.code.should == 200
91
+ end
92
+ end
93
+
94
+ context "specific thread posts" do
95
+ use_vcr_cassette :match_requests_on => [:host, :path]
96
+
97
+ before :each do
98
+ @thread = Disqussed::Threads.create(Disqussed::defaults[:forum], Time.now.to_f)
99
+ Disqussed::Posts.create("test", { :thread => @thread['response']['id'] }, @user)
100
+ Disqussed::Posts.create("test 2", { :thread => @thread['response']['id'] }, @user2)
101
+ @resp = Disqussed::Posts.list({:forum => Disqussed::defaults[:forum], :thread => @thread['response']['id']})
102
+ end
103
+
104
+ it "returns a list of posts" do
105
+ @resp['response'].should_not be_empty
106
+ end
107
+
108
+ it "returns with a 200 HTTP status code" do
109
+ @resp.code.should == 200
110
+ end
111
+ end
112
+ end
113
+ end
114
+ end