hotwire 0.0.0 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.markdown +82 -0
- data/Rakefile +2 -1
- data/VERSION +1 -1
- data/hotwire.gemspec +30 -23
- data/lib/hotwire.rb +13 -7
- data/lib/hotwire/base.rb +44 -0
- data/lib/hotwire/request.rb +60 -0
- data/lib/hotwire/response.rb +18 -0
- data/lib/hotwire/response/base.rb +54 -0
- data/lib/hotwire/response/csv.rb +17 -0
- data/lib/hotwire/response/html.rb +11 -0
- data/lib/hotwire/response/invalid.rb +16 -0
- data/lib/hotwire/response/json.rb +96 -0
- data/test/response/test_base.rb +46 -0
- data/test/response/test_csv.rb +27 -0
- data/test/response/test_html.rb +26 -0
- data/test/response/test_invalid.rb +24 -0
- data/test/response/test_json.rb +30 -0
- data/test/test_active_record_mixins.rb +61 -65
- data/test/test_helper.rb +12 -0
- data/test/test_request.rb +56 -0
- data/test/test_response.rb +42 -0
- metadata +31 -24
- data/README.makrdown +0 -46
- data/lib/hotwire/active_record_mixins.rb +0 -95
- data/lib/hotwire/column_headers.rb +0 -54
- data/lib/hotwire/core_extensions.rb +0 -11
- data/lib/hotwire/row.rb +0 -46
- data/lib/hotwire/table.rb +0 -27
- data/test/active_record_test_helper.rb +0 -14
- data/test/test_column_headers.rb +0 -20
- data/test/test_core_extensions.rb +0 -26
- data/test/test_row.rb +0 -17
- data/test/test_table.rb +0 -40
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class TestResponse < Test::Unit::TestCase
|
4
|
+
context "Hotwire::Response" do
|
5
|
+
|
6
|
+
context "from_request" do
|
7
|
+
context "for json" do
|
8
|
+
setup { @response = Hotwire::Response.from_request(valid_request(:out => 'json')) }
|
9
|
+
|
10
|
+
should "be an instance of Hotwire::Response::Json" do
|
11
|
+
assert @response.is_a?(Hotwire::Response::Json)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
context "for html" do
|
16
|
+
setup { @response = Hotwire::Response.from_request(valid_request(:out => 'html')) }
|
17
|
+
|
18
|
+
should "be an instance of Hotwire::Response::Html" do
|
19
|
+
assert @response.is_a?(Hotwire::Response::Html)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
context "for csv" do
|
24
|
+
setup { @response = Hotwire::Response.from_request(valid_request(:out => 'csv')) }
|
25
|
+
|
26
|
+
should "be an instance of Hotwire::Response::Csv" do
|
27
|
+
assert @response.is_a?(Hotwire::Response::Csv)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
context "for an invalid type" do
|
32
|
+
setup { @response = Hotwire::Response.from_request(valid_request(:out => 'invalid')) }
|
33
|
+
|
34
|
+
should "be an instance of Hotwire::Response::Invalid" do
|
35
|
+
assert @response.is_a?(Hotwire::Response::Invalid)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
+
- 1
|
7
8
|
- 0
|
8
|
-
|
9
|
-
version: 0.0.0
|
9
|
+
version: 0.1.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Les Freeman
|
@@ -14,11 +14,11 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-10-
|
17
|
+
date: 2010-10-25 00:00:00 -07:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
|
-
name:
|
21
|
+
name: thoughtbot-shoulda
|
22
22
|
prerelease: false
|
23
23
|
requirement: &id001 !ruby/object:Gem::Requirement
|
24
24
|
requirements:
|
@@ -27,10 +27,10 @@ dependencies:
|
|
27
27
|
segments:
|
28
28
|
- 0
|
29
29
|
version: "0"
|
30
|
-
type: :
|
30
|
+
type: :development
|
31
31
|
version_requirements: *id001
|
32
32
|
- !ruby/object:Gem::Dependency
|
33
|
-
name:
|
33
|
+
name: redgreen
|
34
34
|
prerelease: false
|
35
35
|
requirement: &id002 !ruby/object:Gem::Requirement
|
36
36
|
requirements:
|
@@ -42,7 +42,7 @@ dependencies:
|
|
42
42
|
type: :development
|
43
43
|
version_requirements: *id002
|
44
44
|
- !ruby/object:Gem::Dependency
|
45
|
-
name:
|
45
|
+
name: mocha
|
46
46
|
prerelease: false
|
47
47
|
requirement: &id003 !ruby/object:Gem::Requirement
|
48
48
|
requirements:
|
@@ -61,29 +61,34 @@ extensions: []
|
|
61
61
|
|
62
62
|
extra_rdoc_files:
|
63
63
|
- LICENSE
|
64
|
-
- README.
|
64
|
+
- README.markdown
|
65
65
|
files:
|
66
66
|
- .document
|
67
67
|
- .gitignore
|
68
68
|
- LICENSE
|
69
|
-
- README.
|
69
|
+
- README.markdown
|
70
70
|
- Rakefile
|
71
71
|
- VERSION
|
72
72
|
- hotwire.gemspec
|
73
73
|
- lib/hotwire.rb
|
74
|
-
- lib/hotwire/
|
75
|
-
- lib/hotwire/
|
76
|
-
- lib/hotwire/
|
77
|
-
- lib/hotwire/
|
78
|
-
- lib/hotwire/
|
79
|
-
-
|
74
|
+
- lib/hotwire/base.rb
|
75
|
+
- lib/hotwire/request.rb
|
76
|
+
- lib/hotwire/response.rb
|
77
|
+
- lib/hotwire/response/base.rb
|
78
|
+
- lib/hotwire/response/csv.rb
|
79
|
+
- lib/hotwire/response/html.rb
|
80
|
+
- lib/hotwire/response/invalid.rb
|
81
|
+
- lib/hotwire/response/json.rb
|
80
82
|
- test/hotwire_test.rb
|
83
|
+
- test/response/test_base.rb
|
84
|
+
- test/response/test_csv.rb
|
85
|
+
- test/response/test_html.rb
|
86
|
+
- test/response/test_invalid.rb
|
87
|
+
- test/response/test_json.rb
|
81
88
|
- test/test_active_record_mixins.rb
|
82
|
-
- test/test_column_headers.rb
|
83
|
-
- test/test_core_extensions.rb
|
84
89
|
- test/test_helper.rb
|
85
|
-
- test/
|
86
|
-
- test/
|
90
|
+
- test/test_request.rb
|
91
|
+
- test/test_response.rb
|
87
92
|
has_rdoc: true
|
88
93
|
homepage: http://github.com/lesfreeman/hotwire
|
89
94
|
licenses: []
|
@@ -115,11 +120,13 @@ signing_key:
|
|
115
120
|
specification_version: 3
|
116
121
|
summary: Under the hood data transformations for the Google Wire protocol.
|
117
122
|
test_files:
|
118
|
-
- test/active_record_test_helper.rb
|
119
123
|
- test/hotwire_test.rb
|
124
|
+
- test/response/test_base.rb
|
125
|
+
- test/response/test_csv.rb
|
126
|
+
- test/response/test_html.rb
|
127
|
+
- test/response/test_invalid.rb
|
128
|
+
- test/response/test_json.rb
|
120
129
|
- test/test_active_record_mixins.rb
|
121
|
-
- test/test_column_headers.rb
|
122
|
-
- test/test_core_extensions.rb
|
123
130
|
- test/test_helper.rb
|
124
|
-
- test/
|
125
|
-
- test/
|
131
|
+
- test/test_request.rb
|
132
|
+
- test/test_response.rb
|
data/README.makrdown
DELETED
@@ -1,46 +0,0 @@
|
|
1
|
-
# Hotwire
|
2
|
-
|
3
|
-
Hotwire is designed to ease the pain of creating Google Wire Protocol compatible data source in Ruby. Specifically, Hotwire can make it dead simple to provide data for the Google Visualization API.
|
4
|
-
|
5
|
-
## Basic Usage
|
6
|
-
|
7
|
-
Create a data hash...
|
8
|
-
|
9
|
-
data = {:columns => ['name', 'years', 'location'], :rows => [["Bob", 21, "Baltimore"],
|
10
|
-
["Fred", 11, "Pittsburgh"]]}
|
11
|
-
|
12
|
-
...convert it...
|
13
|
-
|
14
|
-
data.to_wire
|
15
|
-
|
16
|
-
...and savor the results:
|
17
|
-
|
18
|
-
{"table"=>{"rows"=>[{"c"=>[{"v"=>"Bob"}, {"v"=>21}, {"v"=>"Baltimore"}]}, {"c"=>[{"v"=>"Fred"}, {"v"=>11}, {"v"=>"Pittsburgh"}]}], "cols"=>[{"type"=>"string", "id"=>"name", "label"=>"Name"}, {"type"=>"number", "id"=>"years", "label"=>"Years"}, {"type"=>"string", "id"=>"location", "label"=>"Location"}]}}
|
19
|
-
|
20
|
-
## Basic ActiveRecord Usage
|
21
|
-
If your project includes ActiveRecord, Hotwire will automatically included an extension to itself for processing ActiveRecord collections. This allows you to do something like:
|
22
|
-
|
23
|
-
people = Person.all
|
24
|
-
people.to_wire
|
25
|
-
|
26
|
-
Resulting in:
|
27
|
-
|
28
|
-
{"table"=>{"rows"=>[{"c"=>[{"v"=>"Bob"}, {"v"=>21}, {"v"=>"Baltimore"}]}, {"c"=>[{"v"=>"Fred"}, {"v"=>11}, {"v"=>"Pittsburgh"}]}], "cols"=>[{"type"=>"string", "id"=>"name", "label"=>"Name"}, {"type"=>"number", "id"=>"years", "label"=>"Years"}, {"type"=>"string", "id"=>"location", "label"=>"Location"}]}}
|
29
|
-
|
30
|
-
## Advanced Usage ##
|
31
|
-
|
32
|
-
Under the hood, Hotwire is designed to be extended in the future to allow for other construction methods and direct data manipulation. I haven't had a need for these features yet, but if you do and are interested in implementing them, please let me know.
|
33
|
-
|
34
|
-
## Note on Patches/Pull Requests
|
35
|
-
|
36
|
-
* Fork the project.
|
37
|
-
* Make your feature addition or bug fix.
|
38
|
-
* Add tests for it. This is important so I don't break it in a
|
39
|
-
future version unintentionally.
|
40
|
-
* Commit, do not mess with rakefile, version, or history.
|
41
|
-
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
42
|
-
* Send me a pull request. Bonus points for topic branches.
|
43
|
-
|
44
|
-
## Copyright
|
45
|
-
|
46
|
-
Copyright (c) 2010 Les Freeman. See LICENSE for details.
|
@@ -1,95 +0,0 @@
|
|
1
|
-
module Hotwire #:nodoc:
|
2
|
-
|
3
|
-
##
|
4
|
-
# Hotwire extensions to build from ActiveRecord collections
|
5
|
-
#
|
6
|
-
module ActiveRecordMixins
|
7
|
-
|
8
|
-
##
|
9
|
-
# Extensions to Hotwire::Table
|
10
|
-
#
|
11
|
-
module Table
|
12
|
-
private
|
13
|
-
def self.included base
|
14
|
-
base.class_eval do
|
15
|
-
alias_method_chain :initialize, :active_record_extension
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
##
|
20
|
-
# Parse source data if needed and then pass it along to main initializer
|
21
|
-
#
|
22
|
-
def initialize_with_active_record_extension source
|
23
|
-
source = source_from_active_record_collection(source) if source.is_a?(Array) and source.first.is_a?(ActiveRecord::Base)
|
24
|
-
initialize_without_active_record_extension source
|
25
|
-
end
|
26
|
-
|
27
|
-
##
|
28
|
-
# Parse an ActiveRecord collection into a hash for the initializer
|
29
|
-
#
|
30
|
-
def source_from_active_record_collection source
|
31
|
-
{ :columns => columns_from_active_record_collection(source), :rows => rows_from_active_record_collection(source) }
|
32
|
-
end
|
33
|
-
|
34
|
-
##
|
35
|
-
# Parse an ActiveRecord collection into a column headers array
|
36
|
-
#
|
37
|
-
def columns_from_active_record_collection source
|
38
|
-
source.first.attributes.keys.sort
|
39
|
-
end
|
40
|
-
|
41
|
-
##
|
42
|
-
# Parse an ActiveRecord collection into a rows array
|
43
|
-
#
|
44
|
-
def rows_from_active_record_collection source
|
45
|
-
source.map do |obj|
|
46
|
-
obj.attributes.sort_by{ |key, val| key }.map do |key, val|
|
47
|
-
val
|
48
|
-
end
|
49
|
-
end
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
##
|
54
|
-
# Extensions to Hotwire::Row
|
55
|
-
#
|
56
|
-
module Row
|
57
|
-
private
|
58
|
-
##
|
59
|
-
# Times pulled from an activerecord attribute hash are not converted to the zone automatically
|
60
|
-
#
|
61
|
-
def format_date_with_time_zone_conversion date
|
62
|
-
date = date.in_time_zone if Time.respond_to?(:zone) and Time.zone and date.respond_to?(:in_time_zone)
|
63
|
-
format_date_without_time_zone_conversion date
|
64
|
-
end
|
65
|
-
|
66
|
-
def self.included base
|
67
|
-
base.class_eval do
|
68
|
-
alias_method_chain :format_date, :time_zone_conversion
|
69
|
-
end
|
70
|
-
end
|
71
|
-
end
|
72
|
-
|
73
|
-
##
|
74
|
-
# Extensions to Hotwire::CoreExtensions
|
75
|
-
#
|
76
|
-
module CoreExtensions
|
77
|
-
|
78
|
-
module Array
|
79
|
-
##
|
80
|
-
# Converts an ActiveRecord collection array to a Wire compatible hash
|
81
|
-
#
|
82
|
-
def to_wire
|
83
|
-
raise "to_wire only works on an array of ActiveRecord objects" if self.map {|elem| elem.is_a?(ActiveRecord::Base)}.include?(false)
|
84
|
-
Hotwire::Table.new(self).to_wire
|
85
|
-
end
|
86
|
-
end
|
87
|
-
end
|
88
|
-
|
89
|
-
end
|
90
|
-
|
91
|
-
end
|
92
|
-
|
93
|
-
Hotwire::Table.send(:include, Hotwire::ActiveRecordMixins::Table)
|
94
|
-
Hotwire::Row.send(:include, Hotwire::ActiveRecordMixins::Row)
|
95
|
-
Array.send(:include, Hotwire::ActiveRecordMixins::CoreExtensions::Array)
|
@@ -1,54 +0,0 @@
|
|
1
|
-
require 'active_support'
|
2
|
-
|
3
|
-
module Hotwire #:nodoc:
|
4
|
-
|
5
|
-
##
|
6
|
-
# Represents header information for a Hotwire::Table
|
7
|
-
#
|
8
|
-
class ColumnHeaders
|
9
|
-
|
10
|
-
##
|
11
|
-
# Initialize from a hash of data. See Hotwire::Table.initialize for param information.
|
12
|
-
#
|
13
|
-
def initialize data
|
14
|
-
@data = data
|
15
|
-
end
|
16
|
-
|
17
|
-
##
|
18
|
-
# Convert to a Wire compatible Hash
|
19
|
-
#
|
20
|
-
def to_wire
|
21
|
-
returning [] do |columns|
|
22
|
-
@data[:columns].each_with_index do |column, index|
|
23
|
-
columns << {'id' => column, 'label' => column.humanize, 'type' => column_type_for_index(index)}
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
private
|
29
|
-
|
30
|
-
##
|
31
|
-
# Determine the column data type by index
|
32
|
-
#
|
33
|
-
def column_type_for_index index
|
34
|
-
column_type(@data[:rows].first[index])
|
35
|
-
end
|
36
|
-
|
37
|
-
##
|
38
|
-
# Determine the column data type for a given value
|
39
|
-
#
|
40
|
-
def column_type sample_value
|
41
|
-
case sample_value.class.name
|
42
|
-
when 'BigDecimal', 'Fixnum'
|
43
|
-
'number'
|
44
|
-
when /Time/
|
45
|
-
'datetime'
|
46
|
-
when 'String'
|
47
|
-
'string'
|
48
|
-
else
|
49
|
-
'string'
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
end
|
54
|
-
end
|
data/lib/hotwire/row.rb
DELETED
@@ -1,46 +0,0 @@
|
|
1
|
-
module Hotwire #:nodoc:
|
2
|
-
|
3
|
-
##
|
4
|
-
# Represents a one-dimensional row of data.
|
5
|
-
#
|
6
|
-
class Row
|
7
|
-
|
8
|
-
##
|
9
|
-
# Construct a new Row object from a array of data.
|
10
|
-
#
|
11
|
-
def initialize data
|
12
|
-
raise ArgumentError, "data must be an Array of values" unless data.is_a?(Array)
|
13
|
-
@data = data
|
14
|
-
end
|
15
|
-
|
16
|
-
##
|
17
|
-
# Convert the row to a Wire compatible Hash
|
18
|
-
#
|
19
|
-
def to_wire
|
20
|
-
{ 'c' => @data.map { |value| {'v' => format_value(value) } } }
|
21
|
-
end
|
22
|
-
|
23
|
-
private
|
24
|
-
|
25
|
-
##
|
26
|
-
# Perform any formatting needed to make value Wire compatible.
|
27
|
-
#
|
28
|
-
def format_value value
|
29
|
-
case value.class.name
|
30
|
-
when /Time/
|
31
|
-
format_date value
|
32
|
-
when /NilClass/
|
33
|
-
0
|
34
|
-
else
|
35
|
-
value
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
##
|
40
|
-
# Wire dates seem to expect to have 0 based month.
|
41
|
-
#
|
42
|
-
def format_date value
|
43
|
-
"Date(#{value.year}, #{value.month - 1}, #{value.day}, #{value.hour}, #{value.min}, #{value.sec})"
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|
data/lib/hotwire/table.rb
DELETED
@@ -1,27 +0,0 @@
|
|
1
|
-
module Hotwire #:nodoc:
|
2
|
-
|
3
|
-
##
|
4
|
-
# Represents a two dimensional table of data that is ready to format for the Wire protocol
|
5
|
-
#
|
6
|
-
class Table
|
7
|
-
##
|
8
|
-
# Create a table from a source hash. The hash must contain these keys:
|
9
|
-
# :columns => A one dimensional array of column labels.
|
10
|
-
# :rows => => A two dimensional array of data. Each row is a one dimensional array.
|
11
|
-
#
|
12
|
-
def initialize source
|
13
|
-
raise ArgumentError, "source data must have a :column key and a :rows key" unless source.has_key?(:columns) && source.has_key?(:rows)
|
14
|
-
@column_headers = Hotwire::ColumnHeaders.new(source)
|
15
|
-
@rows = source[:rows].map { |row| Hotwire::Row.new(row) }
|
16
|
-
end
|
17
|
-
|
18
|
-
##
|
19
|
-
# Convert the table to a Wire compatible hash
|
20
|
-
#
|
21
|
-
def to_wire
|
22
|
-
{'table' => {'cols' => @column_headers.to_wire, 'rows' => @rows.map {|r| r.to_wire} } }
|
23
|
-
end
|
24
|
-
|
25
|
-
end
|
26
|
-
|
27
|
-
end
|
@@ -1,14 +0,0 @@
|
|
1
|
-
require 'active_record'
|
2
|
-
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
|
3
|
-
|
4
|
-
ActiveRecord::Schema.define(:version => 1) do
|
5
|
-
create_table :people do |t|
|
6
|
-
t.string :name
|
7
|
-
t.text :bio
|
8
|
-
t.integer :age
|
9
|
-
t.datetime :birth_date
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
class Person < ActiveRecord::Base
|
14
|
-
end
|