A string (thats looks like an array) into an array Ruby - ruby

I have an output from an API that look like this... (its a string)
[[2121212,212121,asd],[2323232,23232323,qasdasd]]
Its a string - not an array. I want to convert it to an array and then extract the first two elements in each array in the nested array to:
[2121212,212121],[2323232,23232323]
What's the best way to do this ruby? I could use regexp and extract - but basically the string is already an array, however the class is a string.
I tried
array.push(response)
but that just put the string in to the array as one element. I guess what would be nice is a to_array method

You will need to use regular expression anyway if not eval (shrudder...), this is the shortest one
str = "[[2121212,212121,asd],[2323232,23232323,qasdasd],[2424242,24242424,qasdasd]]"
p str.scan(/(\d+),(\d+)/)
=>[["2121212", "212121"], ["2323232", "23232323"], ["2424242", "24242424"]]

Assuming this is a JSON response (and if so, it is badly malformed and you should talk to the people that are responsible for this) you could write something like:
require 'json'
input= '[[2121212,212121,Asd],[2323232,23232323,qasdasd]]'
input.gsub!(/([A-Za-z ]+)/,'"\1"')
json = JSON.parse input
output = json.map{|x| x[0...2]}
p output
this prints
[[2121212, 212121], [2323232, 23232323]]

Using eval is very bad but I have no other easy option.
test_str = "[[2121212,212121,asd],[2323232,23232323,qasdasd]]"
test_str.gsub!(/([a-z]+)/) do
"'#{$1}'"
end
=> "[[2121212,212121,'asd'],[2323232,23232323,'qasdasd']]"
test_array = eval(test_str)
=> [[2121212, 212121, "asd"], [2323232, 23232323, "qasdasd"]]
test_array.each do |element|
element.delete(element.last)
end
=> [[2121212, 212121], [2323232, 23232323]]

Related

Parsing JSON from text file

