openagent 0.9.2 → 0.9.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -5,6 +5,7 @@ require "openagent/xml_helpers"
5
5
  require "openagent/errors"
6
6
  require "openagent/message_builder"
7
7
  require "logger"
8
+ require "active_support/hash_with_indifferent_access"
8
9
 
9
10
  module OpenAgent
10
11
  class Client
@@ -28,23 +29,25 @@ module OpenAgent
28
29
 
29
30
  def initialize(opts={})
30
31
  @callbacks = {
31
- :receive_message => [],
32
- :each_loop => []
32
+ :receive_message => [],
33
+ :each_loop => []
33
34
  }
34
35
 
36
+ opts = ActiveSupport::HashWithIndifferentAccess.new(opts)
37
+
35
38
  @name = opts.delete(:name)
36
39
  @url = opts.delete(:url)
37
40
  @pretty_print = opts.has_key?(:pretty_print) ? opts.delete(:pretty_print) : true
38
41
 
39
- @agent_opts = opts['agent_opts'] || {}
42
+ @agent_opts = opts[:agent_opts] || {}
40
43
  @agent_opts[:name] = @name if @name
41
44
 
42
- @zone_opts = opts['zone_opts'] || {}
45
+ @zone_opts = opts[:zone_opts] || {}
43
46
  @zone_opts[:uri] = @url if @url
44
47
 
45
- @log = Logger.new(opts["log"] || STDOUT, 'daily')
46
- @agent = opts['agent'] || OpenAgent::Agent.new(@agent_opts)
47
- @zone = opts['zone'] || OpenAgent::Zone.new(@zone_opts)
48
+ @log = opts[:logger] || Logger.new(opts[:logfile] || STDOUT, 'daily')
49
+ @agent = opts[:agent] || OpenAgent::Agent.new(@agent_opts)
50
+ @zone = opts[:zone] || OpenAgent::Zone.new(@zone_opts)
48
51
 
49
52
  @builder = OpenAgent::MessageBuilder.new(@agent, @zone)
50
53
  end
@@ -83,49 +86,33 @@ module OpenAgent
83
86
  end
84
87
  end
85
88
 
86
- def run
87
- wait_short = 10
88
- wait_long = 30
89
- loop do
90
- begin
91
- trigger(:each_loop)
92
-
93
- wait_period = wait_long
94
- messages_in_queue = true
95
- while messages_in_queue
96
- get_message do |message, outgoing, xml|
97
- if inner = message.inner_message
98
- trigger(:receive_message, message, outgoing, xml)
99
-
100
- if inner.response
101
- case message.status_code
102
- when ZIS_SUCCESS then
103
- if inner.response.more_packets?
104
- wait_period = wait_short
105
- else
106
- wait_period = wait_long
107
- end
108
- when ZIS_NO_MESSAGES then
109
- if not inner.more_packets?
110
- wait_period = wait_long
111
- end
112
- end
113
- end
114
-
115
- # We send an Ack for both an Event and a Response
116
- if message.status_code == ZIS_SUCCESS
117
- ack(inner.source_id, inner.msg_id)
118
- end
119
- else
120
- if message.status_code == ZIS_NO_MESSAGES
121
- messages_in_queue = false
122
- end
123
- end
89
+ def run_once
90
+ trigger(:each_loop)
91
+ message = nil
92
+ begin
93
+ get_message do |message, outgoing, xml|
94
+ if inner = message.inner_message
95
+ trigger(:receive_message, message, outgoing, xml)
96
+
97
+ # We send an Ack for both an Event and a Response
98
+ if message.status_code == ZIS_SUCCESS
99
+ ack(inner.source_id, inner.msg_id)
124
100
  end
125
101
  end
102
+ end
103
+ end while message && message.status_code == ZIS_SUCCESS
104
+ end
105
+
106
+ def run(wait_period=30)
107
+ loop do
108
+ begin
109
+ run_once
126
110
  sleep wait_period
127
111
  rescue ResponseError => e
128
112
  @log.error e
