Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get words from DB #19

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions api/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }

gem "sinatra"
gem "json"
gem "mysql2"
2 changes: 2 additions & 0 deletions api/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -20,6 +21,7 @@ PLATFORMS

DEPENDENCIES
json
mysql2
sinatra

BUNDLED WITH
Expand Down
13 changes: 12 additions & 1 deletion api/api.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'sinatra'
require 'json'
require 'mysql2'

set :bind, '0.0.0.0'

Expand All @@ -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']
)
Comment on lines +17 to +22
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SQLの接続をリクエストを受けるたびに毎回するのは良くない気がします。
clientはグローバル変数でも良いかもしれないですね。


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