With URI gem in Ruby, how do I actually parse this cookie that has paths, etc? - ruby-on-rails

When I get some cookies back from the server, I'm needing to only pass the key=value parameters in the next request. I don't need the path information, expiration, etc. However, when I inspect the hash, I get all of that back and find no real way to only parse and keep the important information.
For example, here's the cookie returned from my HTTP POST request:
[1] pry(main)> headers['Cookie']
=> "ds_user=testuser; expires=Fri, 01-Sep-2017 22:20:55 GMT; Max-Age=7776000; Path=/, rur=ATN; Path=/, csrftoken=V6pcoCJdHJLb7BGu2BV8TwE5ZoA5fm; expires=Sat, 02-Jun-2018 22:20:55 GMT; Max-Age=31449600; Path=/; Secure, mid=WTM2RwABAAHw09zRkX55hIbjqszz; expires=Fri, 29-May-2037 22:20:55 GMT; Max-Age=630720000; Path=/, uid=554580241; expires=Fri, 01-Sep-2017 22:20:55 GMT; Max-Age=7776000; Path=/, sessionid=4be2b3d03caf0f3073ce8f1215a2d723a23981a52b0c%3AZ0Y0P0yRXSvpy2gBwZguWo4zoOpGajRA%3A%7B%22_auth_user_id%22%3A5545802841%2C%22_auth_user_backend%22%3A%22accounts.backends.CaseInsensitiveModelBackend%22%2C%22_auth_user_hash%22%3A%22%22%2C%22_token_ver%22%3A2%2C%22_token%22%3A%225545802841%3Ai2MlZYtSByn9IVBMuL8AHkCq0fX4HI7N%3A480fd4a05bfcdd081c70d0c4c4391883064a35cdd43402506937e611590d92a2%22%2C%22_platform%22%3A1%2C%22last_refreshed%22%3A1496528455.4560296535%7D; expires=Fri, 01-Sep-2017 22:20:55 GMT; HttpOnly; Max-Age=7776000; Path=/; Secure"
I can't do something like headers['Cookie'].split("; ") because the semicolon (;) appears at random parts of the cookie returned in the header.
So for example, when I submit the next request using the cookies returned from that request, it needs to look something like:
Cookie: csrftoken=hgsdco49G6E1EOyR48ofKhtG0P; ds_user=testuser; uid=55458041; mid=WTIRigABAAFjQJOKtJ3jaWGbi70P; rur=ATN; sessionid=a9c568a15f567fb8bf9752a3%3AIMAaCgikVRROWcqGu6aoBhJTx5VGutKh%3A%7B%22_auth_user_id%22%3A5545802841%2C%22_auth_user_backend%22%3A%22accounts.backends.CaseInsensitiveModelBackend%22%2C%22_auth_user_hash%22%3A%22%22%2C%22_token_ver%22%3A2%2C%22_token%22%3A%225545802841%3AyvzFW559ItJDPMOY9CyBja4NfW049qxy%3A4c5e7c48a20d3c8a1cbc4d0f9a0631449ff24c9b1dbc35a0f80f50568d0a3365%22%2C%22_platform%22%3A1%2C%22last_refreshed%22%3A1496453514.4971137047%7D
Any idea what the easiest way to achieve this would be?

