diff --git a/api/Gemfile b/api/Gemfile index 2461a7a..09cbeef 100644 --- a/api/Gemfile +++ b/api/Gemfile @@ -6,3 +6,4 @@ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" } gem "sinatra" gem "json" +gem "mysql2" diff --git a/api/Gemfile.lock b/api/Gemfile.lock index 685c173..f84dd3f 100644 --- a/api/Gemfile.lock +++ b/api/Gemfile.lock @@ -4,6 +4,7 @@ GEM json (2.3.0) mustermann (1.1.1) ruby2_keywords (~> 0.0.1) + mysql2 (0.5.3) rack (2.2.2) rack-protection (2.0.8.1) rack @@ -20,6 +21,7 @@ PLATFORMS DEPENDENCIES json + mysql2 sinatra BUNDLED WITH diff --git a/api/api.rb b/api/api.rb index 39e6624..4fa8a5f 100644 --- a/api/api.rb +++ b/api/api.rb @@ -1,5 +1,6 @@ require 'sinatra' require 'json' +require 'mysql2' set :bind, '0.0.0.0' @@ -13,6 +14,16 @@ end def get_words - ary = [{name: "apple"}, {name: "banana"}] + client = Mysql2::Client.new( + database: ENV['MYSQL_DATABASE'], + host: ENV['HOST'], + username: ENV['MYSQL_USER'], + password: ENV['MYSQL_PASSWORD'] + ) + + sql = "select name from words order by rand() limit 10" + + ary = Array.new + client.query(sql).each {|row| ary << row} return ary.to_json end