Skip to content

Commit

Permalink
Merge pull request #7 from Krystosterone/code-cleanup
Browse files Browse the repository at this point in the history
Code cleanup
  • Loading branch information
royjs committed Oct 23, 2014
2 parents 07a475c + 875573f commit c4dc219
Show file tree
Hide file tree
Showing 16 changed files with 120 additions and 110 deletions.
33 changes: 16 additions & 17 deletions lib/mollom_rest_api/v1/blacklist.rb
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
class MollomRestApi::V1::Blacklist < MollomRestApi::Interface
class << self
def create(public_key, value, request_parameters = {})
post(request_parameters.reverse_merge(value: value), [public_key])
end

def self.create(public_key, value, request_parameters = {})
post(request_parameters.reverse_merge(value: value), [public_key])
end

def self.update(public_key, blacklist_entry_id, request_parameters = {})
post(request_parameters, [public_key, blacklist_entry_id])
end
def update(public_key, blacklist_entry_id, request_parameters = {})
post(request_parameters, [public_key, blacklist_entry_id])
end

def self.delete(public_key, blacklist_entry_id)
post({}, [public_key, blacklist_entry_id])
end
def delete(public_key, blacklist_entry_id)
post({}, [public_key, blacklist_entry_id])
end

def self.list(public_key, request_parameters = {})
get(request_parameters, [public_key])
end
def list(public_key, request_parameters = {})
get(request_parameters, [public_key])
end

def self.read(public_key, blacklist_entry_id)
get({}, [public_key, blacklist_entry_id])
end
def read(public_key, blacklist_entry_id)
get({}, [public_key, blacklist_entry_id])
end

class << self
protected

def results_key
Expand Down
12 changes: 7 additions & 5 deletions lib/mollom_rest_api/v1/captcha.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
class MollomRestApi::V1::Captcha < MollomRestApi::Interface
def self.create(type = 'image', request_parameters = {})
post(request_parameters.reverse_merge(type: type))
end
class << self
def create(type = 'image', request_parameters = {})
post(request_parameters.reverse_merge(type: type))
end

def self.verify(captcha_id, solution, request_parameters = {})
post(request_parameters.reverse_merge(solution: solution), captcha_id)
def verify(captcha_id, solution, request_parameters = {})
post(request_parameters.reverse_merge(solution: solution), captcha_id)
end
end
end
13 changes: 8 additions & 5 deletions lib/mollom_rest_api/v1/content.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
class MollomRestApi::V1::Content < MollomRestApi::Interface
def self.check(request_parameters = {})
post(request_parameters)
end
class << self
def create(request_parameters = {})
post(request_parameters)
end
alias_method :check, :create

def self.update(content_id, request_parameters = {})
post(request_parameters, [content_id])
def update(content_id, request_parameters = {})
post(request_parameters, [content_id])
end
end
end
8 changes: 2 additions & 6 deletions lib/mollom_rest_api/v1/feedback.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class MollomRestApi::V1::Feedback < MollomRestApi::Interface
class << self
def add(reason, request_parameters = {})
raise_missing_parameter_error unless content_xor_captcha_id_present_in?(request_parameters)
def create(reason, request_parameters = {})
raise_missing_parameter_error unless request_parameters.include?(:contentId) ^ request_parameters.include?(:captchaId)
post(request_parameters.reverse_merge(reason: reason))
end

Expand All @@ -10,9 +10,5 @@ def add(reason, request_parameters = {})
def raise_missing_parameter_error
raise ArgumentError.new("Specify one of two parameters: contentId or captchaId.")
end

def content_xor_captcha_id_present_in?(request_parameters)
request_parameters.include?(:contentId) ^ request_parameters.include?(:captchaId)
end
end
end
30 changes: 16 additions & 14 deletions lib/mollom_rest_api/v1/site.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
class MollomRestApi::V1::Site < MollomRestApi::Interface
def self.create(url, email, request_parameters = {})
post(request_parameters.reverse_merge(url: url, email: email))
end
class << self
def create(url, email, request_parameters = {})
post(request_parameters.reverse_merge(url: url, email: email))
end

def self.update(public_key, request_parameters = {})
post(request_parameters, [public_key])
end
def update(public_key, request_parameters = {})
post(request_parameters, [public_key])
end

def self.list(request_parameters = {})
get(request_parameters)
end
def list(request_parameters = {})
get(request_parameters)
end

def self.read(public_key)
get({}, [public_key])
end
def read(public_key)
get({}, [public_key])
end

def self.delete(public_key)
post({}, [public_key])
def delete(public_key)
post({}, [public_key])
end
end
end
34 changes: 16 additions & 18 deletions lib/mollom_rest_api/v1/whitelist.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,29 @@
class MollomRestApi::V1::Whitelist < MollomRestApi::Interface
class << self
def create(public_key, value, context, request_parameters = {})
post(request_parameters.reverse_merge(value: value, context: context), [public_key])
end