Considerer that the cookie is separed by ";" and under the Path is separated by commas you can proceed working as follow:
pry(main)> cookie = headers['Cookie']
=> "ds_user=testuser; expires=Fri, 01-Sep-2017 22:20:55 GMT; Max-Age=7776000; Path=/, rur=ATN; Path=/, csrftoken=V6pcoCJdHJLb7BGu2BV8TwE5ZoA5fm; expires=Sat, 02-Jun-2018 22:20:55 GMT; Max-Age=31449600; Path=/; Secure, mid=WTM2RwABAAHw09zRkX55hIbjqszz; expires=Fri, 29-May-2037 22:20:55 GMT; Max-Age=630720000; Path=/, uid=554580241; expires=Fri, 01-Sep-2017 22:20:55 GMT; Max-Age=7776000; Path=/, sessionid=4be2b3d03caf0f3073ce8f1215a2d723a23981a52b0c%3AZ0Y0P0yRXSvpy2gBwZguWo4zoOpGajRA%3A%7B%22_auth_user_id%22%3A5545802841%2C%22_auth_user_backend%22%3A%22accounts.backends.CaseInsensitiveModelBackend%22%2C%22_auth_user_hash%22%3A%22%22%2C%22_token_ver%22%3A2%2C%22_token%22%3A%225545802841%3Ai2MlZYtSByn9IVBMuL8AHkCq0fX4HI7N%3A480fd4a05bfcdd081c70d0c4c4391883064a35cdd43402506937e611590d92a2%22%2C%22_platform%22%3A1%2C%22last_refreshed%22%3A1496528455.4560296535%7D; expires=Fri, 01-Sep-2017 22:20:55 GMT; HttpOnly; Max-Age=7776000; Path=/; Secure"
pry(main)> cookie.split(";").each do |f|
f =~ /^(.*?)=(.*?)$/
if $1 == " Path" then
puts "There is a #{$1} that contains #{$2}"
else
puts "#{$1} = #{$2}"
end
end
pry(main)* ds_user = testuser
expires = Fri, 01-Sep-2017 22:20:55 GMT
Max-Age = 7776000
There is a Path that contains /, rur=ATN
There is a Path that contains /, csrftoken=V6pcoCJdHJLb7BGu2BV8TwE5ZoA5fm
expires = Sat, 02-Jun-2018 22:20:55 GMT
Max-Age = 31449600
There is a Path that contains /
Secure, mid = WTM2RwABAAHw09zRkX55hIbjqszz
expires = Fri, 29-May-2037 22:20:55 GMT
Max-Age = 630720000
There is a Path that contains /, uid=554580241
expires = Fri, 01-Sep-2017 22:20:55 GMT
Max-Age = 7776000
There is a Path that contains /, sessionid=4be2b3d03caf0f3073ce8f1215a2d723a23981a52b0c%3AZ0Y0P0yRXSvpy2gBwZguWo4zoOpGajRA%3A%7B%22_auth_user_id%22%3A5545802841%2C%22_auth_user_backend%22%3A%22accounts.backends.CaseInsensitiveModelBackend%22%2C%22_auth_user_hash%22%3A%22%22%2C%22_token_ver%22%3A2%2C%22_token%22%3A%225545802841%3Ai2MlZYtSByn9IVBMuL8AHkCq0fX4HI7N%3A480fd4a05bfcdd081c70d0c4c4391883064a35cdd43402506937e611590d92a2%22%2C%22_platform%22%3A1%2C%22last_refreshed%22%3A1496528455.4560296535%7D
expires = Fri, 01-Sep-2017 22:20:55 GMT
=
Max-Age = 7776000
There is a Path that contains /
=
=> ["ds_user=testuser",
" expires=Fri, 01-Sep-2017 22:20:55 GMT",
" Max-Age=7776000",
" Path=/, rur=ATN",
" Path=/, csrftoken=V6pcoCJdHJLb7BGu2BV8TwE5ZoA5fm",
" expires=Sat, 02-Jun-2018 22:20:55 GMT",
" Max-Age=31449600",
" Path=/",
" Secure, mid=WTM2RwABAAHw09zRkX55hIbjqszz",
" expires=Fri, 29-May-2037 22:20:55 GMT",
" Max-Age=630720000",
" Path=/, uid=554580241",
" expires=Fri, 01-Sep-2017 22:20:55 GMT",
" Max-Age=7776000",
" Path=/, sessionid=4be2b3d03caf0f3073ce8f1215a2d723a23981a52b0c%3AZ0Y0P0yRXSvpy2gBwZguWo4zoOpGajRA%3A%7B%22_auth_user_id%22%3A5545802841%2C%22_auth_user_backend%22%3A%22accounts.backends.CaseInsensitiveModelBackend%22%2C%22_auth_user_hash%22%3A%22%22%2C%22_token_ver%22%3A2%2C%22_token%22%3A%225545802841%3Ai2MlZYtSByn9IVBMuL8AHkCq0fX4HI7N%3A480fd4a05bfcdd081c70d0c4c4391883064a35cdd43402506937e611590d92a2%22%2C%22_platform%22%3A1%2C%22last_refreshed%22%3A1496528455.4560296535%7D",
" expires=Fri, 01-Sep-2017 22:20:55 GMT",
" HttpOnly",
" Max-Age=7776000",
" Path=/",
" Secure"]
Be careful with the space after ; and proceed with whath you wnat inside the path

Related

Is it possible in Delphi to union enumerations into a larger enumeration?

