-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Sean O'Brien
committed
Jan 27, 2009
0 parents
commit a6b4f5f
Showing
14 changed files
with
334 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/doc | ||
.gem | ||
test/*.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
Copyright (c) 2009 Sean O'Brien | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining | ||
a copy of this software and associated documentation files (the | ||
"Software"), to deal in the Software without restriction, including | ||
without limitation the rights to use, copy, modify, merge, publish, | ||
distribute, sublicense, and/or sell copies of the Software, and to | ||
permit persons to whom the Software is furnished to do so, subject to | ||
the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
= has_force | ||
|
||
This plugin will submit an ActiveRecord model to SalesForce.com using the Web2Lead gateway. | ||
|
||
== Example usage | ||
|
||
class Lead < ActiveRecord::Base | ||
has_force :oid => '123' | ||
end | ||
|
||
Lead.new(:first_name => "Test").to_salesforce | ||
|
||
|
||
Copyright (c) 2009 Sean O'Brien, released under the MIT license |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
require 'rake' | ||
require 'rake/testtask' | ||
require 'rake/rdoctask' | ||
|
||
desc 'Default: run unit tests.' | ||
task :default => :test | ||
|
||
desc 'Test the test plugin.' | ||
Rake::TestTask.new(:test) do |t| | ||
t.libs << 'lib' | ||
t.libs << 'test' | ||
t.pattern = 'test/**/*_test.rb' | ||
t.verbose = true | ||
end | ||
|
||
desc 'Generate documentation for the test plugin.' | ||
Rake::RDocTask.new(:rdoc) do |rdoc| | ||
rdoc.rdoc_dir = 'doc' | ||
rdoc.title = 'has_force' | ||
rdoc.options << '--line-numbers' << '--inline-source' | ||
rdoc.options << '--webcvs=http://github.com/sob/has_force/tree/master/' | ||
rdoc.rdoc_files.include('README.rdoc') | ||
rdoc.rdoc_files.include('lib/**/*.rb') | ||
end | ||
|
||
desc 'Update .manifest with the latest list of project filenames' | ||
task :manifest do | ||
list = Dir['**/*'].sort | ||
spec_file = Dir['*.gemspec'].first | ||
list -= [spec_file] if spec_file | ||
|
||
File.read('.gitignore').each_line do |glob| | ||
glob = glob.chomp.sub(/^\//, '') | ||
list -= Dir[glob] | ||
list -= Dir["#{glob}/**/*"] if File.directory?(glob) && !File.symlink?(glob) | ||
puts "excluding #{glob}" | ||
end | ||
|
||
if spec_file | ||
spec = File.read spec_file | ||
spec.gsub! /^(\s* s.(test_)?files \s* = \s* )( \[ [^\]]* \] | %w\( [^)]* \) )/mx do | ||
assignment = $1 | ||
bunch = $2 ? list.grep(/^test\//) : list | ||
'%s%%w(%s)' % [ assignment, bunch.join(' ') ] | ||
end | ||
|
||
File.open(spec_file, 'w') {|f| f << spec } | ||
end | ||
File.open('.manifest', 'w') {|f| f << list.join("\n") } | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
require 'force/has_force' | ||
require 'force/core_ext/array' | ||
require 'force/core_ext/hash' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
unless Array.instance_methods.include? 'stringify' | ||
Array.class_eval do | ||
# converts all values into strings in the current array | ||
# [:one, 2, 3].stringify! => ['one', '2', '3'] | ||
def stringify! | ||
collect{|v| v.to_s } | ||
end | ||
|
||
# return a copy of the current array with all values converted to strings | ||
# [:one, 2, 3].stringify => ['one', '2', '3'] | ||
def stringify | ||
dup.stringify! | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
unless Hash.instance_methods.include? '-' | ||
Hash.class_eval do | ||
# removes one or more keys from a hash | ||
# {:red => 1, :blue => 2, :green => 3} - [:red, :blue] => {:green => 3} | ||
def -(v) | ||
hsh = self.dup | ||
(v.is_a?(Array) ? v : [v]).each{|k| hsh.delete(k) } | ||
hsh | ||
end | ||
end | ||
end | ||
|
||
unless Hash.instance_methods.include? 'stringify_values' | ||
Hash.class_eval do | ||
# returns a new hash with each of the values converted to a string | ||
# {:red => 1, :blue => 2} => {:red => '1', :blue => '2'} | ||
def stringify_values | ||
inject({}) do |options, (key, value)| | ||
options[key] = value.to_s | ||
options | ||
end | ||
end | ||
|
||
# returns the hash with each of the values converted to a string | ||
# {:red => 1, :blue => 2} => {:red => '1', :blue => '2'} | ||
def stringify_values! | ||
self.each do |key, value| | ||
self[key] = value.to_s | ||
end | ||
self | ||
end | ||
end | ||
end | ||
|
||
unless Hash.instance_methods.include? 'transform_key' | ||
Hash.class_eval do | ||
# returns a new hash with a key renamed | ||
# {:one => 1, :two => 2}.transform_key(:two, :three) => {:one => 1, :three => 2} | ||
def transform_key(old_key, new_key) | ||
self.dup.transform_key!(old_key, new_key) | ||
end | ||
|
||
# renames a key in a hash | ||
# {:one => 1, :two => 2}.transform_key(:two, :three) => {:one => 1, :three => 2} | ||
def transform_key!(old_key, new_key) | ||
self[new_key] = self.delete(old_key) | ||
return self | ||
end | ||
|
||
# returns a new hash with renamed keys | ||
# accepts a hash of key, value pairs to rename | ||
# {:one => 1, :two => 2}.transform_keys(:two => :three) => {:one => 1, :three => 2} | ||
def transform_keys(transform) | ||
self.dup.transform_keys!(transform) | ||
end | ||
|
||
# returns a hash with renamed keys | ||
# accepts a hash of key, value pairs to rename | ||
# {:one => 1, :two => 2}.transform_keys(:two => :three) => {:one => 1, :three => 2} | ||
def transform_keys!(transform) | ||
raise ArgumentError, "transform_keys takes a single hash argument" unless transform.is_a?(Hash) | ||
self.each_key do |k| | ||
self[transform.has_key?(k) ? transform[k] : k] = self.delete(k) | ||
end | ||
self | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
require 'net/http' | ||
require 'net/https' | ||
require 'uri' | ||
|
||
module Force | ||
def self.included(base) | ||
base.send :extend, ClassMethods | ||
end | ||
|
||
module ClassMethods | ||
def has_force(options = {}) | ||
options = { | ||
:skip_fields => [ :id, :created_at, :created_by, :updated_at, :updated_by, :status ], | ||
:transform_fields => { }, | ||
:oid => '' | ||
}.merge(options) | ||
|
||
cattr_accessor :force_skip_fields | ||
cattr_accessor :force_transform_fields | ||
cattr_accessor :force_oid | ||
|
||
self.force_skip_fields = options[:skip_fields] | ||
self.force_transform_fields = options[:transform_fields] | ||
self.force_oid = options[:oid] | ||
|
||
send :include, InstanceMethods | ||
end | ||
end | ||
|
||
module InstanceMethods | ||
def to_salesforce | ||
url = URI.parse('https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8') | ||
http = Net::HTTP.new(url.host, url.port) | ||
http.use_ssl = true | ||
http.verify_mode = OpenSSL::SSL::VERIFY_NONE | ||
request = Net::HTTP::Post.new(url.path) | ||
attribs = (self.attributes - self.class.force_skip_fields.stringify).stringify_values! | ||
attribs.transform_keys!(self.class.force_transform_fields) | ||
request.set_form_data(attribs.merge({:oid => self.class.force_oid})) | ||
begin | ||
response = http.request(request) | ||
case response | ||
when Net::HTTPSuccess, Net::HTTPRedirection | ||
return true | ||
end | ||
end | ||
return false | ||
end | ||
end | ||
end | ||
|
||
ActiveRecord::Base.send :include, Force |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
require 'force' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
require File.expand_path(File.join(File.dirname(__FILE__), 'test_helper.rb')) | ||
|
||
class CoreExtTest < Test::Unit::TestCase | ||
def test_array_stringify_values | ||
assert_equal ["test", "1", "red"], [:test, 1, :red].stringify! | ||
end | ||
|
||
def test_hash_minus_key | ||
assert_equal({:one => '1'}, {:one => '1', :two => '2'} - [:two]) | ||
end | ||
|
||
def test_hash_stringify_values | ||
assert_equal({:one => '1', :two => 'two'}, {:one => 1, :two => :two}.stringify_values!) | ||
end | ||
|
||
def test_hash_transform_key | ||
assert_equal({:one => '1', :three => '2'}, {:one => '1', :two => '2'}.transform_key(:two, :three)) | ||
end | ||
|
||
def test_hash_transform_multiple_keys | ||
assert_equal({:one => '1', :three => '2', :five => '4'}, {:one => '1', :two => '2', :four => '4'}.transform_keys({:two => :three, :four => :five})) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
sqlite: | ||
:adapter: sqlite | ||
:database: ":memory:" | ||
|
||
sqlite3: | ||
:adapter: sqlite3 | ||
:database: ":memory:" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
require File.expand_path(File.join(File.dirname(__FILE__), 'test_helper')) | ||
|
||
class HasForceTest < Test::Unit::TestCase | ||
load_schema | ||
|
||
class Lead < ActiveRecord::Base | ||
has_force :oid => '123' | ||
end | ||
|
||
def test_schema_has_loaded_correctly | ||
assert_equal [], Lead.all | ||
end | ||
|
||
def test_lead_class_sets_force_oid_variable | ||
assert_not_nil(Lead.force_oid) | ||
assert_equal('123', Lead.force_oid) | ||
end | ||
|
||
def test_lead_class_sets_force_skip_fields | ||
assert_not_nil(Lead.force_skip_fields) | ||
assert_equal([:id, :created_at, :created_by, :updated_at, :updated_by, :status], Lead.force_skip_fields) | ||
end | ||
|
||
def test_lead_class_sets_force_transform_fields | ||
assert_not_nil(Lead.force_transform_fields) | ||
assert_equal({}, Lead.force_transform_fields) | ||
end | ||
|
||
def test_a_lead_responds_to_salesforce | ||
@lead = Lead.new | ||
assert_respond_to(@lead, :to_salesforce) | ||
end | ||
|
||
def test_a_lead_submits_to_salesforce | ||
@lead = Lead.new | ||
assert_equal @lead.to_salesforce, true | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
ActiveRecord::Schema.define(:version => 0) do | ||
create_table :leads, :force => true do |t| | ||
t.string :first_name | ||
t.string :last_name | ||
t.datetime :submitted_at | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
ENV['RAILS_ENV'] = 'test' | ||
ENV['RAILS_ROOT'] ||= File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '..', '..')) | ||
|
||
require 'test/unit' | ||
require File.expand_path(File.join(ENV['RAILS_ROOT'], 'config', 'environment.rb')) | ||
|
||
def load_schema | ||
config = YAML::load(IO.read(File.expand_path(File.join(File.dirname(__FILE__), 'database.yml')))) | ||
ActiveRecord::Base.logger = Logger.new(File.expand_path(File.join(File.dirname(__FILE__), 'debug.log'))) | ||
|
||
db_adapter = ENV['db'] | ||
|
||
db_adapter ||= | ||
begin | ||
require 'rubygems' | ||
require 'sqlite' | ||
'sqlite' | ||
rescue MissingSourceFile | ||
begin | ||
require 'sqlite3' | ||
'sqlite3' | ||
rescue MissingSourceFile | ||
end | ||
end | ||
|
||
if db_adapter.nil? | ||
raise "No DB Adapter selected. Pass the DB= option to pick one or install sqlite or sqlite3" | ||
end | ||
|
||
ActiveRecord::Base.establish_connection(config[db_adapter]) | ||
load(File.expand_path(File.join(File.dirname(__FILE__), 'schema.rb'))) | ||
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'rails', 'init.rb')) | ||
end |