I have a log file that appears to have a time stamp followed by a tab, then a string of JSON.
10/28/2014 00:04:51 {"servers":[{"id":833495,"account_id":39033,"name":"USTSMASCMSP113","host":"USTSMASCMSP113","health_status":"green","reporting":true,"last_reported_at":"2014-10-28T04:04:17+00:00","summary":{"cpu":28.7,"cpu_stolen":0,"disk_io":0.17,"memory":60.8,"memory_used":10444865536,"memory_total":17176723456,"fullest_disk":84.0,"fullest_disk_free":8171000000},"links":{"alert_policy":28370}},{"id":831246,"account_id":39033,"name":"USTSMASCMSP118","host":"USTSMASCMSP118","health_status":"green","reporting":true,"last_reported_at":"2014-10-28T04:04:17+00:00","summary":{"cpu":30.7,"cpu_stolen":0,"disk_io":2.57,"memory":54.3,"memory_used":9329180672,"memory_total":17176723456,"fullest_disk":83.1,"fullest_disk_free":8653000000},"links":{"alert_policy":28370}},{"id":833455,"account_id":39033,"name":"USTSMASCMSP119","host":"USTSMASCMSP119","health_status":"green","reporting":true,"last_reported_at":"2014-10-28T04:04:17+00:00","summary":{"cpu":36.3,"cpu_stolen":0,"disk_io":0.38,"memory":54.5,"memory_used":9362735104,"memory_total":17176723456,"fullest_disk":73.7,"fullest_disk_free":17912000000},"links":{"alert_policy":28370}},{"id":838342,"account_id":39033,"name":"USTSMASCMSP120","host":"USTSMASCMSP120","health_status":"green","reporting":true,"last_reported_at":"2014-10-28T04:04:17+00:00","summary":{"cpu":29.6,"cpu_stolen":0,"disk_io":0,"memory":55.6,"memory_used":9550430208,"memory_total":17176723456,"fullest_disk":84.9,"fullest_disk_free":10259000000},"links":{"alert_policy":28370}},{"id":833500,"account_id":39033,"name":"USTSMASCMSP121","host":"USTSMASCMSP121","health_status":"green","reporting":true,"last_reported_at":"2014-10-28T04:04:17+00:00","summary":{"cpu":38.9,"cpu_stolen":0,"disk_io":5.47,"memory":56.7,"memory_used":9743368192,"memory_total":17176723456,"fullest_disk":84.9,"fullest_disk_free":10288000000},"links":{"alert_policy":28370}},{"id":831221,"account_id":39033,"name":"USTSMASCMSP123","host":"USTSMASCMSP123","health_status":"orange","reporting":true,"last_reported_at":"2014-10-28T04:04:17+00:00","summary":{"cpu":32.4,"cpu_stolen":0,"disk_io":0.42,"memory":53.8,"memory_used":9232711680,"memory_total":17176723456,"fullest_disk":87.2,"fullest_disk_free":8705000000},"links":{"alert_policy":28370}},{"id":833466,"account_id":39033,"name":"USTSMASCMSP124","host":"USTSMASCMSP124","health_status":"green","reporting":true,"last_reported_at":"2014-10-28T04:04:17+00:00","summary":{"cpu":25.1,"cpu_stolen":0,"disk_io":4.28,"memory":58.6,"memory_used":10065281024,"memory_total":17176723456,"fullest_disk":74.2,"fullest_disk_free":19387000000},"links":{"alert_policy":28370}},{"id":838346,"account_id":39033,"name":"USTSMASCMSP125","host":"USTSMASCMSP125","health_status":"orange","reporting":true,"last_reported_at":"2014-10-28T04:04:17+00:00","summary":{"cpu":32.2,"cpu_stolen":0,"disk_io":0.13,"memory":56.5,"memory_used":9703522304,"memory_total":17176723456,"fullest_disk":89.1,"fullest_disk_free":7443000000},"links":{"alert_policy":28370}},{"id":833504,"account_id":39033,"name":"USTSMASCMSP126","host":"USTSMASCMSP126","health_status":"orange","reporting":true,"last_reported_at":"2014-10-28T04:04:17+00:00","summary":{"cpu":29.6,"cpu_stolen":0,"disk_io":0,"memory":56.9,"memory_used":9776922624,"memory_total":17176723456,"fullest_disk":85.7,"fullest_disk_free":9761000000},"links":{"alert_policy":28370}},{"id":831212,"account_id":39033,"name":"USTSMASCMSP127","host":"USTSMASCMSP127","health_status":"orange","reporting":true,"last_reported_at":"2014-10-28T04:04:17+00:00","summary":{"cpu":30.5,"cpu_stolen":0,"disk_io":0,"memory":50.9,"memory_used":8735686656,"memory_total":17176723456,"fullest_disk":85.0,"fullest_disk_free":10222000000},"links":{"alert_policy":28370}},{"id":833510,"account_id":39033,"name":"USTSMASCMSP128","host":"USTSMASCMSP128","health_status":"green","reporting":true,"last_reported_at":"2014-10-28T04:04:17+00:00","summary":{"cpu":26.8,"cpu_stolen":0,"disk_io":0,"memory":57.8,"memory_used":9936306176,"memory_total":17176723456,"fullest_disk":79.7,"fullest_disk_free":10393000000},"links":{"alert_policy":28370}},{"id":833473,"account_id":39033,"name":"USTSMASCMSP129","host":"USTSMASCMSP129","health_status":"orange","reporting":true,"last_reported_at":"2014-10-28T04:04:17+00:00","summary":{"cpu":31.9,"cpu_stolen":0,"disk_io":0.17,"memory":56.4,"memory_used":9693036544,"memory_total":17176723456,"fullest_disk":86.7,"fullest_disk_free":9095000000},"links":{"alert_policy":28370}},{"id":3922351,"account_id":39033,"name":"USTSMASCMSP136","host":"USTSMASCMSP136","health_status":"orange","reporting":true,"last_reported_at":"2014-10-28T04:04:17+00:00","summary":{"cpu":16.2,"cpu_stolen":0,"disk_io":0.28,"memory":13.3,"memory_used":4552916992,"memory_total":34356592640,"fullest_disk":73.3,"fullest_disk_free":56043000000},"links":{"alert_policy":28369}},{"id":831184,"account_id":39033,"name":"USTSMASCMSP144","host":"USTSMASCMSP144","health_status":"orange","reporting":true,"last_reported_at":"2014-10-28T04:04:17+00:00","summary":{"cpu":30.8,"cpu_stolen":0,"disk_io":0.88,"memory":61.8,"memory_used":10617880576,"memory_total":17176723456,"fullest_disk":90.9,"fullest_disk_free":8237000000},"links":{"alert_policy":28370}},{"id":833478,"account_id":39033,"name":"USTSMASCMSP145","host":"USTSMASCMSP145","health_status":"green","reporting":true,"last_reported_at":"2014-10-28T04:04:17+00:00","summary":{"cpu":30.5,"cpu_stolen":0,"disk_io":0,"memory":50.3,"memory_used":8631877632,"memory_total":17176723456,"fullest_disk":81.5,"fullest_disk_free":9712000000},"links":{"alert_policy":28370}},{"id":838357,"account_id":39033,"name":"USTSMASCMSP146","host":"USTSMASCMSP146","health_status":"green","reporting":true,"last_reported_at":"2014-10-28T04:04:17+00:00","summary":{"cpu":29.7,"cpu_stolen":0,"disk_io":0,"memory":52.8,"memory_used":9069133824,"memory_total":17176723456,"fullest_disk":80.6,"fullest_disk_free":9934000000},"links":{"alert_policy":28370}},{"id":838360,"account_id":39033,"name":"USTSMASCMSP147","host":"USTSMASCMSP147","health_status":"green","reporting":true,"last_reported_at":"2014-10-28T04:04:17+00:00","summary":{"cpu":28.7,"cpu_stolen":0,"disk_io":0,"memory":73.6,"memory_used":12640583680,"memory_total":17176723456,"fullest_disk":80.8,"fullest_disk_free":9825000000},"links":{"alert_policy":28370}},{"id":3964940,"account_id":39033,"name":"USTSMASCMSP148","host":"USTSMASCMSP148","health_status":"orange","reporting":true,"last_reported_at":"2014-10-28T04:04:17+00:00","summary":{"cpu":12.6,"cpu_stolen":0,"disk_io":0.08,"memory":10.6,"memory_used":3636461568,"memory_total":34356592640,"fullest_disk":73.2,"fullest_disk_free":56213000000},"links":{"alert_policy":28369}},{"id":832099,"account_id":39033,"name":"USTSMASCMSP196","host":"USTSMASCMSP196","health_status":"green","reporting":true,"last_reported_at":"2014-10-28T04:04:17+00:00","summary":{"cpu":24.8,"cpu_stolen":0,"disk_io":0,"memory":30.2,"memory_used":5187305472,"memory_total":17176723456,"fullest_disk":77.2,"fullest_disk_free":11962000000},"links":{"alert_policy":28370}},{"id":832093,"account_id":39033,"name":"USTSMASCMSP197","host":"USTSMASCMSP197","health_status":"green","reporting":true,"last_reported_at":"2014-10-28T04:04:17+00:00","summary":{"cpu":24.8,"cpu_stolen":0,"disk_io":0.34,"memory":30.1,"memory_used":5169479680,"memory_total":17176723456,"fullest_disk":74.5,"fullest_disk_free":13362000000},"links":{"alert_policy":28370}},{"id":832082,"account_id":39033,"name":"USTSMASCMSP198","host":"USTSMASCMSP198","health_status":"green","reporting":true,"last_reported_at":"2014-10-28T04:04:17+00:00","summary":{"cpu":23.5,"cpu_stolen":0,"disk_io":0,"memory":26.8,"memory_used":4604297216,"memory_total":17176723456,"fullest_disk":72.8,"fullest_disk_free":14268000000},"links":{"alert_policy":28370}},{"id":832077,"account_id":39033,"name":"USTSMASCMSP199","host":"USTSMASCMSP199","health_status":"green","reporting":true,"last_reported_at":"2014-10-28T04:04:17+00:00","summary":{"cpu":21.0,"cpu_stolen":0,"disk_io":0,"memory":31.3,"memory_used":5379194880,"memory_total":17176723456,"fullest_disk":71.5,"fullest_disk_free":14950000000},"links":{"alert_policy":28370}},{"id":832832,"account_id":39033,"name":"USTSMASCMSP200","host":"USTSMASCMSP200","health_status":"green","reporting":true,"last_reported_at":"2014-10-28T04:04:17+00:00","summary":{"cpu":50.3,"cpu_stolen":0,"disk_io":0,"memory":28.9,"memory_used":4966055936,"memory_total":17176723456,"fullest_disk":74.3,"fullest_disk_free":13477000000},"links":{"alert_policy":28370}},{"id":831194,"account_id":39033,"name":"USTSMASCMSP201","host":"USTSMASCMSP201","health_status":"green","reporting":true,"last_reported_at":"2014-10-28T04:04:17+00:00","summary":{"cpu":27.0,"cpu_stolen":0,"disk_io":0,"memory":28.6,"memory_used":4918870016,"memory_total":17176723456,"fullest_disk":74.4,"fullest_disk_free":13397000000},"links":{"alert_policy":28370}},{"id":832059,"account_id":39033,"name":"USTSMASCMSP202","host":"USTSMASCMSP202","health_status":"green","reporting":true,"last_reported_at":"2014-10-28T04:04:17+00:00","summary":{"cpu":25.1,"cpu_stolen":0,"disk_io":0,"memory":26.6,"memory_used":4564451328,"memory_total":17176723456,"fullest_disk":68.3,"fullest_disk_free":16645000000},"links":{"alert_policy":28370}},{"id":832043,"account_id":39033,"name":"USTSMASCMSP203","host":"USTSMASCMSP203","health_status":"green","reporting":true,"last_reported_at":"2014-10-28T04:04:17+00:00","summary":{"cpu":24.7,"cpu_stolen":0,"disk_io":1.17,"memory":25.2,"memory_used":4326424576,"memory_total":17176723456,"fullest_disk":68.2,"fullest_disk_free":16646000000},"links":{"alert_policy":28370}},{"id":832034,"account_id":39033,"name":"USTSMASCMSP204","host":"USTSMASCMSP204","health_status":"green","reporting":true,"last_reported_at":"2014-10-28T04:04:17+00:00","summary":{"cpu":26.5,"cpu_stolen":0,"disk_io":0,"memory":27.1,"memory_used":4653580288,"memory_total":17176723456,"fullest_disk":68.5,"fullest_disk_free":16510000000},"links":{"alert_policy":28370}},{"id":832031,"account_id":39033,"name":"USTSMASCMSP205","host":"USTSMASCMSP205","health_status":"green","reporting":true,"last_reported_at":"2014-10-28T04:04:17+00:00","summary":{"cpu":27.8,"cpu_stolen":0,"disk_io":0,"memory":24.9,"memory_used":4285530112,"memory_total":17176723456,"fullest_disk":68.8,"fullest_disk_free":16376000000},"links":{"alert_policy":28370}},{"id":832024,"account_id":39033,"name":"USTSMASCMSP206","host":"USTSMASCMSP206","health_status":"green","reporting":true,"last_reported_at":"2014-10-28T04:04:17+00:00","summary":{"cpu":30.2,"cpu_stolen":0,"disk_io":0,"memory":26.5,"memory_used":4548722688,"memory_total":17176723456,"fullest_disk":67.2,"fullest_disk_free":17181000000},"links":{"alert_policy":28370}},{"id":831202,"account_id":39033,"name":"USTSMASCMSP207","host":"USTSMASCMSP207","health_status":"green","reporting":true,"last_reported_at":"2014-10-28T04:04:17+00:00","summary":{"cpu":23.9,"cpu_stolen":0,"disk_io":0,"memory":25.6,"memory_used":4394582016,"memory_total":17176723456,"fullest_disk":67.3,"fullest_disk_free":17146000000},"links":{"alert_policy":28370}},{"id":3979556,"account_id":39033,"name":"USTSMASCMSP227","host":"USTSMASCMSP227","health_status":"green","reporting":true,"last_reported_at":"2014-10-28T04:04:17+00:00","summary":{"cpu":8.25,"cpu_stolen":0,"disk_io":5.96,"memory":18.1,"memory_used":3116367872,"memory_total":17174626304,"fullest_disk":65.1,"fullest_disk_free":18301000000},"links":{"alert_policy":28369}},{"id":3965125,"account_id":39033,"name":"USTSMASCMSP247","host":"USTSMASCMSP247","health_status":"green","reporting":true,"last_reported_at":"2014-10-28T04:04:17+00:00","summary":{"cpu":18.2,"cpu_stolen":0,"disk_io":34.0,"memory":32.3,"memory_used":5543821312,"memory_total":17174626304,"fullest_disk":59.9,"fullest_disk_free":28765000000},"links":{"alert_policy":28369}},{"id":3965022,"account_id":39033,"name":"USTSMASCMSP248","host":"USTSMASCMSP248","health_status":"orange","reporting":true,"last_reported_at":"2014-10-28T04:04:17+00:00","summary":{"cpu":43.9,"cpu_stolen":0,"disk_io":8.61,"memory":28.1,"memory_used":4821352448,"memory_total":17143169024,"fullest_disk":86.5,"fullest_disk_free":9645000000},"links":{"alert_policy":28369}},{"id":3851100,"account_id":39033,"name":"ustsmvscmsp888","host":"ustsmvscmsp888","health_status":"orange","reporting":true,"last_reported_at":"2014-10-28T04:04:17+00:00","summary":{"cpu":19.7,"cpu_stolen":0,"disk_io":0,"memory":42.9,"memory_used":1841299456,"memory_total":4293918720,"fullest_disk":72.9,"fullest_disk_free":14243000000},"links":{"alert_policy":28369}}],"links":{"server.alert_policy":"/v2/alert_policies/{alert_policy_id}"}}
I have code that reads the input file and I am attempting to get values from the string like so.
require 'json'
input = File.open("input.txt", 'r')
input.each do |line|
line = line.split("\t")
my_json = line[1].to_json
puts my_json["cpu"]
end
The end result looks like this:
C:\>ruby test.rb
cpu
I am not entirely sure if the string itself is not formatted correctly or if I'm not calling on the value correctly.
Run this code inside console and check the return but i'm pretty sure you can't call
my_json["cpu"]
You will need to call something like:
my_json["servers"].first["cpu"]
my_json = line[1].to_json
Will take an object and convert it to JSON, not parse an existing JSON string into a ruby object. You're going to wind up with an escaped string within a JSON string.
You need something like
my_json = JSON.parse(line[1])
you can verify this has worked correctly by outputting my_json to stdout:
$stdout << my_json.class.name
You should have an instance of Hash or Openstruct.
Consider this:
require 'json'
line = '10/28/2014 00:04:51 {"servers":[{"id":833495,"account_id":39033}]}'
JSON[line[/\{.+}/]]
# => {"servers"=>[{"id"=>833495, "account_id"=>39033}]}
That means you can reduce your parsing code to something like:
File.foreach("input.txt") do |line|
data = JSON[line[/\{.+}/]]
# do something with the data
end
Regular expressions are greedy, so /\{.+}/ will start at the first { and look until it finds the last }, and return the starting and ending braces, and everything in between.
The JSON [] method is smart enough to know that if its parameter is a string it should try to parse it and return a Ruby array or hash. If the parameter is an Array or Hash, it will serialize it.
Note: It isn't necessary, and definitely not desirable to include huge data samples like you did. Reduce it to the bare minimum necessary to demonstrate the problem.

