Class: LogStash::Inputs::Blueliv
- Inherits:
-
Base
- Object
- Base
- LogStash::Inputs::Blueliv
- Defined in:
- lib/logstash/inputs/blueliv.rb
Instance Attribute Summary collapse
-
#auth ⇒ Object
Returns the value of attribute auth.
-
#timeout ⇒ Object
Returns the value of attribute timeout.
Instance Method Summary collapse
- #client ⇒ Object
- #db_updated?(now) ⇒ Boolean
- #get_feed(queue, name, url, &block) ⇒ Object
- #get_feed_each(queue, name, url, interval, &block) ⇒ Object
- #get_url(name, feed_type, feed_interval) ⇒ Object
- #register ⇒ Object
- #run(queue) ⇒ Object
- #write_last_update_db(date) ⇒ Object
Instance Attribute Details
#auth ⇒ Object
Returns the value of attribute auth.
70 71 72 |
# File 'lib/logstash/inputs/blueliv.rb', line 70 def auth @auth end |
#timeout ⇒ Object
Returns the value of attribute timeout.
70 71 72 |
# File 'lib/logstash/inputs/blueliv.rb', line 70 def timeout @timeout end |
Instance Method Details
#client ⇒ Object
206 207 208 |
# File 'lib/logstash/inputs/blueliv.rb', line 206 def client RestClient end |
#db_updated?(now) ⇒ Boolean
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/logstash/inputs/blueliv.rb', line 105 def db_updated?(now) result = false if File.exists?(INITIALIZE_FILE) File.open(INITIALIZE_FILE, "r") do |file| file.each do |line| unless line.strip.start_with? "#" begin time = DateTime.strptime(line.strip, "%s") result = ((now - time) * ONE_DAY_IN_SECONDS).to_i < ONE_DAY_IN_SECONDS break rescue Exception => e @logger.error(e) end end end end end result end |
#get_feed(queue, name, url, &block) ⇒ Object
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/logstash/inputs/blueliv.rb', line 161 def get_feed(queue, name, url, &block) @logger.info("Start getting #{url} feed") loop do begin response = client.get("#{url}?key=#{API_CLIENT}", :Authorization => @auth, :timeout => @timeout, :user_agent => USER_AGENT, :headers => {"X-API-CLIENT" => API_CLIENT}) response_json = JSON.parse(response.body) items = response_json[RESOURCES[name.to_sym][:items]] items.each do |it| it["location"] = [it["longitude"].to_f, it["latitude"].to_f] collection = RESOURCES[name.to_sym][:items].downcase it["@collection"] = collection it["document_id"] = if it.has_key?("_id") then it["_id"] else SecureRandom.base64(32) end it.delete("_id") if it.has_key?("_id") @logger.debug("#{it}") evt = LogStash::Event.new(it) decorate(evt) queue << evt end @logger.info("End getting data from #{url}") block.call if block break rescue RestClient::Exception => e case e.http_code when 401, 403 @logger.info("You do not have access to this resource #{url}! Please contact #{@contact}") break when 404 @logger.info("Resource #{url} not found") break when 429 @logger.info("You exceeded your request limit rate!") break else @logger.error(e) @logger.info("Will retry in #{FAILURE_SLEEP} seconds") sleep(FAILURE_SLEEP) end rescue Exception => e @logger.info("Will retry in #{FAILURE_SLEEP} seconds") sleep(FAILURE_SLEEP) end end end |
#get_feed_each(queue, name, url, interval, &block) ⇒ Object
154 155 156 157 158 159 |
# File 'lib/logstash/inputs/blueliv.rb', line 154 def get_feed_each(queue, name, url, interval, &block) loop do get_feed(queue, name, url, &block) sleep(interval) end end |
#get_url(name, feed_type, feed_interval) ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/logstash/inputs/blueliv.rb', line 133 def get_url(name, feed_type, feed_interval) base_url = @api_url + RESOURCES[name.to_sym][:endpoint] if RESOURCES[name.to_sym][:feeds].key?(feed_type.to_sym) feed_lookup = RESOURCES[name.to_sym][:feeds][feed_type.to_sym] else raise ArgumentError, "Feed #{feed_type} does not exist!" end interval = nil feed_lookup.keys.each do |k| if feed_interval <= k interval = k break end end interval = feed_lookup.keys.max if interval == nil return base_url + feed_lookup[interval], interval end |
#register ⇒ Object
85 86 87 88 89 90 91 |
# File 'lib/logstash/inputs/blueliv.rb', line 85 def register merger = proc { |key, v1, v2| Hash === v1 && Hash === v2 ? v1.merge(v2, &merger) : Array === v1 && Array === v2 ? v1 | v2 : [:undefined, nil, :nil].include?(v2) ? v1 : v2 } @auth = "Bearer #{api_key}" @feeds = DEFAULT_CONFIG.merge(feeds, &merger) @timeout = http_timeout end |
#run(queue) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/logstash/inputs/blueliv.rb', line 93 def run(queue) threads = [] @feeds.each do |name, conf| if feeds[name]["active"] == 'true' url, interval = get_url(name, @feeds[name]["feed_type"], @feeds[name]["interval"]) threads << Thread.new{get_feed_each(queue, name, url, interval)} end end threads.map {|t| t.join} end |
#write_last_update_db(date) ⇒ Object
126 127 128 129 130 131 |
# File 'lib/logstash/inputs/blueliv.rb', line 126 def write_last_update_db(date) File.open(INITIALIZE_FILE, "w") do |file| file.write("# GENERATED FILE. DO NOT EDIT IT\n") file.write("#{date.to_time.to_i}\n") end end |