def self.create(public_key, value, context, request_parameters = {})
post(request_parameters.reverse_merge(value: value, context: context), [public_key])
end

def self.update(public_key, whitelist_entry_id, request_parameters = {})
post(request_parameters, [public_key, whitelist_entry_id])
end
def update(public_key, whitelist_entry_id, request_parameters = {})
post(request_parameters, [public_key, whitelist_entry_id])
end

def self.delete(public_key, whitelist_entry_id)
post({}, [public_key, whitelist_entry_id])
end
def delete(public_key, whitelist_entry_id)
post({}, [public_key, whitelist_entry_id])
end

def self.list(public_key, request_parameters = {})
get(request_parameters, [public_key])
end
def list(public_key, request_parameters = {})
get(request_parameters, [public_key])
end

def self.read(public_key, whitelist_entry_id)
get({}, [public_key, whitelist_entry_id])
end
def read(public_key, whitelist_entry_id)
get({}, [public_key, whitelist_entry_id])
end

class << self
protected

def results_key
'entry'
end
end

end
8 changes: 4 additions & 4 deletions spec/fixtures/v_test_01.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ def self.an_overriden_post_action
post({authorId: 9}, "delete")
end

def self.list
get
end

def self.an_overriden_get_action
get({status: :active}, "list")
end

def self.list
get
end

def self.delete
post({}, ['a_path_parameter'])
end
Expand Down
4 changes: 2 additions & 2 deletions spec/mollom_rest_api/interface_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
include_examples "api error handling", class_under_test: MollomRestApi::VTest01::Ticket, method_under_test: :an_overriden_get_action
end

describe 'a get action to fetch a list', vcr: {cassette_name: 'interface/some_api/a_get_list_action'} do
it "should call the correct url using the overriden properties in the method" do
describe 'a get action to fetch a list', vcr: {cassette_name: 'interface/some_api/list'} do
it "should call the correct url using the method name" do
expect(MollomRestApi::VTest01::Ticket.list).to eq('Valid response.')
end

Expand Down
6 changes: 3 additions & 3 deletions spec/mollom_rest_api/v1/captcha_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require "spec_helper"

describe MollomRestApi::V1::Captcha do
describe :create_captcha do
describe :create do
context "when no type is passed", vcr: {cassette_name: "captcha/create/default_request"} do
let(:response) {{"id"=>"TEST1wquvxygw6nkom", "url"=>"http://dev.mollom.com/v1/captcha/TEST1wquvxygw6nkom.png"}}

Expand Down Expand Up @@ -29,7 +29,7 @@
include_examples "api error handling", class_under_test: MollomRestApi::V1::Captcha, method_under_test: :create
end

describe :verify_captcha do
describe :verify do
context "when an invalid captcha id is passed", vcr: {cassette_name: "captcha/verify/invalid_captcha_id"} do
it "should raise an exception" do
expect{MollomRestApi::V1::Captcha.verify('inexistent_id', 'solution')}.to raise_exception do |exception|
Expand Down Expand Up @@ -58,6 +58,6 @@
end
end

include_examples "api error handling", class_under_test: MollomRestApi::V1::Captcha, method_under_test: :verify, method_args: [[9000], 'a_solution']
include_examples "api error handling", class_under_test: MollomRestApi::V1::Captcha, method_under_test: :verify, method_args: [9000, 'a_solution']
end
end
18 changes: 10 additions & 8 deletions spec/mollom_rest_api/v1/content_spec.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
require "spec_helper"

describe MollomRestApi::V1::Content do
describe :check do
context "when the api responded correctly", vcr: {cassette_name: "content/check/valid_request"} do
let(:request_parameters) {{postTitle: "My Title", postBody: "Some text right over here!", authorName: "Jean-Luc Picard"}}
let(:response) {{"id"=>"TEST1up51ut7qyu2b1", "spamScore"=>"0.5", "reason"=>"some secret reason", "postTitle"=>"My Title", "postBody"=>"Some text right over here!", "spamClassification"=>"unsure"}}
[:create, :check].each do |action|
describe action do
context "when the api responded correctly", vcr: {cassette_name: "content/create"} do
let(:request_parameters) {{postTitle: "My Title", postBody: "Some text right over here!", authorName: "Jean-Luc Picard"}}
let(:response) {{"id"=>"TEST1up51ut7qyu2b1", "spamScore"=>"0.5", "reason"=>"some secret reason", "postTitle"=>"My Title", "postBody"=>"Some text right over here!", "spamClassification"=>"unsure"}}

it "should return a json response classifying the content" do
expect(MollomRestApi::V1::Content.check(request_parameters)).to eq(response)
it "should return a json response classifying the content" do
expect(MollomRestApi::V1::Content.send(action, request_parameters)).to eq(response)
end
end
end

include_examples "api error handling", class_under_test: MollomRestApi::V1::Content, method_under_test: :check
include_examples "api error handling", class_under_test: MollomRestApi::V1::Content, method_under_test: action
end
end

