Advertisement
Guest User

Untitled

a guest
Aug 26th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.44 KB | None | 0 0
  1.   def process_response_chunk(body) do
  2.     # The goal here is to grab the table on the wiki page
  3.     # And then convert it into a tuple (name, value)
  4.     props = body
  5.       |> IO.iodata_to_binary
  6.       |> Floki.find("tr.smwb-propvalue")
  7.  
  8.     # Getting the property names is easy
  9.     # It's just the innerHTML of the first a element in the table header
  10.     prop_names = props
  11.       |> Floki.find("th>a")
  12.       |> Enum.map(&Floki.text/1)
  13.  
  14.     # It's a little more complex to get the property value
  15.     # The best way to do this is to grab the innerHTML of the property value column
  16.     # And remove the text that isn't the value
  17.     # The property will look like prop_value  +
  18.     # So we have to remove the trailing characters
  19.     prop_values = props
  20.         |> Floki.find("span.smwb-value")
  21.         |> Enum.map(&Floki.text/1)
  22.  
  23.         # Remove the "+" character from the value
  24.         |> Enum.map(fn (val) -> String.replace(val, "+", "") end)
  25.  
  26.         # String.strip doesn't seem to work as expected
  27.         # So it's easier to just convert to a char list
  28.         # And remove the character 160, which is  
  29.         # Otherwise known as  
  30.         |> Enum.map(&String.to_char_list/1)
  31.         |> Enum.map(fn (val) -> Enum.reject(val, fn (v) -> v == 160 end) end)
  32.  
  33.     Enum.zip(prop_names, prop_values)
  34.       |> Enum.map(&Tuple.to_list/1)
  35.       |> Enum.map(fn (l) -> Enum.join(l, "\t") end)
  36.       |> Enum.join(",\n")
  37.   end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement