benschwarz-smoke 0.5.10 → 0.5.13

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -4,7 +4,7 @@ smoke is a Ruby based DSL that allows you to query web services such as YQL, RSS
4
4
 
5
5
  These "services" can then be re-represented, sorted and filtered. Data can be collected from multiple sources, sorted via a common property, chopped up and refried.
6
6
 
7
- Then you can output as a plain ruby object (or JSON)
7
+ Then you can output as a plain ruby object or one of your other favourites (JSON, YAML, XML)
8
8
 
9
9
  ## Examples of use
10
10
 
data/Rakefile CHANGED
@@ -12,11 +12,12 @@ begin
12
12
  gem.authors = ["Ben Schwarz"]
13
13
  gem.files = FileList['lib/**/*.rb', 'rdoc/**/*', '[A-Z]*', 'spec/**/*', 'vendor/**/*'].to_a
14
14
  gem.add_dependency("simple-rss", "1.2")
15
- gem.add_dependency("json", "1.1.3")
15
+ gem.add_dependency("json", ">= 1.1.3")
16
16
  gem.add_dependency("fakeweb", "1.2.5")
17
- gem.add_dependency("crack", "0.1.1")
17
+ gem.add_dependency("crack", ">= 0.1.1")
18
18
  gem.add_dependency("moneta", "0.6.0")
19
19
  gem.add_dependency("rest-client", "1.0.3")
20
+ gem.add_dependency("nokogiri", "1.3.2")
20
21
  # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
21
22
  end
22
23
  rescue LoadError
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
- :patch: 10
2
+ :patch: 13
3
3
  :major: 0
4
4
  :minor: 5
data/lib/smoke.rb CHANGED
@@ -6,6 +6,7 @@ require 'json'
6
6
  require 'crack'
7
7
  require 'moneta'
8
8
  require 'restclient'
9
+ require 'nokogiri'
9
10
 
10
11
  module Smoke
11
12
  class << self
@@ -103,7 +104,7 @@ module Smoke
103
104
  end
104
105
  end
105
106
 
106
- %w(core_ext/hash core_ext/string smoke/cache smoke/request smoke/origin).each {|r| require File.join(File.dirname(__FILE__), r)}
107
+ %w(core_ext/hash core_ext/string smoke/cache smoke/request smoke/origin smoke/output/xml).each {|r| require File.join(File.dirname(__FILE__), r)}
107
108
 
108
109
  class Object # :nodoc:
109
110
  include Smoke
data/lib/smoke/cache.rb CHANGED
@@ -17,6 +17,11 @@ module Smoke
17
17
  def enabled?
18
18
  Smoke.config[:cache][:enabled]
19
19
  end
20
+
21
+ # Clear all your request caches
22
+ def clear!
23
+ cache.clear
24
+ end
20
25
 
21
26
  protected
22
27
  def cache
data/lib/smoke/origin.rb CHANGED
@@ -28,9 +28,11 @@ module Smoke
28
28
  return ::JSON.generate(@items)
29
29
  when :yaml
30
30
  return YAML.dump(@items)
31
+ when :xml
32
+ return Smoke::Output::XML.generate(@name, @items)
31
33
  else
32
34
  return @items
33
- end
35
+ end
34
36
  end
35
37
 
36
38
  def items=(response) # :nodoc:
@@ -225,7 +227,7 @@ module Smoke
225
227
 
226
228
  private
227
229
  def prepare!
228
- @prepare.each{|p| p.call } unless @prepare.nil?
230
+ @prepare.each{|block| block.call} unless @prepare.empty?
229
231
  end
230
232
 
231
233
  def transform!
@@ -0,0 +1,23 @@
1
+ module Smoke
2
+ module Output
3
+ class XML
4
+ def self.generate(tree_name, items)
5
+ builder = Nokogiri::XML::Builder.new do |xml|
6
+ xml.send(tree_name) {
7
+ xml.items {
8
+ items.each do |item|
9
+ xml.item {
10
+ item.each_pair do |key, value|
11
+ xml.send(key, value)
12
+ end
13
+ }
14
+ end
15
+ }
16
+ }
17
+ end
18
+
19
+ builder.to_xml
20
+ end
21
+ end
22
+ end
23
+ end
@@ -14,6 +14,28 @@ module Smoke
14
14
  super((names << "joined").join("_").to_sym, &block)
15
15
  end
16
16
 
