1

I have an action in a controller that sends an api call to a 3rd party app. Part of the payload is a js code in a string format that the app stores.

Here's an illustration:

def store_code_on_app
    myCode = "
        function hello(you){
            console.log("hello", you);
        }
    "

    RestClient.post(
    "http://theapp.com/codes/create",
        {
            code: myCode
        }
    )
end

Since my actual code is very long and to best manage multiple codes later on, I would like to store those codes in a file inside some folder in my rails app and call it from the controller. I would prefer to save the file with the appropriate extension (mycode.js) so it's easier to handle and debug.

How would you recommend I do that? Probably by requiring or including a file? perhaps in the lib folder?

5
  • shrug Just load the file and send it. If you're doing it a lot you'd probably want to cache it. Or keep it in a DB. Requiring a file means it has to be a Ruby file, and you'd still be writing code in strings--if these are things you keep locally wouldn't it be more fun to write them using actual syntax-aware editors? Commented Jul 11, 2019 at 23:19
  • @DaveNewton keeping it in the DB is an option, but I would prefer to get the string from a file and apply some string interpolation to it. I cannot send a file. I need to send a string. Unless is there a way to load the file and extract the code in it as a string? There must be a simple way to do this... Commented Jul 11, 2019 at 23:24
  • 1
    @Ben I edited my answer to include a solution for dynamic content if we want. I am not quite sure about that but still pretty confident that it might work also. Commented Jul 11, 2019 at 23:56
  • @rubyprince awesome! thanks. Commented Jul 12, 2019 at 0:02
  • ... A file is a string on disk. What do you think erb is? It's a templating system that reads a file, does interpolarion, then gets sent to the browser (usually as a gzipped byte sequence, but same difference). Commented Jul 12, 2019 at 1:14

1 Answer 1

2

You could save it anywhere and load it using File.read if you dont want any dynamic content.

lib/something/code.js

function hello(you){
   console.log("hello", you);
}

controller

def store_code_on_app
    myCode = File.read("#{Rails.root}/lib/something/code.js")

    RestClient.post(
    "http://theapp.com/codes/create",
        {
            code: myCode
        }
    )
end

If it is dynamic, you could use render_to_string but I am not sure about this, but something along this line might work

app/views/shared_js_templates/code.js.erb

function <%= console_string %>(you){
  console.log("<%= console_string %>", you);
}

controller

def store_code_on_app
    myCode = render_to_string(
      'shared_js_templates/code.js.erb',
      layout: false,
      locals: { console_string: 'hello' }
    )

    RestClient.post(
    "http://theapp.com/codes/create",
        {
            code: myCode
        }
    )
end

With dynamic, you could do stuff like:

app/views/shared_js_templates/code.js.erb

<% 10.times do |index| %>
  function hello<%= index %>(you){
    console.log("hello<%= index %>", you);
  }
<% end %>
Sign up to request clarification or add additional context in comments.

Comments

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.