What is the best way to host a small ruby ​​app online?

I have a small ruby ​​application that I wrote that is anagram seeker. This is for learning ruby, but I would like to put it online for personal use. I have experience with Rails, and many of them have recommended Sinatra. I am fine too, but I cannot find any information on how to use a text file instead of a database.

The application is quite simple, checks the text file of the word list, then finds all the anagrams. I suppose this should be pretty simple, but I'm stuck on importing this text file into Rails (or Sinatra, if I choose that). In a Rails project, I put a text file in a directory lib.

Unfortunately, even though the path in Rails is correct, I get an error:

no such file to load -- /Users/court/Sites/cvtest/lib/english.txt 

( cvtest- name of the rails project)

Here is the code. It works great on its own:

file_path = '/Users/court/Sites/anagram/dictionary/english.txt'

input_string = gets.chomp


# validate input to list
if File.foreach(file_path) {|x| break x if x.chomp == input_string}

  #break down the word
  word = input_string.split(//).sort 

  # match word  
  anagrams = IO.readlines(file_path).partition{
    |line| line.strip!
    (line.size == word.size && line.split(//).sort == word)
  }[0] 

  #list all words except the original
    anagrams.each{ |matched_word| puts matched_word unless matched_word == input_string } 


#display error if 
else
  puts "This word cannot be found in the dictionary"
end
+3
source share
3 answers

Factor of actual functionality (anagram search) per method. Call this method from your web application.

In Rails, you must create a controller action that calls this method instead of ActiveRecord. In Sinatra, you simply create a route that calls the method. Here is an example of Sinatra:

get '/word/:input'
  anagrams = find_anagrams(params[:input])
  anagrams.join(", ")
end

Then, when you access http://yourapp.com/word/pool , it will print “loop, polo”.

+5
source

, , , , , , GET:

require 'rubygems'
require 'sinatra'

def find_anagrams word
  # your anagram method here
end

get '/anagram' do
  @word = params['word']
  @anagrams = find_anagrams @word if @word
  haml :anagram
end

haml ( , ). , , :

%h1 
  Enter a word
  %form{:action => "anagram"}
    %input{:type => "text", :name => "word"}
    %input{:type => "submit"}
- if @word
  %h1 
    Anagrams of 
    &= @word
  - if @anagrams
    %ul
      - @anagrams.each do |word|
        %li&= word
  - else
    %p No anagrams found
+5

With Sinatra, you can do anything. These examples do not even require a sinatra, you can collapse your own rack interface.

require 'rubygems'
require 'sinatra'
require 'yaml'

documents = YAML::load_file("your_data.yml")

Or:

require 'rubygems'
require 'sinatra'
content = Dir[File.join(__DIR__, "content/*.textile)].map {|path|
  content = RedCloth(File.read(path)).to_html
}

Etcetera.

+3
source

Source: https://habr.com/ru/post/1705746/


All Articles