Ruby Iterator - define a method that accepts an array and a string

I want to define a method that accept an array, and a string, then it should find all the strings in the array that starts with the string that was supplied: example array["Jamaica","Japan","USA","China"]; if the string supplied is Ja, then it should return Jamaica and Japan
Try using keep_if and regex :
["Jamaica","Japan","USA","China"].keep_if { |c| c =~ /^ja/i }
# returns ["Jamaica", "Japan"]
It's a static example. To create the regex dynamically, do Regexp.new("^#{your_var}", true)
The most important methods for an Array are not found in the documentation for Array, but on that for Enumerable. You are searching for a method which finds all elements which comply to some condition.
This condition was that the string starts with some characters. Wouldn't it be nice if a string object had a method for that?
This is overkill but it would work quickly for huge arrays. Lucky for you, I just yesterday stumbled upon a new high speed trie library for ruby called TRIEZ. Run gem install triez then from their example switched up a little bit:
require 'triez'
countries = ["Jamaica","Japan","USA","China"]
t = Triez.new
countries.each do |word|
t[word] = 1
end
ja_countries = []
t.search_with_prefix 'Ja' do |suffix|
ja_countries += "Ja#{suffix}"
end
It's implemented in C so I bet it's fast as hell on huge arrays.
You can do this using many different things, if you don' want to modify the array you can use the select method from Array Class.
["Jamaica","Japan","USA","China"].select{|item| item.match("Ja")}
The Array will be intact.

