0

Here's the python code:

with open('config.json') as json_data_file:
       configuration = json.load(json_data_file)
       access_token=configuration["canvas"]["access_token"]
       baseUrl="https://"+configuration["canvas"]["host"]+"/api/v1/courses/"

header = {'Authorization' : 'Bearer ' + access_token}

I'm trying to write some code in ruby that does the exact same thing as the python code above. This is how I wrote it:

File.open("config.json") {|json_data_file|
  configuration = json.load(json_data_file)
  access_token=configuration["canvas"]["access_token"]
  baseUrl="https://"+configuration["canvas"]["host"]+"/api/v1/courses/"
}

header = {'Authorization': 'Bearer ' + access_token}

Is this the correct way to do it?

2
  • 2
    Is anything happening that suggests that it's not the correct way of doing it? Commented Jan 26, 2019 at 11:11
  • As of now, I can't really test it because I need a testing environment for the Canvas LMS. I have a colleague who has already set up a testing environment, but I want to make sure it's correct before I can send it to him for testing. Commented Jan 26, 2019 at 12:21

1 Answer 1

2

It's simpler than that. You use IO#read to return the file content as a string and then you use JSON.parse to convert JSON to a Ruby structure.

Say this is your config.json file:

{
  "canvas": {
    "access_token": "i like pie",
    "host": "ilikepie.com"
  }
}

And to access information from config.json in Ruby code:

# You need to require JSON
require 'json'

config_file = File.read('config.json')
config_hash = JSON.parse(config_file)

# Now you can access everything from `config_hash`
access_token = config_hash['canvas']['access_token'] # => 'i like pie'
host = config_hash['canvas']['host'] # => 'ilikepie.com'

Your final Ruby code should look something like this:

require 'json'

config = JSON.parse(File.read('config.json'))

access_token = config['canvas']['access_token']
host = config['canvas']['host']

# Use snake case in Ruby scripts. Also, use string interpolation, it's much clearer.
base_url = "https://#{host}/api/v1/courses/" # => 'https://ilikepie.com/api/v1/courses/'
Sign up to request clarification or add additional context in comments.

5 Comments

When I said "use snake case in Ruby scripts" I didn't mean you must use it, but you should.
Thanks, I'll give that a try when I have a testing environment up and running. But what about the ruby code that I wrote? Is that incorrect?
@BobK It is. First: you did not require 'json'. Second: json.load does not exist, it should be JSON, all capitals. Third: the load method (ruby-doc.org/stdlib-2.6/libdoc/json/rdoc/…) converts a Ruby structure to JSON and not JSON to a Ruby structure (that's JSON.parse job). Fourth: access_token and baseUrl are local to the block associated with File.open. That means you won't be able to access them once you exit the block.
I would be glad to add examples to my answer to show why it is incorrect.
I actually added a line to my python code above. It defines a header for the Rest API. Unfortunately I don't know the Ruby equivalent of it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.