benschwarz-smoke 0.3.7 → 0.3.9

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -83,6 +83,14 @@ Integrity [is running for smoke](http://integrity.ffolio.net/smoke)
83
83
  #### For wiki pages (docs, later)
84
84
  * How to use `path`
85
85
  * YQL Definitions
86
+ * Tranformations
87
+ * Insert
88
+ * Joining
89
+ * Variable injection
90
+ * Sort, Reverse
91
+ * Keep, Discard
92
+ * Truncate
93
+
86
94
 
87
95
  ### Copyright
88
96
 
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
- :patch: 7
2
+ :patch: 9
3
3
  :major: 0
4
4
  :minor: 3
data/lib/smoke/origin.rb CHANGED
@@ -71,27 +71,50 @@ module Smoke
71
71
  # rename(:href => :link)
72
72
  # end
73
73
  def emit(&block)
74
+ raise ArgumentError, "requires a block" unless block_given?
74
75
  @transformation << block
75
76
  end
76
77
 
77
78
  # Transform must be used inside an `emit` block.
78
- # It can be used to alter the a single field at a time
79
+ # It can be used to alter named keys within the item set
79
80
  #
80
81
  # Usage:
81
82
  # emit do
82
- # transform :name do |name|
83
+ # transform :name, :description do |name|
83
84
  # name.gsub(/\302/, "")
84
85
  # end
85
86
  # end
86
87
  #
87
88
  # In quasi-english: The result of the block is returned and set to each
88
- # of the :name attributes in the result set.
89
- def transform(name, &block)
90
- items.each do |item|
91
- item[name] = yield(item[name]) || item[name]
89
+ # of the :name and :description keys in the result set.
90
+ def transform(*keys)
91
+ raise ArgumentError, "requires a block" unless block_given?
92
+ keys.each do |key|
93
+ items.each do |item|
94
+ item[key] = yield(item[key]) || item[key]
95
+ end
92
96
  end
93
97
  end
94
98
 
99
+ # Insert must be used inside an `emit` block.
100
+ # It can be used to insert named keys to all items within the result set
101
+ #
102
+ # Usage:
103
+ #
104
+ # emit do
105
+ # insert :source, "twitter"
106
+ # end
107
+ #
108
+ # Once output is called, all items will contain a key of :source with
109
+ # a value of "twitter"
110
+ def insert(key, value)
111
+ @items.each do |item|
112
+ item[key] = value
113
+ end
114
+
115
+ return self
116
+ end
117
+
95
118
  # Prepare is used when you'd like to provision for
96
119
  # arguments / variables to be set after the source definition.
97
120
  # Eg, create a source definition for twitter, omitting the "username".
data/lib/smoke.rb CHANGED
@@ -5,11 +5,6 @@ require 'crack'
5
5
  require 'simple-rss'
6
6
  require 'json'
7
7
 
8
- $:<< File.join(File.dirname(__FILE__))
9
-
10
- # Core ext
11
- require 'core_ext/hash.rb'
12
-
13
8
  module Smoke
14
9
  class << self
15
10
  @@active_sources = {}
@@ -96,8 +91,7 @@ module Smoke
96
91
  end
97
92
  end
98
93
 
99
- require 'smoke/request'
100
- require 'smoke/origin'
94
+ %w(core_ext/hash smoke/request smoke/origin).each {|r| require File.join(File.dirname(__FILE__), r)}
101
95
 
102
96
  class Object # :nodoc:
103
97
  include Smoke
@@ -7,7 +7,7 @@ describe Smoke::Origin do
7
7
  rename(:head => :title)
8
8
  sort(:title)
9
9
 
10
- transform :title do |title|
10
+ transform :title, :name do |title|
11
11
  title.gsub(/Animal: /, '')
12
12
  end
13
13
  end
@@ -24,7 +24,7 @@ describe Smoke::Origin do
24
24
  end
25
25
 
26
26
  it "should output a single hash rather than a hash in an array when there is one item" do
27
- Smoke[:test].truncate(1).output.should == {:title => "Kangaroo"}
27
+ Smoke[:test].truncate(1).output.should == {:title => "Kangaroo", :name => "Kelly"}
28
28
  end
29
29
  end
30
30
 
@@ -38,11 +38,11 @@ describe Smoke::Origin do
38
38
  end
39
39
 
40
40
  it "should reverse the results" do
41
- Smoke[:test].sort(:header).reverse.output.should == [{:header => "Platypus"}, {:header => "Kangaroo"}]
41
+ Smoke[:test].sort(:header).reverse.output.should == [{:header => "Platypus", :name => "Peter"}, {:header => "Kangaroo", :name => "Kelly"}]
42
42
  end
43
43
 
44
44
  it "should truncate results given a length" do
45
- Smoke[:test].truncate(1).output.size.should == 1
45
+ Smoke[:test].truncate(1).output.should be_an_instance_of(Hash)
46
46
  end
47
47
 
48
48
  describe "filtering" do
@@ -69,7 +69,7 @@ describe Smoke::Origin do
69
69
  end
70
70
 
71
71
  it "should only contain items that match" do
72
- Smoke[:keep].keep(:head, /^K/).output.should == {:head => "Kangaroo"}
72
+ Smoke[:keep].keep(:head, /^K/).output.should == {:head => "Kangaroo", :name => "Kelly"}
73
73
  end
74
74
 
75
75
  it "should discard items" do
@@ -77,7 +77,7 @@ describe Smoke::Origin do
77
77
  end
78
78
 
79
79
  it "should not contain items that match" do
80
- Smoke[:discard].discard(:head, /^K/).output.should == {:head => "Platypus"}
80
+ Smoke[:discard].discard(:head, /^K/).output.should == {:head => "Platypus", :name => "Peter"}
81
81
  end
82
82
  end
83
83
  end
@@ -96,7 +96,7 @@ describe Smoke::Origin do
96
96
  end
97
97
 
98
98
  it "should output yml" do
99
- @origin.output(:yaml).should =~ /--- \n- :title:/
99
+ @origin.output(:yaml).should =~ /^--- \n- :/
100
100
  end
101
101
 
102
102
  it "should dispatch when output is called" do
@@ -117,7 +117,7 @@ describe Smoke::Origin do
117
117
  end
118
118
  end
119
119
  end
120
- Smoke[:chain].rename(:head => :header).sort(:header).output.should == [{:header => "Kangaroo"}, {:header => "Platypus"}]
120
+ Smoke[:chain].rename(:head => :header).sort(:header).output.should == [{:header => "Kangaroo", :name => "Kelly"}, {:header => "Platypus", :name => "Peter"}]
121
121
  end
122
122
 
123
123
  it "should softly error when attempting to sort on a key that doesn't exist" do
@@ -190,12 +190,33 @@ describe Smoke::Origin do
190
190
  Smoke[:test].should respond_to(:emit)
191
191
  end
192
192
 
193
+ it "emit should require a block" do
194
+ lambda { Smoke[:test].emit }.should raise_error
195
+ lambda { Smoke[:test].emit {} }.should_not raise_error
196
+ end
197
+
193
198
  it "should respond to transform" do
194
199
  Smoke[:test].should respond_to(:transform)
195
200
  end
196
201
 
202
+ it "tranform should require a block" do
203
+ lambda { Smoke[:test].transform }.should raise_error
204
+ lambda { Smoke[:test].transform {} }.should_not raise_error
205
+ end
206
+
197
207
  it "should have at least one transformation" do
198
- Smoke[:test].transformation.size.should == 1
208
+ Smoke[:test].transformation.size.should_not be_nil
209
+ end
210
+ end
211
+
212
+ describe "key insertion" do
213
+ it "should respond to insert" do
214
+ Smoke[:test].should respond_to(:insert)
215
+ end
216
+
217
+ it "should insert values into each key" do
218
+ Smoke[:test].insert(:source, "twitter").output.first.should have_key :source
219
+ Smoke[:test].insert(:source, "twitter").output.first[:source].should == "twitter"
199
220
  end
200
221
  end
201
222
  end
data/spec/spec_helper.rb CHANGED
@@ -1,17 +1,12 @@
1
1
  require 'rubygems'
2
- gem 'rspec'
3
-
4
2
  require 'spec'
5
3
  require 'fake_web'
6
4
 
7
- $:<< File.join(File.dirname(__FILE__), '..', 'lib')
8
-
5
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'smoke')
9
6
 