Passing original string PLUS matches when using gsub and block?

Rails fans are familiar with params[:terms] or a hash of 'things' passed to the controller collected form the url. E.g.:
params
=> {"term"=>"Warren Buffet",
"controller"=>"search",
"format"=>"json",
"action"=>"index"}
If I want to use "Warren Buffet", "Warren" and "Buffet" in the code below, does anyone know which method I should be using instead? gsub is close, but it takes each match and not the original string too. Unless I'm doing it wrong, which is totally possible:
#potential_investors = []
params[:term].gsub(/(\w{1,})/) do |term|
#potential_investors &lt&lt User.where(:investor => true)
.order('first_name ASC, last_name ASC')
.search_potential_investors(term)
end
Thoughts?
How about:
s = "Filthy Rich"
s.split(" ").push(s)
>> ["Filthy", "Rich", "Filthy Rich"]
Or with scan if you prefer to use the regexp instead:
s.scan(/\w+/).push(s)
>> ["Filthy", "Rich", "Filthy Rich"]
params["term"].gsub(/(\w{1,})/)
returns an enumerator. You could convert it to an array and append the original term to it:
ary = params["term"].gsub(/(\w{1,})/).to_a + [params["term"]]
then process it:
ary.each do |term|
...

Why does Array.to_s return brackets?