113
+ rescue Net::OpenTimeout => e
114
+ @log.error e
115
+ sleep(5)
129
116
  end
130
117
  end
131
118
  end
@@ -151,7 +138,7 @@ module OpenAgent
151
138
  @zone.send_request(req).tap do |response|
152
139
  log "Response", formatted_xml(response.body, @pretty_print)
153
140
 
154
- if response.body.empty?
141
+ if response.body.nil? or response.body.empty?
155
142
  raise ResponseError, "Response is empty"
156
143
  else
157
144
  incoming = Message.new
@@ -1,3 +1,3 @@
1
1
  module OpenAgent
2
- VERSION = '0.9.2' unless defined?(OpenAgent::VERSION)
2
+ VERSION = '0.9.3' unless defined?(OpenAgent::VERSION)
3
3
  end
@@ -15,7 +15,7 @@ module SIF
15
15
  property :condition_group, :as => 'SIF_ConditionGroup',
16
16
  :class => SIF::Infra::Common::ConditionGroup,
17
17
  :decorator => ConditionGroup,
18
- :if => lambda { !condition_group.empty? }
18
+ :if => lambda { !(condition_group || []).empty? }
19
19
 
20
20
  property :example, :as => 'SIF_Example'
21
21
  end
data/spec/client_spec.rb CHANGED
@@ -1,6 +1,13 @@
1
1
  require_relative "spec_helper"
2
2
  require "openagent/client"
3
3
 
4
+ def stub_rr(request, response)
5
+ stub_request(:post, "http://localhost:8765/").
6
+ with(:body => request,
7
+ :headers => {'Accept'=>'*/*', 'Content-Type'=>'application/xml', 'User-Agent'=>'Ruby'}).
8
+ to_return(:status => 200, :body => response, :headers => {})
9
+ end
10
+
4
11
  describe OpenAgent::Client do
5
12
 
6
13
  context "agent options" do
@@ -8,7 +15,16 @@ describe OpenAgent::Client do
8
15
  let(:zone) { OpenAgent::Zone.new(YAML::load(File.read(fixture('zone.yaml')))) }
9
16
  end
10
17
 
11
- subject(:client) { OpenAgent::Client.new( :name => "canvas", :url => "http://localhost:8765" ) }
18
+ subject(:client) {
19
+ OpenAgent::Client.new(
20
+ :name => "canvas",
21
+ :url => "http://localhost:8765",
22
+ :logger => Logger.new(EmptyLogger.new)
23
+ ).tap do |client|
24
+ client.builder.guuid = "MSG_ID"
25
+ client.builder.timestamp = "TIMESTAMP"
26
+ end
27
+ }
12
28
 
13
29
  it "initializes" do
14
30
  client.name.should == "canvas"
@@ -29,6 +45,26 @@ describe OpenAgent::Client do
29
45
  end
30
46
  end
31
47
 
48
+ context "run loop" do
49
+ it "no message exits loop" do
50
+ client.stub(:get_message) { nil }
51
+ client.run_once
52
+ end
53
+
54
+ it "ZIS_NO_MESSAGES exits loop" do
55
+ stub_rr(File.read(fixture("messages/get_message.xml")),
56
+ File.read(fixture("messages/response.xml")))
57
+ stub_rr(File.read(fixture("messages/get_message.xml")),
58
+ File.read(fixture("messages/response_done.xml")))
59
+ client.run_once
60
+ end
61
+
62
+ it "empty body raises ResponseError" do
63
+ stub_rr(File.read(fixture("messages/get_message.xml")), "")
64
+ lambda { client.run_once }.should raise_error(OpenAgent::ResponseError)
65
+ end
66
+ end
67
+
32
68
  context "given a response message" do
33
69
  let(:message) do
