Class: HttpState
- Inherits:
-
Object
- Object
- HttpState
- Defined in:
- lib/httpstate.rb
Defined Under Namespace
Classes: Message
Constant Summary collapse
- VERSION =
'0.1.1'
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#uuid ⇒ Object
readonly
Returns the value of attribute uuid.
-
#ws ⇒ Object
readonly
Returns the value of attribute ws.
Class Method Summary collapse
- .get(uuid) ⇒ Object
- .message ⇒ Object
- .post ⇒ Object
- .put ⇒ Object
- .read ⇒ Object
- .set(uuid, data) ⇒ Object
- .write ⇒ Object
Instance Method Summary collapse
- #emit(type, data = nil) ⇒ Object
- #get ⇒ Object (also: #read)
-
#initialize(uuid) ⇒ HttpState
constructor
A new instance of HttpState.
- #off(type, &callback) ⇒ Object
- #on(type, &callback) ⇒ Object
- #set(data) ⇒ Object (also: #post, #put, #write)
Constructor Details
#initialize(uuid) ⇒ HttpState
Returns a new instance of HttpState.
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/httpstate.rb', line 86 def initialize(uuid) @data = nil @et = nil @uuid = uuid @ws = nil Thread.new do EM.run do @ws = Faye::WebSocket::Client.new('wss://httpstate.com/' + @uuid) @ws.on :close do |_| puts '@ws.close' end @ws.on :error do |_| puts '@ws.error' end @ws.on :message do |event| data = self.class..unpack(event.data) if data && data.uuid == @uuid && data.type == 1 @data = data.value emit('change', @data) end end @ws.on :open do |_| puts '@ws.open' @ws.send({ open:@uuid }.to_json) end end get() end end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
21 22 23 |
# File 'lib/httpstate.rb', line 21 def data @data end |
#uuid ⇒ Object (readonly)
Returns the value of attribute uuid.
21 22 23 |
# File 'lib/httpstate.rb', line 21 def uuid @uuid end |
#ws ⇒ Object (readonly)
Returns the value of attribute ws.
21 22 23 |
# File 'lib/httpstate.rb', line 21 def ws @ws end |
Class Method Details
.get(uuid) ⇒ Object
23 24 25 26 27 28 29 30 31 |
# File 'lib/httpstate.rb', line 23 def self.get(uuid) response = Net::HTTP.get(URI.join('https://httpstate.com', uuid)) response rescue StandardError => e puts 'Error: ' + e. nil end |
.message ⇒ Object
57 58 59 |
# File 'lib/httpstate.rb', line 57 def self. Message end |
.post ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/httpstate.rb', line 81 def self.set(uuid, data) uri = URI.join('https://httpstate.com', uuid) request = Net::HTTP::Post.new(uri) request.body = data request.content_type = 'text/plain;charset=UTF-8' response = Net::HTTP.start(uri.hostname, uri.port, use_ssl:uri.scheme == 'https') do |http| http.request(request) end response.code rescue StandardError => e puts 'Error: ' + e. nil end |
.put ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/httpstate.rb', line 82 def self.set(uuid, data) uri = URI.join('https://httpstate.com', uuid) request = Net::HTTP::Post.new(uri) request.body = data request.content_type = 'text/plain;charset=UTF-8' response = Net::HTTP.start(uri.hostname, uri.port, use_ssl:uri.scheme == 'https') do |http| http.request(request) end response.code rescue StandardError => e puts 'Error: ' + e. nil end |
.read ⇒ Object
80 81 82 83 84 85 86 87 88 |
# File 'lib/httpstate.rb', line 80 def self.get(uuid) response = Net::HTTP.get(URI.join('https://httpstate.com', uuid)) response rescue StandardError => e puts 'Error: ' + e. nil end |
.set(uuid, data) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/httpstate.rb', line 61 def self.set(uuid, data) uri = URI.join('https://httpstate.com', uuid) request = Net::HTTP::Post.new(uri) request.body = data request.content_type = 'text/plain;charset=UTF-8' response = Net::HTTP.start(uri.hostname, uri.port, use_ssl:uri.scheme == 'https') do |http| http.request(request) end response.code rescue StandardError => e puts 'Error: ' + e. nil end |
.write ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/httpstate.rb', line 83 def self.set(uuid, data) uri = URI.join('https://httpstate.com', uuid) request = Net::HTTP::Post.new(uri) request.body = data request.content_type = 'text/plain;charset=UTF-8' response = Net::HTTP.start(uri.hostname, uri.port, use_ssl:uri.scheme == 'https') do |http| http.request(request) end response.code rescue StandardError => e puts 'Error: ' + e. nil end |
Instance Method Details
#emit(type, data = nil) ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/httpstate.rb', line 127 def emit(type, data = nil) if @et && @et[type] for callback in @et[type] if data.nil? callback.call else callback.call(data) end end end self end |
#get ⇒ Object Also known as: read
141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/httpstate.rb', line 141 def get if @uuid data = self.class.get(@uuid) if(data != @data) # emit change end @data = data return @data end end |
#off(type, &callback) ⇒ Object
155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/httpstate.rb', line 155 def off(type, &callback) if @et && @et[type] if callback @et[type].delete(callback) end if !callback || @et[type].empty? @et.delete(type) end end self end |
#on(type, &callback) ⇒ Object
169 170 171 172 173 174 175 |
# File 'lib/httpstate.rb', line 169 def on(type, &callback) @et ||= {} @et[type] ||= [] @et[type] << callback self end |
#set(data) ⇒ Object Also known as: post, put, write
177 178 179 |
# File 'lib/httpstate.rb', line 177 def set(data) self.class.set(@uuid, data) if @uuid end |