Delphi can have enumerated types, e.g.:
type
TDay = (Mon, Tue, Wed, Thu, Fri, Sat, Sun); // Enumeration values
Is it possible to union enumerated types:
type
TWeekDay = (Mon, Tue, Wed, Thu, Fri);
TWeekendDay = (Sat, Sun);
TDay = (TWeekday, TWeekendDay); //hypothetical syntax
In reality, i need to decompose a large list into the disjoint items they actually are, without breaking source-code compatibility:
type
TWeekDay = (Mon, Tue, Wed, Thu, Fri);
TWeekendDay = (Sat, Sun);
TDay = (Mon, Tue, Wed, Thu, Fri, Sat, Sun); //identifier redeclared syntax error
And then change some variables:
Day: TWeekday;TDay;
Day: TWeekendDay;TDay
It's sort of the moral equivalent of strict typing. 🕗
The answer is "No".
But a workaround available to you, if subrages are contiguous, is to use subranges:
TDay = (Mon, Tue, Wed, Thu, Fri, Sat, Sun);
TWeekDay2 = Mon..Fri;
TWeekday = type TWeekDay2;
TWeekendDay2 = Sat..Sun;
TWeekendDay = type TWeekendDay;

Sum the next value of hash in an array of hashes

I have an array of hashes like this:
[{Mon, 09 May 2016 14:49:17 UTC +00:00=>12},
{Sun, 17 Apr 2016 14:08:40 UTC +00:00=>30},
{Sun, 16 Apr 2016 14:08:40 UTC +00:00=>18},
{Sun, 15 Apr 2016 14:03:33 UTC +00:00=>21}]
How can I sum the previous value from the oldest date to the current date, my expected output will be:
[{Mon, 09 May 2016 14:49:17 UTC +00:00=>81},
{Sun, 17 Apr 2016 14:08:40 UTC +00:00=>69},
{Sun, 16 Apr 2016 14:08:40 UTC +00:00=>39},
{Sun, 15 Apr 2016 14:03:33 UTC +00:00=>21}]
Thanks!
Assuming that the key of every hash in your array is a DateTime object, you can get what your want with this:
balance = [
{DateTime.parse('Mon, 09 May 2016 14:49:17 UTC +00:00')=>12},
{DateTime.parse('Sun, 17 Apr 2016 14:08:40 UTC +00:00')=>30},
{DateTime.parse('Sun, 16 Apr 2016 14:08:40 UTC +00:00')=>18},
{DateTime.parse('Sun, 15 Apr 2016 14:03:33 UTC +00:00')=>21}
] # => your original array
# Get expected array.
balance.map{ |h|
{
h.keys.first => balance.select{ |e|
e.keys.first <= h.keys.first }.map{ |s|
s[s.keys.first] }.reduce(:+)
}
}
I split the code in lines in order to improve readability.
Another approach would be to sort the array first and then use the map function to keep a running total to collect the required data.
# sort the balances by date
balance = balance.sort {|a, b| a.keys.first <=> b.keys.first }
# get running total and collect for each date
total = 0
balance.map do |entry|
date, value = entry.first
total += value
{date => total}
end
I assume your array is in lastest-to-earliest date order and looks something like arr below:
a = [{ "Mon, 09 May 2016 14:49:17 UTC +00:00"=>12 },
{ "Sun, 17 Apr 2016 14:08:40 UTC +00:00"=>30 },
{ "Sun, 16 Apr 2016 14:08:40 UTC +00:00"=>18 },
{ "Sun, 15 Apr 2016 14:03:33 UTC +00:00"=>21 }]
require 'date'
arr = a.map do |h|
(d, v) = h.to_a.first
{ DateTime.parse(d) => v }
end
#=> [{#<DateTime: 2016-05-09T14:49:17+00:00 ((2457518j,53357s,0n),+0s,2299161j)>=>12},
# {#<DateTime: 2016-04-17T14:08:40+00:00 ((2457496j,50920s,0n),+0s,2299161j)>=>30},
# {#<DateTime: 2016-04-16T14:08:40+00:00 ((2457495j,50920s,0n),+0s,2299161j)>=>18},
# {#<DateTime: 2016-04-15T14:03:33+00:00 ((2457494j,50613s,0n),+0s,2299161j)>=>21}]
We can then compute the required array as follows.
cumv = 0
arr.reverse.
map { |h| h.to_a.first }.
each_with_object([]) do |(d,v),a|
cumv += v
a << { d => cumv }
end.
reverse
#=> [{#<DateTime: 2016-05-09T14:49:17+00:00 ((2457518j,53357s,0n),+0s,2299161j)>=>81},
# {#<DateTime: 2016-04-17T14:08:40+00:00 ((2457496j,50920s,0n),+0s,2299161j)>=>69},
# {#<DateTime: 2016-04-16T14:08:40+00:00 ((2457495j,50920s,0n),+0s,2299161j)>=>39},
# {#<DateTime: 2016-04-15T14:03:33+00:00 ((2457494j,50613s,0n),+0s,2299161j)>=>21}]

How to print my hash value if it contains time stamp values?