For an array, when I type:
puts array[0]
==> text
Yet when I type
puts array[0].to_s
==> ["text"]
Why the brackets and quotes? What am I missing?
ADDENDUM: my code looks like this
page = open(url) {|f| f.read }
page_array = page.scan(/regex/) #pulls partial urls into an array
partial_url = page_array[0].to_s
full_url = base_url + partial_url #adds each partial url to a consistent base_url
puts full_url
what I'm getting looks like:
http://www.stackoverflow/["questions"]
This print the array as is without brackets
array.join(", ")
to_s is just an alias to inspect for the Array class.
Not that this means a lot other than instead of expecting array.to_s to return a string it's actually returning array.inspect which, based on the name of the method, isn't really what you are looking for.
If you want just the "guts" try:
page_array.join
If there are multiple elements to the array:
page_array.join(" ")
This will make:
page_array = ["test","this","function"]
return:
"test this function"
What "to_s" on an Array returns, depends on the version of Ruby you are using as mentioned above. 1.9.X returns:
"[\"test\"]"
You need to show us the regex to really fix this properly, but this will do it:
Replace this
partial_url = page_array[0].to_s
with this
partial_url = page_array[0][0]
This doesn't necessarily fix why you are getting a doubled-up array, but you can flatten it and then call the first element like this.
page_array = page.scan(/regex/).flatten
Flattening takes out stacked arrays and creates one level, so if you had [1,2,[3,[4,5,6]]] and called flatten on it, you would get [1,2,3,4,5,6]
It is also more robust than doing array[0][0], because, if you had more than two arrays nested in the first element, you would run into the same issue.
Iain is correct though, without seeing the regex, we can't suss out the root cause.

