Class: NchanTools::Subscriber
- Inherits:
-
Object
- Object
- NchanTools::Subscriber
show all
- Defined in:
- lib/nchan_tools/pubsub.rb
Defined Under Namespace
Classes: Client, EventSourceClient, HTTPChunkedClient, IntervalPollClient, Logger, LongPollClient, MultiparMixedClient, SubscriberError, WebSocketClient
Instance Attribute Summary collapse
Instance Method Summary
collapse
-
#abort ⇒ Object
-
#errors? ⇒ Boolean
-
#initialize(url, concurrency = 1, opt = {}) ⇒ Subscriber
constructor
A new instance of Subscriber.
-
#make_error(client, what, code, msg, failword = " failed") ⇒ Object
-
#match_errors(regex) ⇒ Object
-
#new_client ⇒ Object
-
#no_errors? ⇒ Boolean
-
#on(evt_name = nil, &block) ⇒ Object
-
#on_failure(err = nil, nostore = false, &block) ⇒ Object
-
#on_message(msg = nil, bundle = nil, &block) ⇒ Object
-
#reset ⇒ Object
-
#run ⇒ Object
-
#stop ⇒ Object
-
#terminate ⇒ Object
-
#terminated? ⇒ Boolean
-
#wait(until_what = nil, timeout = nil) ⇒ Object
Constructor Details
#initialize(url, concurrency = 1, opt = {}) ⇒ Subscriber
Returns a new instance of Subscriber.
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
|
# File 'lib/nchan_tools/pubsub.rb', line 1522
def initialize(url, concurrency=1, opt={})
@empty_block = Proc.new {}
@on={}
@care_about_message_ids=opt[:use_message_id].nil? ? true : opt[:use_message_id]
@url=url
@quit_message = opt[:quit_message]
opt[:timeout] ||= 30
opt[:connect_timeout] ||= 5
@Client_Class = Client.lookup(opt[:client] || :longpoll)
if @Client_Class.nil?
raise SubscriberError, "unknown client type #{opt[:client]}"
end
if !opt[:nostore] && opt[:nomsg]
opt[:nomsg] = nil
puts "nomsg reverted to false because nostore is false"
end
opt[:concurrency]=concurrency
@concurrency = opt[:concurrency]
@opt=opt
if opt[:log]
@log = Subscriber::Logger.new
opt[:logger]=@log
end
new_client
reset
end
|
Instance Attribute Details
#client ⇒ Object
Returns the value of attribute client.
1521
1522
1523
|
# File 'lib/nchan_tools/pubsub.rb', line 1521
def client
@client
end
|
#client_class ⇒ Object
Returns the value of attribute client_class.
1521
1522
1523
|
# File 'lib/nchan_tools/pubsub.rb', line 1521
def client_class
@client_class
end
|
#concurrency ⇒ Object
Returns the value of attribute concurrency.
1521
1522
1523
|
# File 'lib/nchan_tools/pubsub.rb', line 1521
def concurrency
@concurrency
end
|
#errors ⇒ Object
Returns the value of attribute errors.
1521
1522
1523
|
# File 'lib/nchan_tools/pubsub.rb', line 1521
def errors
@errors
end
|
#finished ⇒ Object
Returns the value of attribute finished.
1521
1522
1523
|
# File 'lib/nchan_tools/pubsub.rb', line 1521
def finished
@finished
end
|
#log ⇒ Object
Returns the value of attribute log.
1521
1522
1523
|
# File 'lib/nchan_tools/pubsub.rb', line 1521
def log
@log
end
|
#max_round_trips ⇒ Object
Returns the value of attribute max_round_trips.
1521
1522
1523
|
# File 'lib/nchan_tools/pubsub.rb', line 1521
def max_round_trips
@max_round_trips
end
|
#messages ⇒ Object
Returns the value of attribute messages.
1521
1522
1523
|
# File 'lib/nchan_tools/pubsub.rb', line 1521
def messages
@messages
end
|
#quit_message ⇒ Object
Returns the value of attribute quit_message.
1521
1522
1523
|
# File 'lib/nchan_tools/pubsub.rb', line 1521
def quit_message
@quit_message
end
|
#url ⇒ Object
Returns the value of attribute url.
1521
1522
1523
|
# File 'lib/nchan_tools/pubsub.rb', line 1521
def url
@url
end
|
#waiting ⇒ Object
Returns the value of attribute waiting.
1521
1522
1523
|
# File 'lib/nchan_tools/pubsub.rb', line 1521
def waiting
@waiting
end
|
Instance Method Details
#abort ⇒ Object
1564
1565
1566
|
# File 'lib/nchan_tools/pubsub.rb', line 1564
def abort
@client.terminate
end
|
#errors? ⇒ Boolean
1567
1568
1569
|
# File 'lib/nchan_tools/pubsub.rb', line 1567
def errors?
not no_errors?
end
|
#make_error(client, what, code, msg, failword = " failed") ⇒ Object
1641
1642
1643
|
# File 'lib/nchan_tools/pubsub.rb', line 1641
def make_error(client, what, code, msg, failword=" failed")
"#{client.class.name.split('::').last} #{what}#{failword}: #{msg} (code #{code})"
end
|
#match_errors(regex) ⇒ Object
1573
1574
1575
1576
1577
1578
1579
|
# File 'lib/nchan_tools/pubsub.rb', line 1573
def match_errors(regex)
return false if no_errors?
@errors.each do |err|
return false unless err =~ regex
end
true
end
|
#new_client ⇒ Object
1550
1551
1552
|
# File 'lib/nchan_tools/pubsub.rb', line 1550
def new_client
@client=@Client_Class.new self, @opt
end
|
#no_errors? ⇒ Boolean
1570
1571
1572
|
# File 'lib/nchan_tools/pubsub.rb', line 1570
def no_errors?
@errors.empty?
end
|
#on(evt_name = nil, &block) ⇒ Object
1619
1620
1621
1622
1623
1624
1625
|
# File 'lib/nchan_tools/pubsub.rb', line 1619
def on(evt_name = nil, &block)
if block_given?
@on[evt_name.to_sym] = block
else
@on[evt_name.to_sym] or @empty_block
end
end
|
#on_failure(err = nil, nostore = false, &block) ⇒ Object
1645
1646
1647
1648
1649
1650
1651
1652
|
# File 'lib/nchan_tools/pubsub.rb', line 1645
def on_failure(err=nil, nostore=false, &block)
if block_given?
@on_failure=block
else
@errors << err.to_s unless nostore
@on_failure.call(err.to_s, err.bundle) if @on_failure.respond_to? :call
end
end
|
#on_message(msg = nil, bundle = nil, &block) ⇒ Object
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
|
# File 'lib/nchan_tools/pubsub.rb', line 1627
def on_message(msg=nil, bundle=nil, &block)
if block_given?
@on_message=block
else
@messages << msg if @messages
if @quit_message == msg.to_s
@on_message.call(msg, bundle) if @on_message
return false
end
@on_message.call(msg, bundle) if @on_message
end
end
|
#reset ⇒ Object
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
|
# File 'lib/nchan_tools/pubsub.rb', line 1553
def reset
@errors=[]
unless @nostore
@messages=MessageStore.new :noid => !(client.provides_msgid? && @care_about_message_ids)
@messages.name="sub"
end
@waiting=0
@finished=0
new_client if terminated?
self
end
|
#run ⇒ Object
1582
1583
1584
1585
1586
1587
1588
1589
1590
|
# File 'lib/nchan_tools/pubsub.rb', line 1582
def run
begin
client.current_actor
rescue Celluloid::DeadActorError
return false
end
@client.async.run
self
end
|
#stop ⇒ Object
1591
1592
1593
1594
1595
1596
1597
1598
|
# File 'lib/nchan_tools/pubsub.rb', line 1591
def stop
begin
@client.stop
rescue Celluloid::DeadActorError
return false
end
true
end
|
#terminate ⇒ Object
1599
1600
1601
1602
1603
1604
1605
1606
|
# File 'lib/nchan_tools/pubsub.rb', line 1599
def terminate
begin
@client.terminate
rescue Celluloid::DeadActorError
return false
end
true
end
|
#terminated? ⇒ Boolean
1607
1608
1609
1610
1611
1612
1613
1614
|
# File 'lib/nchan_tools/pubsub.rb', line 1607
def terminated?
begin
client.current_actor unless client == nil
rescue Celluloid::DeadActorError
return true
end
false
end
|
#wait(until_what = nil, timeout = nil) ⇒ Object
1615
1616
1617
|
# File 'lib/nchan_tools/pubsub.rb', line 1615
def wait(until_what=nil, timeout = nil)
@client.poke until_what, timeout
end
|