I have a hash in ruby i want to print it in proper manner using hash objects like hash.each_pair(). But its not working as it has some time stamp values.
file1={:file_modify_date=>2015-01-08 12:34:34 +0530, :file_modify_date_civil=>Thu, 08 Jan 2015, :file_access_date=>2015-01-08 13:23:09 +0530, :file_access_date_civil=>Thu, 08 Jan 2015, :file_inode_change_date=>2015-01-08 12:34:34 +0530, :file_inode_change_date_civil=>Thu, 08 Jan 2015, :file_permissions=>"rw-r--r--", :file_type=>"JPEG", :mime_type=>"image/jpeg"}
file1.each_pair { |k, v| puts "Key: #{k}, Value: #{v}" }
How about you format your time_stamp value to string
file1={:file_modify_date=> time1.strftime("%Y-%d-%m %H:%M:%S +%z"), ....}
using strftime to format your time stamp
file1.each_pair { |k, v| puts "Key: #{k}, Value: #{v}" }
see also Rails strftime
Try following
file1={:file_modify_date=>'2015-01-08 12:34:34 +0530', :file_modify_date_civil=>'Thu, 08 Jan 2015', :file_access_date=>'2015-01-08 13:23:09 +0530', :file_access_date_civil=>'Thu, 08 Jan 2015', :file_inode_change_date=>'2015-01-08 12:34:34 +0530', :file_inode_change_date_civil=>'Thu, 08 Jan 2015', :file_permissions=>"rw-r--r--", :file_type=>"JPEG", :mime_type=>"image/jpeg"}
file1.each_pair { |k, v| puts "Key: #{k}, Value: #{v}" }

Get RTMP uri to Youtube LiveStream video

Can I get RTMP uri of Youtube live-stream using live-stream ID with Youtube Data API v3? For example, for this channel: https://www.youtube.com/watch?v=4YKR-rEwOr8
When I try to do this using "try" page https://developers.google.com/youtube/v3/live/docs/liveStreams/list (part = "status,cdn", id = "4YKR-rEwOr8") I got empty list:
200 OK
- Hide headers -
Cache-Control: private, max-age=0, must-revalidate, no-transform
Content-Encoding: gzip
Content-Length: 183
Content-Type: application/json; charset=UTF-8
Date: Mon, 27 Oct 2014 11:10:54 GMT
Etag: "PSjn-HSKiX6orvNhGZvglLI2lvk/HzQflIssFHeC_PS9MZIweemMYZg"
Expires: Mon, 27 Oct 2014 11:10:54 GMT
Server: GSE
{
"kind": "youtube#liveStreamListResponse",
"etag": "\"PSjn-HSKiX6orvNhGZvglLI2lvk/HzQflIssFHeC_PS9MZIweemMYZg\"",
"pageInfo": {
"totalResults": 0,
"resultsPerPage": 0
},
"items": [
]
}
What I do wrong? I really don't understand.

UDP HTTP Header parsing in Ruby

I get the following response from a a UDP Socket:
HTTP/1.1 200 OK
CACHE-CONTROL: max-age=1800
DATE: Thu, 08 Nov 2012 12:32:33 GMT
EXT:
LOCATION: http://192.168.0.100:49153/nmrDescription.xml
SERVER: Windows2000/0.0 UPnP/1.0 PhilipsIntelSDK/1.4 DLNADOC/1.50
X-User-Agent: redsonic
ST: upnp:rootdevice
USN: uuid:F00DBABE-SA5E-BABA-DADA188ED55ED539::upnp:rootdevice
Is there a library, or simple way to parse all this ?
I'd like to get something like the following:
{
:cache_control => "max-age=1800"
:date => "Thu, 08 Nov 2012 12:32:33 GMT"
:ext => nil
:location => "http://192.168.0.100:49153/nmrDescription.xml"
# Etc.
}
response.split($/).drop(1).inject({}) do |h, l|
k, v = l.split(": ", 2)
h[k.downcase.gsub("-", "_").to_sym] = (v unless v.empty?)
h
end
# => {
# :cache_control=>"max-age=1800",
# :date=>"Thu, 08 Nov 2012 12:32:33 GMT",
# :ext=>nil,
# :location=>"http://192.168.0.100:49153/nmrDescription.xml",
# :server=>"Windows2000/0.0 UPnP/1.0 PhilipsIntelSDK/1.4 DLNADOC/1.50",
# :x_user_agent=>"redsonic",
# :st=>"upnp:rootdevice",
# :usn=>"uuid:F00DBABE-SA5E-BABA-DADA188ED55ED539::upnp:rootdevice"
#}

Resources