Class: WaseEndpoint::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/wase_endpoint/message.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, raw_message) ⇒ Message

Returns a new instance of Message.



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/wase_endpoint/message.rb', line 7

def initialize(id, raw_message)
  myname_and_hashtag, program_counter, program_listing_uri, unix_timestamp, output_uri, input_uri, input_uri_1 = raw_message.split(/,/)
  
  @id = id
  @program_counter = program_counter.to_i
  @program_listing_uri = program_listing_uri.strip
  @timestamp = unix_timestamp.to_i
  @output_uri = output_uri.strip
  @input_uri = input_uri.strip if input_uri
  @input_uri_1 = input_uri_1.strip if input_uri_1
  @increment = 1
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



5
6
7
# File 'lib/wase_endpoint/message.rb', line 5

def id
  @id
end

#input_uriObject (readonly)

Returns the value of attribute input_uri.



5
6
7
# File 'lib/wase_endpoint/message.rb', line 5

def input_uri
  @input_uri
end

#input_uri_1Object (readonly)

Returns the value of attribute input_uri_1.



5
6
7
# File 'lib/wase_endpoint/message.rb', line 5

def input_uri_1
  @input_uri_1
end

#output_uriObject (readonly)

Returns the value of attribute output_uri.



5
6
7
# File 'lib/wase_endpoint/message.rb', line 5

def output_uri
  @output_uri
end

#program_counterObject (readonly)

Returns the value of attribute program_counter.



5
6
7
# File 'lib/wase_endpoint/message.rb', line 5

def program_counter
  @program_counter
end

#program_listing_uriObject (readonly)

Returns the value of attribute program_listing_uri.



5
6
7
# File 'lib/wase_endpoint/message.rb', line 5

def program_listing_uri
  @program_listing_uri
end

#timestampObject (readonly)

Returns the value of attribute timestamp.



5
6
7
# File 'lib/wase_endpoint/message.rb', line 5

def timestamp
  @timestamp
end

Instance Method Details

#==(other) ⇒ Object



20
21
22
# File 'lib/wase_endpoint/message.rb', line 20

def ==(other)
  @id == other.id
end

#fetch_outputObject



28
29
30
# File 'lib/wase_endpoint/message.rb', line 28

def fetch_output
  RestClient.get('http://' + @output_uri)
end

#fetch_program_listingObject



24
25
26
# File 'lib/wase_endpoint/message.rb', line 24

def fetch_program_listing
  JSON.parse(RestClient.get('http://' + @program_listing_uri))
end

#new_program_counterObject



52
53
54
# File 'lib/wase_endpoint/message.rb', line 52

def new_program_counter
  @program_counter + @increment
end

#send_input(input) ⇒ Object

Accepts either a String of raw data, or a Hash of data and a program counter increment. An Exception will be thrown if this increment is negative.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/wase_endpoint/message.rb', line 35

def send_input(input)
  
  if input.is_a?(Hash)
    @increment = input[:increment]
    raise ArgumentError, 'You cannot have negative program counter increments' if @increment < 0
    input = input[:data]
  end
  
  input_uri = 'http://' + (@input_uri || @output_uri)
  
  # RestClient can't follow a redirect for put, so we'll expand it.
  input_uri[/bit\.ly(\/\w+)/]
  expanded_input_uri = Net::HTTP.new('bit.ly').head($1)['Location']
  
  RestClient.put(expanded_input_uri, input)
end