active_record_to_simpledb 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem 'right_aws'
4
+ gem 'activerecord', '>=2.1.2'
5
+ gem 'mysql'
6
+
7
+
8
+ group :development do
9
+ gem "shoulda", ">= 0"
10
+ gem "bundler", "~> 1.0.0"
11
+ gem "jeweler", "~> 1.6.4"
12
+ gem "rcov", ">= 0"
13
+ gem 'rspec'
14
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,55 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ activemodel (3.1.0)
5
+ activesupport (= 3.1.0)
6
+ bcrypt-ruby (~> 3.0.0)
7
+ builder (~> 3.0.0)
8
+ i18n (~> 0.6)
9
+ activerecord (3.1.0)
10
+ activemodel (= 3.1.0)
11
+ activesupport (= 3.1.0)
12
+ arel (~> 2.2.1)
13
+ tzinfo (~> 0.3.29)
14
+ activesupport (3.1.0)
15
+ multi_json (~> 1.0)
16
+ arel (2.2.1)
17
+ bcrypt-ruby (3.0.1)
18
+ builder (3.0.0)
19
+ diff-lcs (1.1.3)
20
+ git (1.2.5)
21
+ i18n (0.6.0)
22
+ jeweler (1.6.4)
23
+ bundler (~> 1.0)
24
+ git (>= 1.2.5)
25
+ rake
26
+ multi_json (1.0.3)
27
+ mysql (2.8.1)
28
+ rake (0.9.2.2)
29
+ rcov (0.9.11)
30
+ right_aws (2.1.0)
31
+ right_http_connection (>= 1.2.5)
32
+ right_http_connection (1.3.0)
33
+ rspec (2.7.0)
34
+ rspec-core (~> 2.7.0)
35
+ rspec-expectations (~> 2.7.0)
36
+ rspec-mocks (~> 2.7.0)
37
+ rspec-core (2.7.1)
38
+ rspec-expectations (2.7.0)
39
+ diff-lcs (~> 1.1.2)
40
+ rspec-mocks (2.7.0)
41
+ shoulda (2.11.3)
42
+ tzinfo (0.3.30)
43
+
44
+ PLATFORMS
45
+ ruby
46
+
47
+ DEPENDENCIES
48
+ activerecord (>= 2.1.2)
49
+ bundler (~> 1.0.0)
50
+ jeweler (~> 1.6.4)
51
+ mysql
52
+ rcov
53
+ right_aws
54
+ rspec
55
+ shoulda
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Ivan Acosta-Rubio
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,19 @@
1
+ = active_record_to_simpledb
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to active_record_to_simpledb
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
9
+ * Fork the project
10
+ * Start a feature/bugfix branch
11
+ * Commit and push until you are happy with your contribution
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2012 Ivan Acosta-Rubio. See LICENSE.txt for
18
+ further details.
19
+
data/Rakefile ADDED
@@ -0,0 +1,50 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ gem.name = "active_record_to_simpledb"
17
+ gem.homepage = "http://github.com/ivanacostarubio/active_record_to_simpledb"
18
+ gem.license = "MIT"
19
+ gem.summary = %Q{It sends ActiveRecord object to SimpleDB using Resque}
20
+ gem.description = %Q{It sends ActiveRecord objets to SimpleDB using Resque}
21
+ gem.email = "ivan@bakedweb.net"
22
+ gem.authors = ["Ivan Acosta-Rubio"]
23
+ end
24
+ Jeweler::RubygemsDotOrgTasks.new
25
+
26
+ require 'rake/testtask'
27
+ Rake::TestTask.new(:test) do |test|
28
+ test.libs << 'lib' << 'test'
29
+ test.pattern = 'test/**/test_*.rb'
30
+ test.verbose = true
31
+ end
32
+
33
+ require 'rcov/rcovtask'
34
+ Rcov::RcovTask.new do |test|
35
+ test.libs << 'test'
36
+ test.pattern = 'test/**/test_*.rb'
37
+ test.verbose = true
38
+ test.rcov_opts << '--exclude "gems/*"'
39
+ end
40
+
41
+ task :default => :test
42
+
43
+ require 'rake/rdoctask'
44
+ Rake::RDocTask.new do |rdoc|
45
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
46
+ rdoc.rdoc_dir = 'rdoc'
47
+ rdoc.title = "active_record_to_simpledb #{version}"
48
+ rdoc.rdoc_files.include('README*')
49
+ rdoc.rdoc_files.include('lib/**/*.rb')
50
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,75 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{active_record_to_simpledb}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Ivan Acosta-Rubio"]
12
+ s.date = %q{2012-01-05}
13
+ s.description = %q{It sends ActiveRecord objets to SimpleDB using Resque}
14
+ s.email = %q{ivan@bakedweb.net}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".rspec",
22
+ "Gemfile",
23
+ "Gemfile.lock",
24
+ "LICENSE.txt",
25
+ "README.rdoc",
26
+ "Rakefile",
27
+ "VERSION",
28
+ "active_record_to_simpledb.gemspec",
29
+ "lib/active_record_to_simpledb.rb",
30
+ "spec/active_record_setup.rb",
31
+ "spec/active_record_to_simpledb_spec.rb",
32
+ "spec/aws.rb",
33
+ "spec/database.yml"
34
+ ]
35
+ s.homepage = %q{http://github.com/ivanacostarubio/active_record_to_simpledb}
36
+ s.licenses = ["MIT"]
37
+ s.require_paths = ["lib"]
38
+ s.rubygems_version = %q{1.3.6}
39
+ s.summary = %q{It sends ActiveRecord object to SimpleDB using Resque}
40
+
41
+ if s.respond_to? :specification_version then
42
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
43
+ s.specification_version = 3
44
+
45
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
46
+ s.add_runtime_dependency(%q<right_aws>, [">= 0"])
47
+ s.add_runtime_dependency(%q<activerecord>, [">= 2.1.2"])
48
+ s.add_runtime_dependency(%q<mysql>, [">= 0"])
49
+ s.add_development_dependency(%q<shoulda>, [">= 0"])
50
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
51
+ s.add_development_dependency(%q<jeweler>, ["~> 1.6.4"])
52
+ s.add_development_dependency(%q<rcov>, [">= 0"])
53
+ s.add_development_dependency(%q<rspec>, [">= 0"])
54
+ else
55
+ s.add_dependency(%q<right_aws>, [">= 0"])
56
+ s.add_dependency(%q<activerecord>, [">= 2.1.2"])
57
+ s.add_dependency(%q<mysql>, [">= 0"])
58
+ s.add_dependency(%q<shoulda>, [">= 0"])
59
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
60
+ s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
61
+ s.add_dependency(%q<rcov>, [">= 0"])
62
+ s.add_dependency(%q<rspec>, [">= 0"])
63
+ end
64
+ else
65
+ s.add_dependency(%q<right_aws>, [">= 0"])
66
+ s.add_dependency(%q<activerecord>, [">= 2.1.2"])
67
+ s.add_dependency(%q<mysql>, [">= 0"])
68
+ s.add_dependency(%q<shoulda>, [">= 0"])
69
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
70
+ s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
71
+ s.add_dependency(%q<rcov>, [">= 0"])
72
+ s.add_dependency(%q<rspec>, [">= 0"])
73
+ end
74
+ end
75
+
@@ -0,0 +1,113 @@
1
+ class ActiveRecordToSimpledb
2
+
3
+ # Public: It connect to aws
4
+ #
5
+ # Example
6
+ #
7
+ # ActiveRecordToSimpledb.aws_connect
8
+ #
9
+ # Return the connection object to AWS
10
+ def self.aws_connect
11
+ RightAws::SdbInterface.new(AWS.key, AWS.secret)
12
+ end
13
+
14
+ #
15
+ # Public: stablishes the connection
16
+ #
17
+ def self.connect
18
+ RightAws::ActiveSdb.establish_connection
19
+ end
20
+
21
+ #
22
+ # Public: It created a new Simpledb domain
23
+ #
24
+ # string - The name of the domain we want to create
25
+ # Example
26
+ #
27
+ # ActiveRecordToSimpledb.create_domain("new_domain")
28
+ #
29
+ def self.create_domain(name)
30
+ ActiveRecordToSimpledb.aws_connect.create_domain(name)
31
+ end
32
+
33
+ class Client
34
+ #
35
+ # Public: This method create simpledb records
36
+ #
37
+ # hash - model attributes
38
+ #
39
+ def self.create(domain,attr)
40
+ ActiveRecordToSimpledb.create_domain(domain)
41
+ ActiveRecordToSimpledb.aws_connect.put_attributes(domain, attr["id"], attr)
42
+ end
43
+ end
44
+
45
+ module Query
46
+
47
+ #
48
+ # Public: It finds a Simpledb record given the domain and record id
49
+ #
50
+ # string - the name of the domain to query
51
+ # integer - the id of the record in ActiveRecord
52
+ #
53
+ def self.find(domain, id)
54
+ ActiveRecordToSimpledb.aws_connect.get_attributes(domain, id)
55
+ end
56
+
57
+ #
58
+ # Public: It finds a Simpledb record using a sqlish query
59
+ # array / string - A SQLish array with the query
60
+ #
61
+ # Example:
62
+ #
63
+ # query = ["select * from ticket_sales where event_id=?", '123']
64
+ # ActiveRecordToSimpledb::Query.find_by_sql( query )
65
+ #
66
+ def self.find_by_sql(query)
67
+ ActiveRecordToSimpledb.aws_connect.select(query)
68
+ end
69
+ end
70
+
71
+ module Callbacks
72
+
73
+ module Create
74
+
75
+ def domain_name
76
+ self.class.to_s.downcase
77
+ end
78
+ #
79
+ # Private: it create a resque job in we are not in test. Otherwise, it create the simpledb object
80
+ #
81
+ def send_object_to_simpledb
82
+ if RAILS_ENV == "test"
83
+ ActiveRecordToSimpledb::Client.create(domain_name, self.attributes)
84
+ else
85
+ Resque.enqueue(ActiveRecordToSimpledb::Resque::Create, self.attributes)
86
+ end
87
+ end
88
+
89
+ #
90
+ # Private: some meta programing to create a callback in the model class.
91
+ #
92
+ def self.included(base)
93
+ base.class_eval do
94
+ after_create :send_object_to_simpledb
95
+ end
96
+ end
97
+ end
98
+ end
99
+
100
+ #
101
+ # Private: a module with the resque jos
102
+ #
103
+ module Resque
104
+ class Create
105
+ @queue = :active_record_to_simple_db_create
106
+ def self.perform(attributes)
107
+ ActiveRecordToSimpledb::Client.create(self.attributes)
108
+ end
109
+ end
110
+ end
111
+ end
112
+
113
+
@@ -0,0 +1,16 @@
1
+ RAILS_ENV = "test"
2
+
3
+ config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
4
+ ActiveRecord::Base.logger = ActiveSupport::BufferedLogger.new(File.dirname(__FILE__) + "debug.log")
5
+ ActiveRecord::Base.establish_connection(config['test'])
6
+
7
+ def rebuild_model options = {}
8
+ ActiveRecord::Base.connection.create_table :ticket_sales, :force => true do |table|
9
+ table.column :event_id, :integer
10
+ table.column :title, :string
11
+ end
12
+ end
13
+
14
+ rebuild_model
15
+
16
+
@@ -0,0 +1,72 @@
1
+ require 'json'
2
+ require 'right_aws'
3
+ require 'sdb/active_sdb'
4
+ require 'activerecord'
5
+ require 'active_record_setup'
6
+ require 'active_record_to_simpledb'
7
+
8
+ require 'aws'
9
+
10
+ class TicketSale < ActiveRecord::Base
11
+ include ActiveRecordToSimpledb::Callbacks::Create
12
+ end
13
+
14
+
15
+ class Rails
16
+
17
+ def self.version
18
+ '2.1.2'
19
+ end
20
+
21
+ def self.root
22
+ Dir.pwd
23
+ end
24
+ end
25
+
26
+ describe ActiveRecordToSimpledb do
27
+
28
+ context "conection" do
29
+
30
+ it "does authenticate with credentails" do
31
+ RightAws::SdbInterface.should_receive(:new).with(AWS.key,AWS.secret)
32
+ ActiveRecordToSimpledb.aws_connect
33
+ end
34
+
35
+ it "connects to simpledb" do
36
+ RightAws::ActiveSdb.should_receive(:establish_connection)
37
+ ActiveRecordToSimpledb.connect
38
+ end
39
+
40
+ end
41
+
42
+ it "saves the object when inserted in model" do
43
+ attr = { "id" => 1, "event_id" => 123, "title" => "Hello"}
44
+ ActiveRecordToSimpledb::Client.should_receive(:create).with("ticketsale", attr)
45
+ TicketSale.create(attr)
46
+ end
47
+
48
+ it "uses resque to send the object" do
49
+
50
+ end
51
+
52
+ context "integration" do
53
+
54
+
55
+ before(:all) do
56
+ @attr = {"id" => 1, "event_id" => 123, "title" => "Hello" }
57
+ TicketSale.create(@attr)
58
+ end
59
+
60
+ it "retrevies an object from simple db" do
61
+ t = ActiveRecordToSimpledb::Query.find("ticketsale", 2)
62
+ t[:attributes]["title"].should == ["Hello"]
63
+ end
64
+
65
+ it "queries for event id" do
66
+ query = ["select * from ticketsale where event_id=?", '123']
67
+ t = ActiveRecordToSimpledb::Query.find_by_sql( query )
68
+ t[:items][0].first[1]["title"].should == ["Hello"]
69
+ end
70
+ end
71
+
72
+ end
data/spec/aws.rb ADDED
@@ -0,0 +1,17 @@
1
+ class AWS
2
+
3
+ def self.root
4
+ rails_root = (Rails.version < "2.1.2") ? RAILS_ROOT : Rails.root
5
+ YAML::load(IO.read(File.join(rails_root, 'config', 'aws.yml')))
6
+ end
7
+
8
+ def self.key
9
+ AWS.root[RAILS_ENV]['access_key_id']
10
+ end
11
+
12
+ def self.secret
13
+ AWS.root[RAILS_ENV]['secret_access_key']
14
+ end
15
+ end
16
+
17
+
data/spec/database.yml ADDED
@@ -0,0 +1,5 @@
1
+ test:
2
+ adapter: mysql
3
+ host: localhost
4
+ username: root
5
+ database: active_record_to_simpledb
metadata ADDED
@@ -0,0 +1,177 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_record_to_simpledb
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Ivan Acosta-Rubio
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2012-01-05 00:00:00 -04:30
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ prerelease: false
22
+ type: :runtime
23
+ name: right_aws
24
+ version_requirements: &id001 !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 0
30
+ version: "0"
31
+ requirement: *id001
32
+ - !ruby/object:Gem::Dependency
33
+ prerelease: false
34
+ type: :runtime
35
+ name: activerecord
36
+ version_requirements: &id002 !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ segments:
41
+ - 2
42
+ - 1
43
+ - 2
44
+ version: 2.1.2
45
+ requirement: *id002
46
+ - !ruby/object:Gem::Dependency
47
+ prerelease: false
48
+ type: :runtime
49
+ name: mysql
50
+ version_requirements: &id003 !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ segments:
55
+ - 0
56
+ version: "0"
57
+ requirement: *id003
58
+ - !ruby/object:Gem::Dependency
59
+ prerelease: false
60
+ type: :development
61
+ name: shoulda
62
+ version_requirements: &id004 !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ segments:
67
+ - 0
68
+ version: "0"
69
+ requirement: *id004
70
+ - !ruby/object:Gem::Dependency
71
+ prerelease: false
72
+ type: :development
73
+ name: bundler
74
+ version_requirements: &id005 !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ~>
77
+ - !ruby/object:Gem::Version
78
+ segments:
79
+ - 1
80
+ - 0
81
+ - 0
82
+ version: 1.0.0
83
+ requirement: *id005
84
+ - !ruby/object:Gem::Dependency
85
+ prerelease: false
86
+ type: :development
87
+ name: jeweler
88
+ version_requirements: &id006 !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ~>
91
+ - !ruby/object:Gem::Version
92
+ segments:
93
+ - 1
94
+ - 6
95
+ - 4
96
+ version: 1.6.4
97
+ requirement: *id006
98
+ - !ruby/object:Gem::Dependency
99
+ prerelease: false
100
+ type: :development
101
+ name: rcov
102
+ version_requirements: &id007 !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ segments:
107
+ - 0
108
+ version: "0"
109
+ requirement: *id007
110
+ - !ruby/object:Gem::Dependency
111
+ prerelease: false
112
+ type: :development
113
+ name: rspec
114
+ version_requirements: &id008 !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ segments:
119
+ - 0
120
+ version: "0"
121
+ requirement: *id008
122
+ description: It sends ActiveRecord objets to SimpleDB using Resque
123
+ email: ivan@bakedweb.net
124
+ executables: []
125
+
126
+ extensions: []
127
+
128
+ extra_rdoc_files:
129
+ - LICENSE.txt
130
+ - README.rdoc
131
+ files:
132
+ - .document
133
+ - .rspec
134
+ - Gemfile
135
+ - Gemfile.lock
136
+ - LICENSE.txt
137
+ - README.rdoc
138
+ - Rakefile
139
+ - VERSION
140
+ - active_record_to_simpledb.gemspec
141
+ - lib/active_record_to_simpledb.rb
142
+ - spec/active_record_setup.rb
143
+ - spec/active_record_to_simpledb_spec.rb
144
+ - spec/aws.rb
145
+ - spec/database.yml
146
+ has_rdoc: true
147
+ homepage: http://github.com/ivanacostarubio/active_record_to_simpledb
148
+ licenses:
149
+ - MIT
150
+ post_install_message:
151
+ rdoc_options: []
152
+
153
+ require_paths:
154
+ - lib
155
+ required_ruby_version: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ segments:
160
+ - 0
161
+ version: "0"
162
+ required_rubygems_version: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ segments:
167
+ - 0
168
+ version: "0"
169
+ requirements: []
170
+
171
+ rubyforge_project:
172
+ rubygems_version: 1.3.6
173
+ signing_key:
174
+ specification_version: 3
175
+ summary: It sends ActiveRecord object to SimpleDB using Resque
176
+ test_files: []
177
+