How to fix ENV related openweather api_key - ruby-on-rails

I want to use openweather in my app.
It works good to write directly api_key.
However I introduce ENV, It won't work.
Anyone konws how to fix it?
quesiton is bellow
static_pages_controller.rb
...
uri = URI.parse('http://api.openweathermap.org/data/2.5/weather?q=Tokyo&appid=ENV['OPEN_WEATHER_API_KEY']')
json = Net::HTTP.get(uri)
res = JSON.parse(json)
#wind = res['wind']['speed']
#humidity = res['main']['humidity']
#clouds = res['clouds']['all']
...
.env
OPEN_WEATHER_API_KEY=20ab....
error code
/Users/sy/env2/ji-boys/app/controllers/static_pages_controller.rb:19:
syntax error,
unexpected tCONSTANT, expecting ')' ...appid=ENV['OPEN_WEATHER_API_KEY']') ... ^~~~~~~~~~~~~~~~~~~~
/Users/sy/env2/ji-boys/app/controllers/static_pages_controller.rb:19:
syntax error, unexpected ')', expecting end ...d=ENV['OPEN_WEATHER_API_KEY']') ... ^
I think ...appid=ENV['OPEN_WEATHER_API_KEY'].. is wrong.
Searching for how to write code, but can't find that.
Anyone knows this, please teach me how to fix it.
Thank you for reading this.

You need to do string interpolation to embed value
URI.parse("http://api.openweathermap.org/data/2.5/weather?q=Tokyo&appid=#{ENV['OPEN_WEATHER_API_KEY']}")

Related

what does fileURLToPath(import.meta.url) do?

Okay so i was following a mern stack tutorial and the tutor wrote out some lines of code but didnt really explain them well. this is the code:
const path = require('path')
const {fileURLToPath} = require('url')
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
app.use("/assets", express.static(path.join(__dirname, "public/assets")));
Now Im not stupid or a bad programmer. even though i dont really have an explanation for what the code is doing, i have a pretty good idea. the problem is that when i run my database, i get this error:
SyntaxError: Identifier '__filename' has already been declared
which is crazy because i havent used '__filename' anywhere else in the code. when i try to change '__filename' to 'filename' then i get this error:
SyntaxError: Cannot use 'import.meta' outside a module
Im so confused. Please can someone just tell me what the code does and why i am getting these errors and also how to fix the errors.
check your package.json() if it is having a
"type":"module"
if it has then remove it

Syntax error, unexpected tSTRING_BEG ROR

Below is my code:
gem_package 'cucumber' do
clear_sources true
source https://chefrubyaehq.kdc.example.com/
gem_binary '/opt/chef/embedded/bin/gem'
action :install
end
And I am getting the following error:
FATAL: zng937-test/recipes/default.rb:43: unknown regexp options - chfrbyahq
FATAL: zng937-test/recipes/default.rb:44: syntax error, unexpected tSTRING_BEG, expecting keyword_do or '{' or '('
FATAL: gem_binary '/opt/chef/embedded/bin/gem'
Does anyone have any idea why I would be getting this?
You need to put the source (https://chefrubyaehq.kdc.capitalone.com/) inside quotes to make it a string. Either single or double quotes is fine for this case.
Just as the message says.
Your regex //chefrubyaehq is invalid. Ruby regex only has i, o, x, m options.
You forgot to put a period after your regex /\n gem_binary '/ before the method opt.
And when you fix those, you will still encounter more errors.

ROBLOX Lua Error in script: '=' expected near '<eof>'

Hello I am writing a scipt on ROBLOX and I have encountered a problem.
function showVictoryMessage(playerName)
local message = Instance.new("Message")
message.Text = playerName .." has won!"
message.Parent = game.Workspace
wait (2)
message.Destroy()
end
Upon running this function, or more specifically the "message.Destroy" command, I get the error: Error in script: '=' expected near '< eof >'
I have never seen this error before, and the ROBLOX wiki page on Lua errors doesn't mention it.
I would very much appreciate help in this, because I don't personally know anyone who has coded in Lua.
Looks like a syntax error. message.Destroy() should be message:Destroy() according to this Roblox wiki page http://wiki.roblox.com/index.php?title=API:Class/Instance/Destroy
Also see the section Explosions, Messages, and More at URL http://wiki.roblox.com/index.php?title=Basic_Scripting which provides similar syntax using the colon (:) operator.
See also Difference between . and : in Lua and explanation of "possible side-effects of calculations/access are calculated only once" with colon notation.
Instead of message.Destroy() it should be message:Destroy()
Remember that '.' are used directory-wise and ":' are used for inbuilt functions.
WOOOOOOOO! It was a syntax error. The correct command is message:Destroy. Cause why would object.Destroy work and message.Destroy not?

Why is my JSON.parse failing? Ruby on Rails

I'm trying to load images from Flickr's API into a Ruby on Rails app, but I'm getting "Unexpected Token" on my JSON.parse() line.
I found another response here where the returned JSON had it's double quotes escaped out, and the solution was to add the .gsub thing to the end, but I'm still getting an error.
Anyone know what the problem is?
def add
#jsonresults = open("http://api.flickr.com/services/rest/?method=flickr.interestingness.getList&api_key=bb398c11934abb6d51bdd720020f6a4a&per_page=1&page=1&format=json&nojsoncallback=1").read
#images = JSON.parse(#jsonresults.to_json.gsub('\"', '"'))
end
The error:
JSON::ParserError in ImagesController#add
757: unexpected token at '"{"photos":{"page":1, "pages":500, "perpage":1, "total":500, "photo":[{"id":"8234011021", "owner":"24066605#N07", "secret":"b4c05df8c5", "server":"8341", "farm":9, "title":"Crescent Lake", "ispublic":1, "isfriend":0, "isfamily":0}]}, "stat":"ok"}"'
The json returned by the call looks fine. Change your parsing to this:
#images = JSON.parse(#jsonresults)
That is not valid JSON. The outer set of double-quotes do not belong. This is the valid version:
'{"photos":{"page":1, "pages":500, "perpage":1, "total":500, "photo":[{"id":"8234011021", "owner":"24066605#N07", "secret":"b4c05df8c5", "server":"8341", "farm":9, "title":"Crescent Lake", "ispublic":1, "isfriend":0, "isfamily":0}]}, "stat":"ok"}'

how to use getText function in cucumber with ruby-on-rails

I am trying to use getText function ,but its giving error.
assert_equal "Comment List", find(:xpath,'//div[#id='pageContainer']/div[2]/h2').getText()
and error is -
C:/Workspace/cucumber-project/features/step_definitions/comment_steps.rb:135: sy ntax error, unexpected ')', expecting kEND ...d='pageContainer']/div[2]/h2').text ^ (SyntaxError)
Can anybody help me in that, i just want to compare the text by finding through xpath with the expected text
This isn't a Cucumber issue, it's a Ruby syntax error. Your quotes are the problem, you need to do either:
"//div[#id='pageContainer']/div[2]/h2"
or
'//div[#id="pageContainer"]/div[2]/h2'

Resources