Class: Consul::Async::JSONEndpoint

Inherits:
Object
  • Object
show all
Defined in:
lib/consul/async/json_endpoint.rb

Overview

Endpoint (aka URL) of a remote API that might be called

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(conf, url, default_value, enforce_json_200: true, query_params: {}, default_value_on_error: false) ⇒ JSONEndpoint

Returns a new instance of JSONEndpoint.



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/consul/async/json_endpoint.rb', line 96

def initialize(conf, url, default_value, enforce_json_200: true, query_params: {}, default_value_on_error: false)
  @conf = conf.create(url)
  @default_value = default_value
  @default_value_on_error = default_value_on_error
  @url = url
  @queue = EM::Queue.new
  @s_callbacks = []
  @e_callbacks = []
  @enforce_json_200 = enforce_json_200
  @start_time = Time.now.utc
  @consecutive_errors = 0
  @query_params = query_params
  @stopping = false
  @stats = EndPointStats.new
  @last_result = JSONResult.new(default_value.to_json, false, HttpResponse.new(nil), stats, 1, fake: true)
  on_response { |result| @stats.on_response result }
  on_error { |http| @stats.on_error http }
  _enable_network_debug if conf.debug && conf.debug[:network]
  fetch
  queue << Object.new
end

Instance Attribute Details

#confObject (readonly)

Returns the value of attribute conf.



95
96
97
# File 'lib/consul/async/json_endpoint.rb', line 95

def conf
  @conf
end

#default_valueObject (readonly)

Returns the value of attribute default_value.



95
96
97
# File 'lib/consul/async/json_endpoint.rb', line 95

def default_value
  @default_value
end

#enforce_json_200Object (readonly)

Returns the value of attribute enforce_json_200.



95
96
97
# File 'lib/consul/async/json_endpoint.rb', line 95

def enforce_json_200
  @enforce_json_200
end

#last_resultObject (readonly)

Returns the value of attribute last_result.



95
96
97
# File 'lib/consul/async/json_endpoint.rb', line 95

def last_result
  @last_result
end

#query_paramsObject (readonly)

Returns the value of attribute query_params.



95
96
97
# File 'lib/consul/async/json_endpoint.rb', line 95

def query_params
  @query_params
end

#queueObject (readonly)

Returns the value of attribute queue.



95
96
97
# File 'lib/consul/async/json_endpoint.rb', line 95

def queue
  @queue
end

#start_timeObject (readonly)

Returns the value of attribute start_time.



95
96
97
# File 'lib/consul/async/json_endpoint.rb', line 95

def start_time
  @start_time
end

#statsObject (readonly)

Returns the value of attribute stats.



95
96
97
# File 'lib/consul/async/json_endpoint.rb', line 95

def stats
  @stats
end

#urlObject (readonly)

Returns the value of attribute url.



95
96
97
# File 'lib/consul/async/json_endpoint.rb', line 95

def url
  @url
end

Instance Method Details

#_enable_network_debugObject



118
119
120
121
122
123
124
125
126
127
# File 'lib/consul/async/json_endpoint.rb', line 118

def _enable_network_debug
  on_response do |result|
    stats = result.stats
    warn "[DBUG][ OK ]#{result.modified? ? '[MODFIED]' : '[NO DIFF]'}" \
    "[s:#{stats.successes},err:#{stats.errors}]" \
    "[#{stats.body_bytes_human.ljust(8)}][#{stats.bytes_per_sec_human.ljust(9)}]"\
    " #{url.ljust(48)}, next in #{result.retry_in} s"
  end
  on_error { |http| warn "[ERROR]: #{url}: #{http.error.inspect}" }
end

#on_error(&block) ⇒ Object



133
134
135
# File 'lib/consul/async/json_endpoint.rb', line 133

def on_error(&block)
  @e_callbacks << block
end

#on_response(&block) ⇒ Object



129
130
131
# File 'lib/consul/async/json_endpoint.rb', line 129

def on_response(&block)
  @s_callbacks << block
end

#ready?Boolean

Returns:

  • (Boolean)


137
138
139
# File 'lib/consul/async/json_endpoint.rb', line 137

def ready?
  @ready
end

#terminateObject



141
142
143
# File 'lib/consul/async/json_endpoint.rb', line 141

def terminate
  @stopping = true
end