parsing in ruby

I have this Hash:
cookie = {"fbs_138415639544444"=>["\"access_token=138415639544444|5c682220fa7ebccafd97ec58-503523340|9HHx3z7GzOBPdk444wtt&expires=0
&secret=64aa8b3327eafbfd22ba070b&session_key=5c682220fa7dsfdsafas3523340
&sig=4a494b851ff43d3a58dfa8757b702dfe&uid=503523340\""],
"_play_session"=>["fdasdfasdf"]}
I need to get the substring from right after access_token= to right before &expires. The problem is that the number in the key fbs_138415639544444 changes every time, just the part fbs_ remains constant.
Any idea how to only get:
"138415639544444|5c682220fa7ebccafd97ec58-503523340|9HHx3z7GzOBPdk444wtt"
This is a common task when decoding parameters and queries in HTML URLs. Here's a little method to break down the parameters into a hash. From there it's easy to get the value you want:
def get_params_hash(params)
Hash[ *params.split('&').map{ |q| q.split('=') }.flatten ]
end
p get_params_hash(cookie['fbs_138415639544444'].first)['"access_token']
# >> "138415639544444|5c682220fa7ebccafd97ec58-503523340|9HHx3z7GzOBPdk444wtt"
In Ruby 1.9+, hashes retain their insertion order, so if the hash always has the value you want as its first entry, you can use
cookie.keys.first #=> "fbs_138415639544444"
otherwise use:
cookie.keys.select{ |k| k[/^fbs_/] }.first #=> "fbs_138415639544444"
I never code in ruby, but this sounds like a typical task for split function.
you just need to split this
"\"access_token=138415639544444|5c682220fa7ebccafd97ec58-503523340|9HHx3z7GzOBPdk444wtt&expires=0
&secret=64aa8b3327eafbfd22ba070b&session_key=5c682220fa7dsfdsafas3523340
&sig=4a494b851ff43d3a58dfa8757b702dfe&uid=503523340\""
by & symbol. The first element of result array will be:
"\"access_token=138415639544444|5c682220fa7ebccafd97ec58-503523340|9HHx3z7GzOBPdk444wtt"
and after split it by =, and the second element of result array should be:
138415639544444|5c682220fa7ebccafd97ec58-503523340|9HHx3z7GzOBPdk444wtt
If you only need the access_key part, then a regex is probably easiest.
cookie["fbs_138415639544444"][0] =~ /access_token\=([-\w\d\|]*)&/
access_key = $1
Here the access_key is in the first capture group and you can get it with $1.
A better option if you'll need other parts of the string (say the session_key), would probably be to use a couple splits and parse the string into it's own hash.
Edit: Just realized you need the key too.
key = cookie.each_key.find { |k| k.start_with? "fbs_" }
Then you can use key to get the value.
Since the key changes, the first step is to get right key:
key = cookie.keys.select {|k| k =~ /^fbs_/}.first
This matches them if they begin with the text "fbs_". The first match is returned.
Next you can get the other value by a few (ugly) splits:
cookie[key].first.split('=')[1].split('&').first
Using a regex might be a bit cleaner, but it depends on what the valid characters are in that string.
Regexs are brittle so I wouldn't use those when the reality is you are parsing query string params in the end so use the CGI lib:
> require 'cgi'
=> true
> cookie = {"fbs_138415639544444"=>["\"access_token=138415639544444|5c682220fa7ebccafd97ec58-503523340|9HHx3z7GzOBPdk444wtt&expires=0&secret=64aa8b3327eafbfd22ba070b&session_key=5c682220fa7dsfdsafas3523340&sig=4a494b851ff43d3a58dfa8757b702dfe&uid=503523340\""], "_play_session"=>["fdasdfasdf"]}
> CGI.parse(cookie.select {|k,v| k =~ /^fbs_/}.first[1][0])["\"access_token"][0]
=> "138415639544444|5c682220fa7ebccafd97ec58-503523340|9HHx3z7GzOBPdk444wtt"
This is how i solved the problem...
access_token_key = cookies.keys.find{|item| item.starts_with?('fbs_') }
token = cookies[access_token_key].first
access_token = token.split("&").find{|item| item.include?('access_token') }
fb_access_token = access_token.split("=").find{|item| !item.include?('access_token') }

Resources