Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Bakh Inamov committed Aug 28, 2017
0 parents commit 763f71a
Show file tree
Hide file tree
Showing 7 changed files with 301 additions and 0 deletions.
24 changes: 24 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Copyright 2015 Google, Inc
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# [START dependencies]
source "https://rubygems.org"

gem "sinatra"
gem "google-cloud-datastore"
# [END dependencies]

group :test do
gem "rspec"
end
83 changes: 83 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
GEM
remote: https://rubygems.org/
specs:
addressable (2.5.0)
public_suffix (~> 2.0, >= 2.0.2)
diff-lcs (1.2.5)
faraday (0.9.2)
multipart-post (>= 1.2, < 3)
google-cloud-core (0.21.0)
google-cloud-datastore (0.21.0)
google-cloud-core (~> 0.21.0)
google-gax (~> 0.6.0)
google-protobuf (~> 3.0)
googleapis-common-protos (~> 1.3)
grpc (~> 1.0)
google-gax (0.6.0)
googleapis-common-protos (~> 1.3.1)
googleauth (~> 0.5.1)
grpc (~> 1.0)
rly (~> 0.2.3)
google-protobuf (3.0.2)
googleapis-common-protos (1.3.4)
google-protobuf (~> 3.0)
grpc (~> 1.0)
googleauth (0.5.1)
faraday (~> 0.9)
jwt (~> 1.4)
logging (~> 2.0)
memoist (~> 0.12)
multi_json (~> 1.11)
os (~> 0.9)
signet (~> 0.7)
grpc (1.0.1)
google-protobuf (~> 3.0.2)
googleauth (~> 0.5.1)
jwt (1.5.6)
little-plugger (1.1.4)
logging (2.1.0)
little-plugger (~> 1.1)
multi_json (~> 1.10)
memoist (0.15.0)
multi_json (1.12.1)
multipart-post (2.0.0)
os (0.9.6)
public_suffix (2.0.4)
rack (1.6.4)
rack-protection (1.5.3)
rack
rly (0.2.3)
rspec (3.5.0)
rspec-core (~> 3.5.0)
rspec-expectations (~> 3.5.0)
rspec-mocks (~> 3.5.0)
rspec-core (3.5.4)
rspec-support (~> 3.5.0)
rspec-expectations (3.5.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.5.0)
rspec-mocks (3.5.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.5.0)
rspec-support (3.5.0)
signet (0.7.3)
addressable (~> 2.3)
faraday (~> 0.9)
jwt (~> 1.5)
multi_json (~> 1.10)
sinatra (1.4.7)
rack (~> 1.5)
rack-protection (~> 1.4)
tilt (>= 1.3, < 3)
tilt (2.0.5)

PLATFORMS
ruby

DEPENDENCIES
google-cloud-datastore
rspec
sinatra

BUNDLED WITH
1.13.4
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# App Engine Flexible Environment - Cloud Datastore Sample

## Application code

The sample application logs, retrieves, and displays visitor IPs. You
can see that a visit is a Cloud Datastore entity of kind `Visit`, and
is saved using the Dataset `save` method. Then, the ten most recent
visits are retrieved in descending order by building a Query, and
using the Dataset `run` method.

## Run

Make sure `gcloud` is installed and authenticated. You can find your
project id with `gcloud config list`.


```
export GOOGLE_CLOUD_PROJECT=<your-project-id>
bundle
bundle exec ruby ./app.rb
```

## Deploy

```
gcloud app deploy
```

## Test

Note: the tests do a live deploy to App Engine

Put a `client_secrets.json` file for a service account in the root of
your repo.

```
gcloud auth activate-service-account --key-file ../../client_secrets.json
export TEST_DIR=appengine/datastore/
bundle exec rspec
```
80 changes: 80 additions & 0 deletions app.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#[START all]
require "digest/sha2"
require "sinatra"
require "google/cloud/datastore"

get '/' do
erb :lookup
end

get "/search" do
project_id = "b-a-k-h"
datastore = Google::Cloud::Datastore.new
query = datastore.query("Product").order("name", :asc).limit(15).
where("name", ">=", params[:term])
products = datastore.run query

names = Array.new

products.each do |product|
names << product['name']
end

response.write names

content_type :json #"text/plain"
status 200
end

get '/_ah/health' do
status 200
end

#[END all]
__END__

@@ layout
<html>
<head>
<title>awesome lookup app</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="shortcut icon" type="image/png" href="https://storage.googleapis.com/bakh-bucket/favicon.ico"/>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
</head>
<body><%= yield %></body>
</html>

@@ lookup
<h1 align="center">awesome lookup app</h1>
<div align="center" class="ui-widget">
<label for="products">products: </label>
<input id="products" placeholder="start typing">
</div>

<div align="center" class="ui-widget" style="margin-top:2em; font-family:Arial">
Selection:
<div id="log" style="height: 200px; width: 300px; overflow: auto;" class="ui-widget-content"></div>
<div><img src="https://storage.googleapis.com/bakh-bucket/150px-RainbowDashFilly.png" alt="rainbow dash"></div>
</div>

<script>
$( function() {
function log( message ) {
$( "<div>" ).text( message ).prependTo( "#log" );
$( "#log" ).scrollTop( 0 );
}

$( "#products" ).autocomplete({
source: "search",
minLength: 2,
select: function( event, ui ) {
log( "Selected: " + ui.item.value + " aka " + ui.item.id );
}
});
} );

</script>
19 changes: 19 additions & 0 deletions app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright 2015 Google, Inc
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

#[START all]
runtime: ruby
env: flex
entrypoint: bundle exec ruby app.rb
#[END all]
22 changes: 22 additions & 0 deletions data_import.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Imports the Google Cloud client library
require "google/cloud/datastore"
require 'json'

# Your Google Cloud Platform project ID
project_id = "b-a-k-h"

# Instantiates a client
datastore = Google::Cloud::Datastore.new

fileinput = File.read('products.json')
products = JSON.parse(fileinput)
products.each do | product |
entry = datastore.entity 'Product' do |e|
e['sku'] = product['sku']
e['name'] = product['name']
e['type'] = product['type']
e['price'] = product['price']
e['upc'] = product['upc']
end
datastore.save entry
end
33 changes: 33 additions & 0 deletions spec/datastore_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Copyright 2015 Google, Inc
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

require File.expand_path("../../../../spec/e2e", __FILE__)
require "rspec"
require "net/http"

describe "Datastore E2E test" do
before do
skip "End-to-end tests skipped" unless E2E.run?

@url = E2E.url
end

it "returns what we expect" do
uri = URI.parse(@url)
response = Net::HTTP.get(uri)
expect(response).to include("Last 10 visits:")
expect(response).to include("Time:")
expect(response).to include("UTC Addr:")
end
end

0 comments on commit 763f71a

Please sign in to comment.