17
+ # Rename sources immediately after they've been joined together
18
+ # Usage:
19
+ # Smoke.join(:delicious, :twitter, :flickr) do
20
+ # name :web_stream
21
+ # end
22
+ def name(rename = nil)
23
+ return @name if rename.nil?
24
+ Smoke.rename(@name => rename)
25
+ end
26
+
27
+ def method_missing(symbol, *args, &block)
28
+ ivar = "@#{symbol}"
29
+
30
+ unless args.empty?
31
+ sources.each do |source|
32
+ source.last.instance_variable_set(ivar, args.last)
33
+ end
34
+ end
35
+
36
+ return self
37
+ end
38
+
17
39
  protected
18
40
  def sources
19
41
  Smoke.active_sources.find_all{|k, v| @names.include?(k) }
@@ -22,6 +44,7 @@ module Smoke
22
44
  def dispatch
23
45
  # Recall dispatch
24
46
  sources.each do |source|
47
+ source.last.send(:prepare!)
25
48
  source.last.send(:dispatch) if source.last.respond_to?(:dispatch)
26
49
  end
27
50
 
@@ -6,9 +6,13 @@ describe Smoke::Cache do
6
6
  Smoke::Cache.should respond_to(:fetch)
7
7
  end
8
8
 
9
- it "should responsd to enabled?" do
9
+ it "should respond to enabled?" do
10
10
  Smoke::Cache.should respond_to(:enabled?)
11
11
  end
12
+
13
+ it "should respond to clear!" do
14
+ Smoke::Cache.should respond_to(:clear!)
15
+ end
12
16
  end
13
17
 
14
18
  describe "configuration" do
@@ -41,7 +45,7 @@ describe Smoke::Cache do
41
45
  end
42
46
  end
43
47
 
44
- describe "caching my block" do
48
+ describe "caching my request" do
45
49
  before :all do
46
50
  Smoke.configure do |c|
47
51
  c[:cache][:enabled] = true
@@ -71,5 +75,10 @@ describe Smoke::Cache do
71
75
  Smoke::Cache.fetch @url, {}
72
76
  @store['33af9f13054e64520430f7a437cdd377'].should_not be_nil
73
77
  end
78
+
79
+ it "should be cleared" do
80
+ Smoke::Cache.clear!
81
+ @store['33af9f13054e64520430f7a437cdd377'].should be_nil
82
+ end
74
83
  end
75
84
  end
@@ -0,0 +1,24 @@
1
+ require File.join(File.dirname(__FILE__), "..", "..", "spec_helper.rb")
2
+
3
+ describe Smoke::Output::XML do
4
+ before do
5
+ @tree = "tree"
6
+ @items = [{:animal => "monkey"}]
7
+ @xml = Smoke::Output::XML.generate(@tree, @items)
8
+ @document = Nokogiri::XML(@xml)
9
+ end
10
+
11
+ it "should respond to generate" do
12
+ Smoke::Output::XML.should respond_to(:generate)
13
+ end
14
+
15
+ it "should start the tree off with a named key" do
16
+ @document.css("tree").should_not be_nil
17
+ end
18
+
19
+ it "should contain items" do
20
+ @document.css("items").each do |item|
21
+ item.content.should =~ /monkey/
22
+ end
23
+ end
24
+ end
@@ -11,15 +11,14 @@ describe "Join" do
11
11
  before do
12
12
  @source = Smoke.join(:a, :b)
13
13
  end
14
-
15
- # it_should_behave_like "all sources"
16
-
14
+
17
15
  it "should be named in a_b_joined" do
18
16
  Smoke[:a_b_joined].should be_an_instance_of(Smoke::Source::Join)
19
17
  end
20
18
 
21
19
  it "should contain items from sources a and b" do
22
20
  Smoke[:a_b_joined].output.size.should == (@source_a.output.size + @source_b.output.size)
21
+ Smoke[:a_b_joined].output.first.should == @source_a.output.first
23
22
  end
24
23
 
25
24
  it "should accept a block" do
@@ -34,6 +33,20 @@ describe "Join" do
34
33
  Smoke[:a_b_joined].should respond_to(:output)
35
34
  end
36
35
 
36
+ describe "renaming, within a block" do
37
+ it "should respond to name" do
38
+ Smoke[:a_b_joined].should respond_to(:name)
39
+ end
40
+
41
+ it "should rename the source" do
42
+ Smoke.join(:b, :c) do
43
+ name :bee_and_cee
44
+ end
45
+
46
+ Smoke[:bee_and_cee].should_not be_nil
47
+ end
48
+ end
49
+
37
50
  describe "dispatching" do
38
51
  before :all do
