Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have the URL http://localhost:3000/en/invest_offers?utf8=blahblahblah. How can I get ?utf8=blahblahblah part of the URL? Thanks!
Do as below using URI::Generic.query:
require 'uri'
URI("http://localhost:3000/en/invest_offers?utf8=blahblahblah").query
# => "utf8=blahblahblah"
Use URI::parse, URI::Generic#query:
require 'uri'
url = 'http://localhost:3000/en/invest_offers?utf8=blahblahblah'
URI.parse(url)
# => #<URI::HTTP:0x0000000213aae0 URL:http://localhost:3000/en/invest_offers?utf8=blahblahblah>
URI.parse(url).query
# => "utf8=blahblahblah"
Do the following:--
require 'uri'
url = 'http://localhost:3000/en/invest_offers?utf8=blahblahblah'
parsed_url = URI.parse(url)
if you just want to get string query part like "utf8=blahblahblah"
you should do
parsed_url.query
if you want get "blahblahblah",
you should just do
params["utf8"]
or else
p = CGI.parse(parsed_url.query)
# p is now {"utf8"=>["blahblahblah"]}
p["utf8"].first
#=> "blahblahblah"
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
def get_xml
path = "ddd-66252.pdf" // in public
way = File.basename(path)
diff = File.read(path)
render :xml => diff
end
How i can get file from path and need to look like file
Assuming you are using Ruby on Rails because you mention a public folder and have a render method in your code. In Ruby on Rails you can use send_file in your controller like this to send files to the browser:
def send_pdf
send_file(
Rails.root.join('public', 'ddd-66252.pdf'),
filename: 'ddd-66252.pdf',
type: 'application/pdf'
)
end
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I want to check if my url format is correct, it has some AWS acces keys etc:
/https://bucket.s3.amazonaws.com/path/file.txt?AWSAccessKeyId=[.+]&Expires=[.+]&Signature=[.+]/.match(url)
^ something like this. Could you please help?
URI RFC specifies this regular expression for parsing URLs and URIs:
^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?
You can also use the URI module from Ruby standard library:
require 'uri'
if url =~ /^#{URI::regexp(%w(http https))}$/
puts "it's an url alright"
else
puts "that's no url, that's a spaceship"
end
To check for the existence of "some AWS access keys etc" you can do:
require 'uri'
uri = URI.parse(url)
params = URI.decode_www_form(uri.query).to_h
if params.has_key?('AWSAccessKeyId')
unless params['AWSAccessKeyId'] =~ /\A[a-f0-9]{32}\z/
abort 'AWSAccessKeyId not valid'
end
else
abort 'AWSAccessKeyId required'
end
Of course you can just use regular expressions to parse them directly but it gets ugly because the order of the parameters may be different:
>> url = "https://bucket.s3.amazonaws.com/path/file.txt?AWSAccessKeyId=abcd12345&Expires=12345678&Signature=abcd"
>> matchdata = url.match(
/
\A
(?<scheme>http(?:s)?):\/\/
(?<host>[^\/]+)
(?<path>\/.+)\?
(?=.*(?:[\?\&]|\b)AWSAccessKeyId\=(?<aws_access_key_id>[a-f0-9]{1,32}))
(?=.*(?:[\?\&]|\b)Expires=(?<expires>[0-9]+))
/x
)
=> #<MatchData "https://bucket.s3.amazonaws.com/path/file.txt?"
scheme:"https"
host:"bucket.s3.amazonaws.com"
path:"/path/file.txt"
aws_access_key_id:"abcd12345"
expires:"12345678">
>> matchdata[:aws_access_key_id]
# => "abcd12345"
This uses
The positive lookahead of regex : (?=..) to ignore parameter
order
Ruby's regex named captures (?<param_name>.*) to identify
the params from match data
Non capturing groupings (?abcd|efgh)
The matcher (?[\&\?]|\b) to handle Expires=..., ?Expires=... or &Expires=...
And finally the /x free spacing modifier to
allow nicer formatting
We need a url to work with:
url = "/https://bucket.s3.amazonaws.com/path/file.txt?AWSAccessKeyId=somestuff&Expires=somemorestuff&Signature=evenmorestuff"
We also need to escape a bunch of stuff and do some non-greedy matching(.+?):
/https:\/\/bucket.s3.amazonaws.com\/path\/file\.txt\?AWSAccessKeyId=.+?&Expires=.+?&Signature=.+/.match(url)
=> #<MatchData "https://bucket.s3.amazonaws.com/path/file.txt?AWSAccessKeyId=somestuff&Expires=somemorestuff&Signature=evenmorestuff">
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a code:
require 'pp'
def unquote_string(string)
if (string.is_a?(String))
string.gsub(/\\/,'')
else
string
end
end
def filter_array_with_substitution_and_replacement(array,options={})
pp options
return array unless %w(filterRegex substitudeRegex replacementExpression).any? {|key| options.has_key? key}
puts "I will substitude!"
filterRegex = options["filterRegex"]
substitudeRegex = options["substitudeRegex"]
replacementExpression = options["replacementExpression"]
pp "I have: #{replacementExpression}"
array.select{|object|
object =~ filterRegex
}.map!{|object|
object.sub!(substitudeRegex,unquote_string(replacementExpression))
}
end
def sixth_function
array = %w(onetwo onethree onefour onesix none any other)
filterRegex = /one/
substitudeRegex = /(one)(\w+)/
replacementExpression = '/#{$2}.|.#{$1}/'
options = {
"filterRegex" => filterRegex,
"substitudeRegex" => substitudeRegex,
"replacementExpression" => replacementExpression
}
filter_array_with_substitution_and_replacement(array,options)
pp array
end
def MainWork
sixth_function()
end
MainWork()
Output:
{"filterRegex"=>/one/,
"substitudeRegex"=>/(one)(\w+)/,
"replacementExpression"=>"/\#{$2}.|.\#{$1}/"}
I will substitude!
"I have: /\#{$2}.|.\#{$1}/"
smb192168164:Scripts mac$ ruby rubyFirst.rb
{"filterRegex"=>/one/,
"substitudeRegex"=>/(one)(\w+)/,
"replacementExpression"=>"/\#{$2}.|.\#{$1}/"}
I will substitude!
"I have: /\#{$2}.|.\#{$1}/"
["/\#{$2}.|.\#{$1}/",
"/\#{$2}.|.\#{$1}/",
"/\#{$2}.|.\#{$1}/",
"/\#{$2}.|.\#{$1}/",
"none",
"any",
"other"]
It is not correct, because string with replacement have metacharacters quoted. how to correct unquote this string replacementExpression?
Desired output for array after replacement:
["two.|.one/",
"three.|.one/",
"four.|.one/",
"six.|.one/",
"none",
"any",
"other"]
Forget unquote_string - you simply want your replacementExpression to be '\2.|.\1':
"onetwo".sub(/(one)(\w+)/, '\2.|.\1')
#=> "two.|.one"
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I'm struggling with the following code:
I want a method to check if a string has content or not.
has_content = -> (a) { a!=nil && a.strip != ''}
c = ' '
has_content.call(c)
=> false
c.has_content
=> true
Why is the response different? Clearly I am lacking some Proc/lambdas knowledge.
I believe there is something missing in that code that is causing such behavior.
has_content is not defined for String, so unless you defined it before, it should raise an error
1.9.3p429 :002 > ''.has_content
NoMethodError: undefined method `has_content' for "":String
from (irb):2
from /Users/weppos/.rvm/rubies/ruby-1.9.3-p429/bin/irb:12:in `<main>'
As a side note, here's an alternative version of your code
has_content = ->(a) { !a.to_s.strip.empty? }
And here's an example
has_content.(nil)
# => false
has_content.('')
# => false
has_content.(' ')
# => false
has_content.('hello')
# => true
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How to add additional parameters to url from a hash? For example:
parameters = Hash.new
parameters["special"] = '25235'
parameters["code"] = 62346234
http: //127.0.0.1:8000/api/book_category/? %s parameters
require 'httparty'
require 'json'
response = HTTParty.get("http://127.0.0.1:8000/api/book_category/?")
json = JSON.parse(response.body)
puts json
The following should give you a valid URI which you can use for the json query.
require 'httparty'
parameters = {'special' => '512351235','code' => 6126236}
uri = URI.parse('http://127.0.0.1:8000/api/book_category/').tap do |uri|
uri.query = URI.encode_www_form parameters
end
uri.to_s
#=> "http://127.0.0.1:8000/api/book_category/?special=512351235&code=6126236"
The Tin Mans comment on your question is probably the better answer:
require 'httparty'
parameters = {'special' => '512351235','code' => 6126236}
response = HTTParty.get('http://127.0.0.1:8000/api/book_category/', :query => parameters)
json = JSON.parse(response.body)
puts json
The Addressable::URI class is an excellent replacement for the URI module in the standard library, and provides for just such manipulation of URI strings without having to build and escape the query string by hand.
This code demonstrates
require 'addressable/uri'
include Addressable
uri = URI.parse('http://127.0.0.1:8000/api/book_category/')
parametrs = {}
parametrs["special"] = '25235'
parametrs["code"] = 62346234
uri.query_values = parametrs
puts uri
output
http://127.0.0.1:8000/api/book_category/?code=62346234&special=25235