Нет описания http://j1x-huginn.herokuapp.com

website_agent_spec.rb 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. require 'spec_helper'
  2. describe Agents::WebsiteAgent do
  3. before do
  4. stub_request(:any, /xkcd/).to_return(:body => File.read(Rails.root.join("spec/data_fixtures/xkcd.html")), :status => 200)
  5. @site = {
  6. :name => "XKCD",
  7. :expected_update_period_in_days => 2,
  8. :type => "html",
  9. :url => "http://xkcd.com",
  10. :mode => :on_change,
  11. :extract => {
  12. :url => {:css => "#comic img", :attr => "src"},
  13. :title => {:css => "#comic img", :attr => "title"}
  14. }
  15. }
  16. @checker = Agents::WebsiteAgent.new(:name => "xkcd", :options => @site)
  17. @checker.user = users(:bob)
  18. @checker.save!
  19. end
  20. describe "#check" do
  21. it "should check for changes" do
  22. lambda { @checker.check }.should change { Event.count }.by(1)
  23. lambda { @checker.check }.should_not change { Event.count }
  24. end
  25. it "should always save events when in :all mode" do
  26. lambda {
  27. @site[:mode] = :all
  28. @checker.options = @site
  29. @checker.check
  30. @checker.check
  31. }.should change { Event.count }.by(2)
  32. end
  33. it "should log an error if the number of results for a set of extraction patterns differs" do
  34. lambda {
  35. @site[:extract][:url][:css] = "div"
  36. @checker.options = @site
  37. @checker.check
  38. }.should raise_error(StandardError, /Got an uneven number of matches/)
  39. end
  40. end
  41. describe "parsing" do
  42. it "parses CSS" do
  43. @checker.check
  44. event = Event.last
  45. event.payload[:url].should == "http://imgs.xkcd.com/comics/evolving.png"
  46. event.payload[:title].should =~ /^Biologists play reverse/
  47. end
  48. describe "JSON" do
  49. it "works with paths" do
  50. json = {
  51. :response => {
  52. :version => 2,
  53. :title => "hello!"
  54. }
  55. }
  56. stub_request(:any, /json-site/).to_return(:body => json.to_json, :status => 200)
  57. site = {
  58. :name => "Some JSON Response",
  59. :expected_update_period_in_days => 2,
  60. :type => "json",
  61. :url => "http://json-site.com",
  62. :mode => :on_change,
  63. :extract => {
  64. :version => { :path => "response.version" },
  65. :title => { :path => "response.title" }
  66. }
  67. }
  68. checker = Agents::WebsiteAgent.new(:name => "Weather Site", :options => site)
  69. checker.user = users(:bob)
  70. checker.save!
  71. checker.check
  72. event = Event.last
  73. event.payload[:version].should == 2
  74. event.payload[:title].should == "hello!"
  75. end
  76. it "can handle arrays" do
  77. json = {
  78. :response => {
  79. :data => [
  80. { :title => "first", :version => 2 },
  81. { :title => "second", :version => 2.5 }
  82. ]
  83. }
  84. }
  85. stub_request(:any, /json-site/).to_return(:body => json.to_json, :status => 200)
  86. site = {
  87. :name => "Some JSON Response",
  88. :expected_update_period_in_days => 2,
  89. :type => "json",
  90. :url => "http://json-site.com",
  91. :mode => :on_change,
  92. :extract => {
  93. :title => { :path => "response.data[*].title" },
  94. :version => { :path => "response.data[*].version" }
  95. }
  96. }
  97. checker = Agents::WebsiteAgent.new(:name => "Weather Site", :options => site)
  98. checker.user = users(:bob)
  99. checker.save!
  100. lambda {
  101. checker.check
  102. }.should change { Event.count }.by(2)
  103. event = Event.all[-1]
  104. event.payload[:version].should == 2.5
  105. event.payload[:title].should == "second"
  106. event = Event.all[-2]
  107. event.payload[:version].should == 2
  108. event.payload[:title].should == "first"
  109. end
  110. end
  111. end
  112. end