Skip to content
This repository has been archived by the owner on May 8, 2020. It is now read-only.

Commit

Permalink
Merge pull request #6 from samsaradog/master
Browse files Browse the repository at this point in the history
Convert spaces to underscores in response keys.
  • Loading branch information
aevernon committed Jan 12, 2015
2 parents 0a1eedd + 2f767bc commit 97cc017
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
9 changes: 7 additions & 2 deletions lib/commission_junction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,15 @@ def initialize(params)
# Credit: http://listlibrary.net/ruby-talk/2004/03/00sGI1cD
params.each do |key, val|
raise ArgumentError, "key must be a String; got #{key.class} instead" unless key.is_a?(String)
instance_variable_set("@#{key}".intern, val)
instance_eval %Q{ class << self ; attr_reader #{key.intern.inspect} ; end }
clean_key = clean_key_name(key)
instance_variable_set("@#{clean_key}".intern, val)
instance_eval %Q{ class << self ; attr_reader #{clean_key.intern.inspect} ; end }
end
end

def clean_key_name(name)
name.strip.gsub(/\s/, '_')
end
end

class Product < CjObject
Expand Down
33 changes: 29 additions & 4 deletions test/commission_junction_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -389,11 +389,15 @@ def check_link_search_results(results)
end
end

def set_up_service
CommissionJunction.new('developer_key', 123456)
end

def test_contents_extractor_with_first_level
contents = "abc"
response = {'cj_api' => {'first' => contents}}

cj = CommissionJunction.new('developer_key', 123456)
cj = set_up_service

assert_equal(contents, cj.extract_contents(response, "first"))
end
Expand All @@ -402,7 +406,7 @@ def test_contents_extractor_with_second_level
contents = "abc"
response = {'cj_api' => {'first' => {'second' => contents}}}

cj = CommissionJunction.new('developer_key', 123456)
cj = set_up_service

assert_equal(contents, cj.extract_contents(response, "first", "second"))
end
Expand All @@ -411,7 +415,7 @@ def test_contents_extractor_with_error_message
contents = "abc"
response = {'cj_api' => {'error_message' => contents}}

cj = CommissionJunction.new('developer_key', 123456)
cj = set_up_service

assert_raises ArgumentError do
cj.extract_contents(response, "first")
Expand All @@ -421,10 +425,31 @@ def test_contents_extractor_with_error_message
def test_contents_extractor_with_no_cj_api
response = {}

cj = CommissionJunction.new('developer_key', 123456)
cj = set_up_service

assert_raises ArgumentError do
cj.extract_contents(response, "first")
end
end

def set_up_cj_object
CommissionJunction::CjObject.new({"a" => "a"})
end

def test_key_conversion_with_spaces
cjo = set_up_cj_object

assert_equal("abc_def", cjo.clean_key_name("abc def"))
end

def test_key_conversion_with_trailing_spaces
cjo = set_up_cj_object

assert_equal("abcdef", cjo.clean_key_name("abcdef "))
end

def test_initializing_product_using_key_with_spaces
product = CommissionJunction::Product.new("abc def" => "123")
assert_equal(product.abc_def, "123")
end
end

0 comments on commit 97cc017

Please sign in to comment.