describe :update do
Expand Down
12 changes: 6 additions & 6 deletions spec/mollom_rest_api/v1/feedback_spec.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
require "spec_helper"

describe MollomRestApi::V1::Feedback do
describe :add do
describe :create do
context "when no contentId or captchaId is specified" do
it "should raise an argument error" do
expect{MollomRestApi::V1::Feedback.add("spam")}.to raise_exception do |exception|
expect{MollomRestApi::V1::Feedback.create("spam")}.to raise_exception do |exception|
expect(exception).to be_a(ArgumentError)
expect(exception.message).to eq("Specify one of two parameters: contentId or captchaId.")
end
Expand All @@ -13,25 +13,25 @@

context "when any contentId is specified (valid or invalid)", vcr: {cassette_name: "feedback/add/content_request"} do
it "should have silently passed" do
expect(MollomRestApi::V1::Feedback.add("spam", contentId: "some_id")).to be_nil
expect(MollomRestApi::V1::Feedback.create("spam", contentId: "some_id")).to be_nil
end
end

context "when any captchaId is specified (valid or invalid)", vcr: {cassette_name: "feedback/add/captcha_request"} do
it "should have silently passed" do
expect(MollomRestApi::V1::Feedback.add("spam", captchaId: "some_id")).to be_nil
expect(MollomRestApi::V1::Feedback.create("spam", captchaId: "some_id")).to be_nil
end
end

context "when a contentId and a captchaId are passed" do
it "should raise an argument error" do
expect{MollomRestApi::V1::Feedback.add("spam", captchaId: "one", contentId: "two")}.to raise_exception do |exception|
expect{MollomRestApi::V1::Feedback.create("spam", captchaId: "one", contentId: "two")}.to raise_exception do |exception|
expect(exception).to be_a(ArgumentError)
expect(exception.message).to eq("Specify one of two parameters: contentId or captchaId.")
end
end
end

include_examples "api error handling", class_under_test: MollomRestApi::V1::Content, method_under_test: :check
include_examples "api error handling", class_under_test: MollomRestApi::V1::Feedback, method_under_test: :create, method_args: ['a_reason', {contentId: 'one'}]
end
end
18 changes: 11 additions & 7 deletions spec/mollom_rest_api/v1/site_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,23 @@
include_examples "api error handling", class_under_test: MollomRestApi::V1::Site, method_under_test: :list
end

describe :read, vcr: {cassette_name: 'site/read'} do
let(:response) { {"publicKey"=>"1cu4vdx7a6m9c1jc2425k1o9r8me2mrz", "privateKey"=>"1khj0vgpcqtjfwbbl5fr3wiykmr2ajro", "url"=>"http://url", "email"=>"an-email@gmail.com", "platformName"=>"Drupal", "platformVersion"=>"6.20", "expectedLanguages"=>nil} }
describe :read do
context 'when fetching a specific site', vcr: {cassette_name: 'site/read'} do
let(:response) { {"publicKey"=>"1cu4vdx7a6m9c1jc2425k1o9r8me2mrz", "privateKey"=>"1khj0vgpcqtjfwbbl5fr3wiykmr2ajro", "url"=>"http://url", "email"=>"an-email@gmail.com", "platformName"=>"Drupal", "platformVersion"=>"6.20", "expectedLanguages"=>nil} }

it 'should return the site' do
expect(MollomRestApi::V1::Site.read('1cu4vdx7a6m9c1jc2425k1o9r8me2mrz')).to eq(response)
it 'should return the site' do
expect(MollomRestApi::V1::Site.read('1cu4vdx7a6m9c1jc2425k1o9r8me2mrz')).to eq(response)
end
end

include_examples "api error handling", class_under_test: MollomRestApi::V1::Site, method_under_test: :read, method_args: ['a_public_key']
end

describe :delete, vcr: {cassette_name: 'site/delete'} do
it 'should delete a site' do
expect{MollomRestApi::V1::Site.delete('1cu4vdx7a6m9c1jc2425k1o9r8me2mrz')}.not_to raise_error
describe :delete do
context 'when deleting a specific site', vcr: {cassette_name: 'site/delete'} do
it 'should delete a site' do
expect{MollomRestApi::V1::Site.delete('1cu4vdx7a6m9c1jc2425k1o9r8me2mrz')}.not_to raise_error
end
end

include_examples "api error handling", class_under_test: MollomRestApi::V1::Site, method_under_test: :delete, method_args: ['a_public_key']
Expand Down
4 changes: 3 additions & 1 deletion spec/mollom_rest_api/v1_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@
describe MollomRestApi::V1 do
include AlternateSyntaxMacro

it_should_provide_an_alternate_syntax_to_api_calls
describe 'when calling the alternate syntax' do
it_should_provide_an_alternate_syntax_to_api_calls
end
end

0 comments on commit c4dc219

Please sign in to comment.