[英]Ruby on Rails: How may create a quoted string to a request header?
I am writing from scratch a Twitter client and one of the requisites is not using Twitter gems, so I must create my own requests. 我正在从头开始编写Twitter客户端,但前提之一是不使用Twitter gems,因此必须创建自己的请求。
Twitter API documentation says here that I must have a Authorization header like this: Twitter API文档在这里说,我必须具有这样的Authorization标头:
Authorization:
OAuth oauth_consumer_key="xvz1evFS4wEEPTGEFPHBog",
oauth_nonce="kYjzVBB8Y0ZFabxSWbWovY3uYSQ2pTgmZeNu2VS4cg",
oauth_signature="tnnArxj06cWHq44gCs1OSKk%2FjLY%3D",
oauth_signature_method="HMAC-SHA1",
oauth_timestamp="1318622958",
oauth_token="370773112-GmHxMAgYyLbNEtIKZeRNFsMKPR9EyMZeS9weJAEb",
oauth_version="1.0"
As you may see I must have something like oauth_consumer_key="xvz1evFS4wEEPTGEFPHBog"
with the second part in quotes. 如您所见,我必须使用类似oauth_consumer_key="xvz1evFS4wEEPTGEFPHBog"
,并将第二部分用引号引起来。 I tried using %Q like in 我尝试使用像
["oauth_consumer_key",%Q( Figaro.env.TWITTER_ACCESS_TOKEN )].join('=')
assuming %Q would return a quoted string. 假设%Q将返回带引号的字符串。 but when I inspect the value, all I get is 但是当我检查价值时,我得到的只是
oauth_consumer_key=xvz1evFS4wEEPTGEFPHBog
which, obviously, is different from the required result. 显然,这与所需结果不同。
What am I missing? 我想念什么?
%Q(x)
is basically the same as "x". %Q(x)
与“ x”基本相同。
To achieve the desired result you have to manually introduce quotes into %Q
expression, like this: %Q("#{Figaro.env.TWITTER_ACCESS_TOKEN}")
为了获得所需的结果,您必须手动将引号引入%Q
表达式中,例如: %Q("#{Figaro.env.TWITTER_ACCESS_TOKEN}")
1. My solution: 1.我的解决方案:
'oauth_consumer_key="#{Figaro.env.TWITTER_ACCESS_TOKEN}"'
2. Why: 2.为什么:
%Q()
basically replaces the variable with double quotes ""
, it is literally the same as if you wrote "Figaro.env.TWITTER_ACCESS_TOKEN"
%Q()
基本上用双引号""
替换变量,这实际上与您编写"Figaro.env.TWITTER_ACCESS_TOKEN"
In fact, to display the content of a variable, you have to use interpolation instead of the name itself, using "#{var}"
. 实际上,要显示变量的内容 ,必须使用"#{var}"
使用插值法而不是名称本身。
You can also use %Q
directly with interpolation using %Q{var}
(note {}
instead of ()
). 您也可以将%Q
直接用于%Q{var}
插值(注意{}
代替()
)。
Your problem is elsewhere: with the join() method. 您的问题在其他地方:使用join()方法。 It's getting rid of double quotes. 它消除了双引号。 In that case doing ["var", var].join('=')
and ["var", %Q{var}].join('=')
ends doing exactly the same thing but more complicated. 在那种情况下,执行["var", var].join('=')
和["var", %Q{var}].join('=')
会做完全相同的事情,但是更加复杂。
@Artem Ignatiev's solution should works . @Artem Ignatiev的解决方案应该有效 。 But if you need to be more readable, you don't even need to join, imho, it makes sense only when you are using more than two variables. 但是,如果您需要更具可读性,甚至不需要加入,恕我直言,仅当您使用两个以上的变量时,它才有意义。
For instance, I will use something like 'oauth_consumer_key="#{var}"'
mixing single and double quote to make sure it causes no confusions. 例如,我将使用诸如'oauth_consumer_key="#{var}"'
混合单引号和双引号来确保不会引起混淆。
If you do need to use %Q()
and join
, you can still use ["oauth_consumer_key", %Q("#{Figaro.env.TWITTER_ACCESS_TOKEN})].join('=')
. 如果确实需要使用%Q()
和join
,则仍然可以使用["oauth_consumer_key", %Q("#{Figaro.env.TWITTER_ACCESS_TOKEN})].join('=')
。
join
you can use single quote interpolation %q
or double quote interpolation %Q
without affecting the ends results: 请注意,由于join
您可以使用单引号内插%q
或双引号内插%Q
而不影响最终结果: eg 例如
['oauth_consumer_key', %q("#{var}")].join('=') == ["oauth_consumer_key", %Q("#{var}")].join('=')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.