34
70
  SIF::Infra::Common::Message.new(
@@ -0,0 +1,12 @@
1
+ <SIF_Message xmlns="http://www.sifinfo.org/infrastructure/2.x" Version="2.0r1">
2
+ <SIF_SystemControl>
3
+ <SIF_Header>
4
+ <SIF_MsgId>MSG_ID</SIF_MsgId>
5
+ <SIF_Timestamp>TIMESTAMP</SIF_Timestamp>
6
+ <SIF_SourceId>canvas</SIF_SourceId>
7
+ </SIF_Header>
8
+ <SIF_SystemControlData>
9
+ <SIF_GetMessage/>
10
+ </SIF_SystemControlData>
11
+ </SIF_SystemControl>
12
+ </SIF_Message>
@@ -28,7 +28,7 @@
28
28
  </SIF_Header>
29
29
  <SIF_RequestMsgId>6B05EF70132C01319D9348E0EB1826A1</SIF_RequestMsgId>
30
30
  <SIF_PacketNumber>209</SIF_PacketNumber>
31
- <SIF_MorePackets>Yes</SIF_MorePackets>
31
+ <SIF_MorePackets>No</SIF_MorePackets>
32
32
  <SIF_ObjectData>
33
33
  <StudentPersonal RefId="140A051A0D2B360A1E1F0E251FBA175A">
34
34
  <LocalId>24939</LocalId>
@@ -0,0 +1,16 @@
1
+ <?xml version="1.0" encoding="ISO-8859-1"?>
2
+
3
+ <SIF_Message xmlns="http://www.sifinfo.org/infrastructure/2.x" Version="2.0r1">
4
+ <SIF_Ack>
5
+ <SIF_Header>
6
+ <SIF_MsgId>D72C57D4EE0D4E0AB68BD2393B12E51D</SIF_MsgId>
7
+ <SIF_Timestamp>2013-10-09T10:42:16-06:00</SIF_Timestamp>
8
+ <SIF_SourceId>canvas</SIF_SourceId>
9
+ </SIF_Header>
10
+ <SIF_OriginalSourceId>canvas</SIF_OriginalSourceId>
11
+ <SIF_OriginalMsgId>088E6810133001313D9314109FD501C1</SIF_OriginalMsgId>
12
+ <SIF_Status>
13
+ <SIF_Code>9</SIF_Code>
14
+ </SIF_Status>
15
+ </SIF_Ack>
16
+ </SIF_Message>
data/spec/spec_helper.rb CHANGED
@@ -1,7 +1,15 @@
1
1
  require "webmock/rspec"
2
+ require "logger"
3
+ require "stringio"
2
4
 
3
5
  $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
4
6
 
5
7
  def fixture(file)
6
8
  File.join(File.dirname(__FILE__), 'fixtures', file)
7
- end
9
+ end
10
+
11
+ class EmptyLogger < StringIO
12
+ def write(input)
13
+ # do nothing
14
+ end
15
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: openagent
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.2
4
+ version: 0.9.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2014-02-10 00:00:00.000000000 Z
14
+ date: 2014-02-13 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: rake
@@ -379,7 +379,9 @@ files:
379
379
  - spec/event_spec.rb
380
380
  - spec/fixtures/agent.yaml
381
381
  - spec/fixtures/messages/event.xml
382
+ - spec/fixtures/messages/get_message.xml
382
383
  - spec/fixtures/messages/response.xml
384
+ - spec/fixtures/messages/response_done.xml
383
385
  - spec/fixtures/sif/attendance_code_info.xml
384
386
  - spec/fixtures/sif/calendar_date.xml
385
387
  - spec/fixtures/sif/calendar_summary.xml
@@ -461,7 +463,9 @@ test_files:
461
463
  - spec/event_spec.rb
462
464
  - spec/fixtures/agent.yaml
463
465
  - spec/fixtures/messages/event.xml
466
+ - spec/fixtures/messages/get_message.xml
464
467
  - spec/fixtures/messages/response.xml
468
+ - spec/fixtures/messages/response_done.xml
465
469
  - spec/fixtures/sif/attendance_code_info.xml
466
470
  - spec/fixtures/sif/calendar_date.xml
467
471
  - spec/fixtures/sif/calendar_summary.xml