commentatr 0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ coverage
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --colour
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in commentatr_client.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Gregory Eremin
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,106 @@
1
+ ## Ruby wrapper for [Commentatr API](http://commentatr.net/developer/documentation/)
2
+ This gem supports ruby version 1.9+ only.
3
+
4
+ ### Installation
5
+
6
+ Add this line to your application's Gemfile:
7
+ ```ruby
8
+ gem 'commentatr'
9
+ ```
10
+
11
+ And then execute:
12
+ ```bash
13
+ $ bundle
14
+ ```
15
+
16
+ Or install it yourself as:
17
+ ```bash
18
+ $ gem install commentatr
19
+ ```
20
+
21
+ ### Usage
22
+
23
+ Initialize client
24
+ ```ruby
25
+ # Without params
26
+ Commentatr::Client.new()
27
+
28
+ # With blog credentials
29
+ Commentatr::Client.new(partner_host: "example.com", token: "...your secret token...")
30
+
31
+ # With client credentials
32
+ Commentatr::Client.new(client_id: 42, token: "...your secret token...")
33
+ ```
34
+
35
+ Fetch comments for blog
36
+ ```ruby
37
+ # All entry comments
38
+ cttr.comments(page_id: "blog-1")
39
+
40
+ # Last 5 entry comments
41
+ cttr.comments(page_id: "blog-1", order: "desc", limit: 5)
42
+
43
+ # All comments since last time you checked
44
+ cttr.comments(page_id: "blog-1", after: Time.now.to_i - 3600)
45
+ ```
46
+
47
+ Fetch comments for all client blogs
48
+ ```ruby
49
+ cttr.client_comments(order: "asc", offset: 100, limit: 10)
50
+ ```
51
+
52
+ Fetch counters of comments for each entry of each client blog
53
+ ```ruby
54
+ cttr.counters
55
+ ```
56
+
57
+ Moderator actions
58
+ ```ruby
59
+ cttr.approve(comment_id: 1)
60
+ cttr.reject(comment_id: 2)
61
+ cttr.delete(comment_id: 3)
62
+ cttr.junk(comment_id: 4)
63
+
64
+ # Mass management
65
+ cttr.approve(comment_id: [1, 2, 3, 4])
66
+ ```
67
+
68
+ Admin actions
69
+ ```ruby
70
+ # Change comments approval setting
71
+ cttr = Commentatr::Client.new(partner_host: "example.com", token: "...your secret token...")
72
+ cttr.update_config(approval: false, readonly: true)
73
+
74
+ # Regenerate blog token
75
+ commentatr_config[:token] = cttr.regenerate_blog_token(partner_host: "example.com")
76
+
77
+ # Regenerate client token
78
+ commentatr_config[:token] = cttr.regenerate_client_token
79
+ ```
80
+
81
+ You can perform all these actions passing all needed authentication params for each query
82
+ ```ruby
83
+ cttr = Commentatr::Client.new
84
+ cttr.comments(
85
+ partner_host: "example.com",
86
+ token: "...your secret token...",
87
+ page_id: "blog-1",
88
+ order: "asc",
89
+ offset: 50,
90
+ limit: 10
91
+ )
92
+ ```
93
+
94
+ ### Working with responses
95
+ All responses are stored in `Commentatr::Response` objects
96
+ ```ruby
97
+ response = cttr.comments(page_id: "blog-1")
98
+ response.code
99
+ # => 200
100
+ response.body.success
101
+ # => true
102
+ response.body.data
103
+ # => [ ...comments here... ]
104
+ response.body.errors
105
+ # => []
106
+ ```
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/commentatr/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Gregory Eremin"]
6
+ gem.email = ["magnolia_fan@me.com"]
7
+ gem.description = %q{Ruby wrapper for Commentatr API}
8
+ gem.summary = %q{Gem provides easy access to Commentatr API web service}
9
+ gem.homepage = "http://commentatr.net/developer/gem"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^spec/})
14
+ gem.name = "commentatr"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Commentatr::VERSION
17
+
18
+ gem.add_development_dependency "rspec"
19
+ gem.add_development_dependency "webmock"
20
+ gem.add_development_dependency "simplecov"
21
+ gem.add_development_dependency "awesome_print"
22
+ end
data/lib/commentatr.rb ADDED
@@ -0,0 +1,8 @@
1
+ require "net/http"
2
+ require "json"
3
+
4
+ require "commentatr/version"
5
+ require "commentatr/request"
6
+ require "commentatr/response"
7
+ require "commentatr/routes"
8
+ require "commentatr/client"
@@ -0,0 +1,60 @@
1
+ module Commentatr
2
+ class Client
3
+ attr_accessor :client_id, :token, :partner_host, :page_id
4
+
5
+ def initialize(params = {})
6
+ %w[ client_id token partner_host page_id ].each { |param|
7
+ instance_variable_set("@#{param}", params[param.to_sym]) unless params[param.to_sym].nil?
8
+ }
9
+ end
10
+
11
+ Commentatr::ROUTES.each { |name, params|
12
+ define_method(name) do |args = {}|
13
+ exec_mathod(params, args)
14
+ end
15
+ }
16
+
17
+ private
18
+
19
+ def exec_mathod(params, args = {})
20
+ args.merge!(instance_variables.map{ |key|
21
+ { key.to_s.gsub("@", "").to_sym => instance_variable_get(key) }
22
+ }.inject(:merge) || {})
23
+
24
+ params[:uri_params] = params[:uri].scan(/:\w+/).map{ |param|
25
+ param.gsub(":", "").to_sym
26
+ }
27
+ params[:required_params] ||= []
28
+ params[:optional_params] ||= []
29
+ validate_params(params, args)
30
+
31
+ uri = params[:uri].clone
32
+ params[:uri_params].each { |key|
33
+ uri.gsub!(":#{key}", args.delete(key))
34
+ }
35
+
36
+ case params[:method]
37
+ when :get, :delete
38
+ get_params = args
39
+ post_params = {}
40
+ when :post, :put
41
+ get_params = {}
42
+ post_params = args
43
+ end
44
+
45
+ Commentatr::Request.new(params[:method], uri, get_params, post_params).make
46
+ end
47
+
48
+ def validate_params(params, args)
49
+ required_params = (params[:uri_params] + params[:required_params]).uniq
50
+ required_params.each { |key|
51
+ raise "Parameter required: #{key}" unless args.keys.include?(key)
52
+ }
53
+
54
+ allowed_params = (required_params + params[:optional_params]).uniq
55
+ args.keys.each { |key|
56
+ raise "Unknown parameter: #{key}" unless allowed_params.include?(key)
57
+ }
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,35 @@
1
+ module Commentatr
2
+ class Request
3
+ HOST = "api.commentatr.net"
4
+ PORT = 80
5
+
6
+ attr_accessor :method, :uri, :get_params, :post_params
7
+ attr_reader :response
8
+
9
+ def initialize(method, uri, get_params = {}, post_params = {})
10
+ @method, @uri, @get_params, @post_params = method, uri, get_params, post_params
11
+ @response = nil
12
+ end
13
+
14
+ def make
15
+ uri = @uri + ("?" + uri_params(@get_params) unless @get_params.empty?).to_s
16
+
17
+ http = ::Net::HTTP.start(HOST, PORT)
18
+ response = http.send_request(@method.to_s.upcase, uri, uri_params(@post_params))
19
+
20
+ begin
21
+ body = JSON.parse(response.body)
22
+ rescue
23
+ body = nil
24
+ end
25
+
26
+ @response = Commentatr::Response.new(response.code.to_i, body)
27
+ end
28
+
29
+ private
30
+
31
+ def uri_params(params)
32
+ params.map { |key, value| "#{key}=#{value}" }.join("&")
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,20 @@
1
+ module Commentatr
2
+ class Response
3
+ attr_reader :code, :body
4
+
5
+ def initialize(code, body)
6
+ @code = code
7
+ unless body.nil?
8
+ @body = ResponseBody.new(body["success"], body["data"], body["errors"])
9
+ end
10
+ end
11
+
12
+ class ResponseBody
13
+ attr_reader :success, :data, :errors
14
+
15
+ def initialize(success, data, errors)
16
+ @success, @data, @errors = success, data, errors
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,61 @@
1
+ module Commentatr
2
+ ROUTES = {
3
+ comments: {
4
+ method: :get,
5
+ uri: "/api/blogs/:partner_host/pages/:page_id/comments.json",
6
+ optional_params: [:order, :limit, :after, :token]
7
+ },
8
+ client_comments: {
9
+ method: :get,
10
+ uri: "/api/clients/:client_id/comments.json",
11
+ required_params: [:token],
12
+ optional_params: [:order, :limit, :after]
13
+ },
14
+ counters: {
15
+ method: :get,
16
+ uri: "/api/clients/:client_id/counters.json",
17
+ required_params: [:token]
18
+ },
19
+ delete: {
20
+ method: :delete,
21
+ uri: "/api/blogs/:partner_host/pages/:page_id/comments/:comment_id.json",
22
+ required_params: [:token]
23
+ },
24
+ approve: {
25
+ method: :put,
26
+ uri: "/api/blogs/:partner_host/pages/:page_id/comments/:comment_id/approve.json",
27
+ required_params: [:token]
28
+ },
29
+ reject: {
30
+ method: :put,
31
+ uri: "/api/blogs/:partner_host/pages/:page_id/comments/:comment_id/reject.json",
32
+ required_params: [:token]
33
+ },
34
+ junk: {
35
+ method: :put,
36
+ uri: "/api/blogs/:partner_host/pages/:page_id/comments/:comment_id/junk.json",
37
+ required_params: [:token]
38
+ },
39
+ regenerate_blog_token: {
40
+ method: :get,
41
+ uri: "/api/blogs/:partner_host/regenerate_token.json",
42
+ required_params: [:token]
43
+ },
44
+ regenerate_client_token: {
45
+ method: :get,
46
+ uri: "/api/clients/:client_id/regenerate_token.json",
47
+ required_params: [:token]
48
+ },
49
+ update_config: {
50
+ method: :put,
51
+ uri: "/api/blogs/:partner_host/update_config.json",
52
+ required_params: [:token],
53
+ optional_params: [:approval, :readonly]
54
+ },
55
+ client_tokens: {
56
+ method: :get,
57
+ uri: "/api/clients/:client_id/tokens.json",
58
+ required_params: [:token]
59
+ }
60
+ }
61
+ end
@@ -0,0 +1,3 @@
1
+ module Commentatr
2
+ VERSION = "0.1"
3
+ end
@@ -0,0 +1,39 @@
1
+ require "spec_helper"
2
+
3
+ describe "Comments method" do
4
+ it "works with preconfigured partner_host" do
5
+ stub_request(:get, "http://#{$commentatr_host}/api/blogs/example.com/pages/1/comments.json").
6
+ to_return(status: 200, body: { success: true, data: nil, errors: [] }.to_json)
7
+
8
+ commentatr = Commentatr::Client.new(partner_host: "example.com")
9
+ commentatr.comments(page_id: "1")
10
+ end
11
+
12
+ it "works with preconfigured partner_host and page_id" do
13
+ stub_request(:get, "http://#{$commentatr_host}/api/blogs/example.com/pages/1/comments.json").
14
+ to_return(status: 200, body: { success: true, data: nil, errors: [] }.to_json)
15
+ commentatr = Commentatr::Client.new(partner_host: "example.com", page_id: "1")
16
+ commentatr.comments
17
+ end
18
+
19
+ it "works without preconfigured partner_host and page_id" do
20
+ stub_request(:get, "http://#{$commentatr_host}/api/blogs/example.com/pages/1/comments.json").
21
+ to_return(status: 200, body: { success: true, data: nil, errors: [] }.to_json)
22
+ commentatr = Commentatr::Client.new
23
+ commentatr.comments(partner_host: "example.com", page_id: "1")
24
+ end
25
+
26
+ it "updates comment" do
27
+ stub_request(:put, "http://#{$commentatr_host}/api/blogs/example.com/pages/1/comments/test/approve.json").
28
+ to_return(status: 200, body: { success: true, data: nil, errors: [] }.to_json)
29
+ commentatr = Commentatr::Client.new(partner_host: "example.com", token: "test")
30
+ commentatr.approve(page_id: "1", comment_id: "test")
31
+ end
32
+
33
+ it "handles bad responses" do
34
+ stub_request(:get, "http://#{$commentatr_host}/api/blogs/example.com/pages/1/comments.json").
35
+ to_return(status: 500, body: "bad response")
36
+ commentatr = Commentatr::Client.new
37
+ commentatr.comments(partner_host: "example.com", page_id: "1")
38
+ end
39
+ end
@@ -0,0 +1,16 @@
1
+ # SimpleCov
2
+ require "simplecov"
3
+ SimpleCov.start
4
+
5
+ # App
6
+ require "commentatr"
7
+
8
+ # Webmock
9
+ require "webmock/rspec"
10
+ require "webmock"
11
+ include WebMock::API
12
+ WebMock.disable_net_connect!
13
+ $commentatr_host = "#{Commentatr::Request::HOST}:#{Commentatr::Request::PORT}"
14
+
15
+ RSpec.configure do |config|
16
+ end
metadata ADDED
@@ -0,0 +1,126 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: commentatr
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Gregory Eremin
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-08-04 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: webmock
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: simplecov
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: awesome_print
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description: Ruby wrapper for Commentatr API
79
+ email:
80
+ - magnolia_fan@me.com
81
+ executables: []
82
+ extensions: []
83
+ extra_rdoc_files: []
84
+ files:
85
+ - .gitignore
86
+ - .rspec
87
+ - Gemfile
88
+ - LICENSE
89
+ - README.md
90
+ - Rakefile
91
+ - commentatr.gemspec
92
+ - lib/commentatr.rb
93
+ - lib/commentatr/client.rb
94
+ - lib/commentatr/request.rb
95
+ - lib/commentatr/response.rb
96
+ - lib/commentatr/routes.rb
97
+ - lib/commentatr/version.rb
98
+ - spec/requests/comments_spec.rb
99
+ - spec/spec_helper.rb
100
+ homepage: http://commentatr.net/developer/gem
101
+ licenses: []
102
+ post_install_message:
103
+ rdoc_options: []
104
+ require_paths:
105
+ - lib
106
+ required_ruby_version: !ruby/object:Gem::Requirement
107
+ none: false
108
+ requirements:
109
+ - - ! '>='
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ required_rubygems_version: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ requirements: []
119
+ rubyforge_project:
120
+ rubygems_version: 1.8.23
121
+ signing_key:
122
+ specification_version: 3
123
+ summary: Gem provides easy access to Commentatr API web service
124
+ test_files:
125
+ - spec/requests/comments_spec.rb
126
+ - spec/spec_helper.rb