Skip to content
This repository has been archived by the owner on Dec 1, 2022. It is now read-only.

Commit

Permalink
Simpler, more reliable test for plaintext JSON in state_looks_like_pl…
Browse files Browse the repository at this point in the history
…aintext
  • Loading branch information
diamondap committed Sep 12, 2016
1 parent d774e35 commit bd0b6e4
Showing 1 changed file with 4 additions and 12 deletions.
16 changes: 4 additions & 12 deletions app/models/work_item_state.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,11 @@ def unzipped_state
private

# Returns true if state looks like plaintext.
# We only look at the first 50 chars, because the full
# string may be a few megabytes, and the gzip header
# in the first few bytes will contain binary data.
# Since our plaintext should always be JSON data,
# we'll do a simple test for opening and closing
# braces.
def state_looks_like_plaintext
if state.length <= 50
sample = state
else
last_index = [state.length, 50].min
sample = state.slice(0, last_index)
end
# Regex checks for unprintable characters.
# If no match, we assume plaintext.
!sample.match(/[^[:print:]]/)
state.start_with?('{') && state.end_with?('}')
end

def set_action
Expand Down

0 comments on commit bd0b6e4

Please sign in to comment.