0
class Database
  include Cinch::Plugin

  DB = SQLite3::Database.new('development.sqlite3')
  match /(select .* from gears where .* like .*)/i 

  def execute(m)
    @db = SQLite3::Database.new('development.sqlite3')
    #m.reply @db.execute("select * from gears where lab like 'Primary'") 
  end
end

This part of an IRC bot. I am trying to have the matched regex that the user inputs directly inputed to the @db.execute to be able perform the query. Any help or suggestions for a different way to go would be appreciated.

aealvarez3
  • 49
  • 8
  • is there some reason you can't take the string the user supplied and just use that? What would be an example input from the user? – MCBama May 12 '15 at 18:09
  • I want to use regex because there could be multiple inputs. An example input would be "select * from gears where Lab like 'Primary'" another example would be "select * from gears where RU like 40". The differences are in the regex where the ".*" are. If I where to use a string instead i would have to code for all query possibilities instead of just a fill in the blank situation. @Micah – aealvarez3 May 12 '15 at 18:15
  • Alright, then it should be fairly simple. I assume `execute(m)` is the method we're working on where `m` is the string input? – MCBama May 12 '15 at 18:21
  • Yes "m.reply " is what is returned in the irc channel if a matching regex is inputed. – aealvarez3 May 12 '15 at 18:45

1 Answers1

1

Something along these lines should work:

def execute(m)
  @db = SQLite3::Database.new('development.sqlite3')
  input = m.input # or however you're getting the input into this function
  regex = /(select .* from gears where .* like .*)/i 
  db_query_string = regex.match(input).to_s
  m.reply @db.execute(db_query_string) 
end
MCBama
  • 1,432
  • 10
  • 18
  • No luck :(. "Match" needs to be called outside of the function. If i place it inside it does not recognize the match. – aealvarez3 May 12 '15 at 19:32
  • @aealvarez3 your match function seems strange to me then. Is it not the typical Ruby `Regex#match` function? – MCBama May 12 '15 at 19:37
  • I am not 100% positive but I want to say it is not. It is the command used in Cinch in order to match a user input into an IRC chat room and return whatever i specify. https://github.com/cinchrb/cinch – aealvarez3 May 12 '15 at 19:55
  • Ah! That explains my confusion. It's from `Cinch` which is not something I've used much. Lemme do some research. – MCBama May 12 '15 at 19:55
  • @aealvarez3 Ok, reading up on `Cinch` the `match` command is simply a listener that waits for a message to match the input then calls `execute(m)` using the message that was provided. `regex.match` is different from your other `match` function. As such, my example WILL work as long as you can get the inputted message at the point I mentioned. So the question is: can you get the user input within `execute(m)`? – MCBama May 12 '15 at 20:12
  • I can't atleast from what i tried it didn't work. I set `m.match /hello/i` inside of `execute(m)` and typed hello into the irc channel that the bot is monitoring and got no response. I also tried `match /hello/i` inside of `execute(m)` and still no luck. The match is only recognized if it is outside of `execute(m)`. – aealvarez3 May 12 '15 at 21:04
  • @aealvarez3 you're not understanding what I'm saying. Leave your `match` where it is, outside `execute(m)`. All that `match` command is is a listener for that particular phrase to be input by a user. Once a user inputs the phrase, it calls `execute(m)`. Now, you need something that looks like `m.user.input` that returns the exact string the user input. Once you have that, you're golden. – MCBama May 12 '15 at 21:14
  • Okay I get what you are saying now. But unfortunately i am not sure how to make it happen. I have tried setting a variable like `input = match /hello/i` but if i set it in a variable it no longer gets matched when a user inputs it. Do you have any suggestions on how i could make this happen. – aealvarez3 May 13 '15 at 15:05
  • @aealvarez3 Stop using match to try and get your input. It isn't going to work. it's going to be coming through `m`. most likely it's a method associated with `m.user` so if you need to just print out `m.user.methods` and see what your options are. There SHOULD be an API somewhere you can use. – MCBama May 13 '15 at 15:25
  • thanks for your help @Micah. I finally got it working based on your suggestions. – aealvarez3 May 18 '15 at 20:16
  • @aealvarez3 good to hear. Good luck on the rest of your project! – MCBama May 18 '15 at 20:25