39
52
  FakeWeb.register_uri("http://photos.tld", :response => File.join(SPEC_DIR, 'supports', 'flickr-photo.json'))
@@ -48,6 +61,36 @@ describe "Join" do
48
61
  Smoke[:should_dispatch].should_receive(:dispatch)
49
62
  Smoke.join(:a, :should_dispatch).output
50
63
  end
64
+
65
+ it "should call prepare blocks before dispatching" do
66
+ Smoke.data(:prepare_dispatch) do
67
+ prepare do
68
+ url "http://photos.tld"
69
+ end
70
+
71
+ path :photos, :photo
72
+ end
73
+
74
+ Smoke.join(:prepare_dispatch) do
75
+ name :always_be_prepared
76
+ end
77
+
78
+ lambda { Smoke.always_be_prepared.output }.should_not raise_error(ArgumentError)
79
+ end
80
+ end
81
+
82
+ describe "variable injection" do
83
+ before :all do
84
+ TestSource.source :variable
85
+ TestSource.source :injection
86
+ Smoke.join(:variable, :injection)
87
+ end
88
+
89
+ it "should inject variables to its source items" do
90
+ Smoke.variable_injection_joined.some_ivar("value")
91
+ Smoke.variable.some_ivar.should == "value"
92
+ Smoke.injection.some_ivar.should == "value"
93
+ end
51
94
  end
52
95
  end
53
96
  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.5.10
4
+ version: 0.5.13
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-09-12 00:00:00 -07:00
12
+ date: 2009-09-27 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -28,7 +28,7 @@ dependencies:
28
28
  version_requirement:
29
29
  version_requirements: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "="
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: 1.1.3
34
34
  version:
@@ -48,7 +48,7 @@ dependencies:
48
48
  version_requirement:
49
49
  version_requirements: !ruby/object:Gem::Requirement
50
50
  requirements:
51
- - - "="
51
+ - - ">="
52
52
  - !ruby/object:Gem::Version
53
53
  version: 0.1.1
54
54
  version:
@@ -72,6 +72,16 @@ dependencies:
72
72
  - !ruby/object:Gem::Version
73
73
  version: 1.0.3
74
74
  version:
75
+ - !ruby/object:Gem::Dependency
76
+ name: nokogiri
77
+ type: :runtime
78
+ version_requirement:
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - "="
82
+ - !ruby/object:Gem::Version
83
+ version: 1.3.2
84
+ version:
75
85
  description:
76
86
  email: ben.schwarz@gmail.com
77
87
  executables: []
@@ -91,6 +101,7 @@ files:
91
101
  - lib/smoke.rb
92
102
  - lib/smoke/cache.rb
93
103
  - lib/smoke/origin.rb
104
+ - lib/smoke/output/xml.rb
94
105
  - lib/smoke/request.rb
95
106
  - lib/smoke/source/data.rb
96
107
  - lib/smoke/source/feed.rb
@@ -119,6 +130,7 @@ files:
119
130
  - spec/core_ext/hash_spec.rb
120
131
  - spec/smoke/cache_spec.rb
121
132
  - spec/smoke/origin_spec.rb
133
+ - spec/smoke/output/xml_spec.rb
122
134
  - spec/smoke/request_spec.rb
123
135
  - spec/smoke/shared_spec.rb
124
136
  - spec/smoke/source/data_spec.rb
@@ -138,6 +150,7 @@ files:
138
150
  - spec/supports/test_source.rb
139
151
  has_rdoc: false
140
152
  homepage: http://github.com/benschwarz/smoke
153
+ licenses:
141
154
  post_install_message:
142
155
  rdoc_options:
143
156
  - --charset=UTF-8
@@ -158,7 +171,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
158
171
  requirements: []
159
172
 
160
173
  rubyforge_project:
161
- rubygems_version: 1.2.0
174
+ rubygems_version: 1.3.5
162
175
  signing_key:
163
176
  specification_version: 3
164
177
  summary: smoke is a Ruby based DSL that allows you to query web services such as YQL, RSS / Atom and JSON or XML in an elegant manner.
@@ -166,6 +179,7 @@ test_files:
166
179
  - spec/core_ext/hash_spec.rb
167
180
  - spec/smoke/cache_spec.rb
168
181
  - spec/smoke/origin_spec.rb
182
+ - spec/smoke/output/xml_spec.rb
169
183
  - spec/smoke/request_spec.rb
170
184
  - spec/smoke/shared_spec.rb
171
185
  - spec/smoke/source/data_spec.rb