10
7
  SPEC_DIR = File.dirname(__FILE__) unless defined? SPEC_DIR
11
8
  $:<< SPEC_DIR
12
9
 
13
- require 'smoke'
14
-
15
10
  require 'supports/mayo.rb'
16
11
  require 'supports/test_source.rb'
17
12
 
@@ -1,9 +1,9 @@
1
1
  module TestSource
2
2
  def self.source(name, &block)
3
- source = Smoke::Origin.new(name, &block || Proc.new {})
3
+ source = Smoke::Origin.new(name, &block)
4
4
  source.items = [
5
- {:head => "Animal: Platypus"},
6
- {:head => "Animal: Kangaroo"}
5
+ {:head => "Animal: Platypus", :name => "Peter"},
6
+ {:head => "Animal: Kangaroo", :name => "Kelly"}
7
7
  ]
8
8
  return source
9
9
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: benschwarz-smoke
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.7
4
+ version: 0.3.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Schwarz
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-06-15 00:00:00 -07:00
12
+ date: 2009-06-18 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -100,7 +100,7 @@ requirements: []
100
100
  rubyforge_project:
101
101
  rubygems_version: 1.2.0
102
102
  signing_key:
103
- specification_version: 2
103
+ specification_version: 3
104
104
  summary: smoke is a DSL that allows you to take data from YQL, RSS / Atom
105
105
  test_files: []
106
106