Skip to content

Commit 60f205d

Browse files
author
James Moriarty
committed
feature: google cloud mvp.
1 parent d72d26f commit 60f205d

File tree

8 files changed

+185
-2
lines changed

8 files changed

+185
-2
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
*.json
12
/public/cache/
23
/.bundle/
34
/.yardoc

Gemfile

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ gem 'grape', '~> 0.11'
77
gem 'roar', '~> 1'
88
gem 'grape-roar', '~> 0.3'
99
gem 'aws-sdk', '~> 2'
10+
gem 'google-cloud-storage'
1011
gem 'mini_magick'
1112
gem 'puma'
1213

Gemfile.lock

+49
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ GEM
66
i18n (~> 0.7)
77
minitest (~> 5.1)
88
tzinfo (~> 1.1)
9+
addressable (2.5.0)
10+
public_suffix (~> 2.0, >= 2.0.2)
911
aws-sdk (2.5.5)
1012
aws-sdk-resources (= 2.5.5)
1113
aws-sdk-core (2.5.5)
@@ -26,9 +28,35 @@ GEM
2628
descendants_tracker (0.0.4)
2729
thread_safe (~> 0.3, >= 0.3.1)
2830
diff-lcs (1.2.5)
31+
digest-crc (0.4.1)
2932
docile (1.1.5)
3033
enumerable-lazy (0.0.1)
3134
equalizer (0.0.11)
35+
faraday (0.10.0)
36+
multipart-post (>= 1.2, < 3)
37+
google-api-client (0.9.20)
38+
addressable (~> 2.3)
39+
googleauth (~> 0.5)
40+
httpclient (~> 2.7)
41+
hurley (~> 0.1)
42+
memoist (~> 0.11)
43+
mime-types (>= 1.6)
44+
representable (~> 2.3.0)
45+
retriable (~> 2.0)
46+
google-cloud-core (0.21.1)
47+
googleauth (~> 0.5.1)
48+
google-cloud-storage (0.21.0)
49+
digest-crc (~> 0.4)
50+
google-api-client (~> 0.9.11)
51+
google-cloud-core (~> 0.21.0)
52+
googleauth (0.5.1)
53+
faraday (~> 0.9)
54+
jwt (~> 1.4)
55+
logging (~> 2.0)
56+
memoist (~> 0.12)
57+
multi_json (~> 1.11)
58+
os (~> 0.9)
59+
signet (~> 0.7)
3260
grape (0.17.0)
3361
activesupport
3462
builder
@@ -43,21 +71,35 @@ GEM
4371
grape
4472
roar (>= 1.0)
4573
hashie (3.4.4)
74+
httpclient (2.8.2.4)
75+
hurley (0.2)
4676
i18n (0.7.0)
4777
ice_nine (0.11.2)
4878
jmespath (1.3.1)
4979
json (1.8.3)
80+
jwt (1.5.6)
81+
little-plugger (1.1.4)
82+
logging (2.1.0)
83+
little-plugger (~> 1.1)
84+
multi_json (~> 1.10)
85+
memoist (0.15.0)
5086
method_source (0.8.2)
87+
mime-types (3.1)
88+
mime-types-data (~> 3.2015)
89+
mime-types-data (3.2016.0521)
5190
mini_magick (4.5.1)
5291
minitest (5.9.0)
5392
multi_json (1.12.1)
5493
multi_xml (0.5.5)
94+
multipart-post (2.0.0)
5595
mustermann19 (0.4.4)
5696
enumerable-lazy
97+
os (0.9.6)
5798
pry (0.10.3)
5899
coderay (~> 1.1.0)
59100
method_source (~> 0.8.1)
60101
slop (~> 3.4)
102+
public_suffix (2.0.4)
61103
puma (3.6.0)
62104
rack (1.6.4)
63105
rack-accept (0.4.5)
@@ -69,6 +111,7 @@ GEM
69111
rake (10.5.0)
70112
representable (2.3.0)
71113
uber (~> 0.0.7)
114+
retriable (2.1.0)
72115
roar (1.0.4)
73116
representable (>= 2.0.1, < 2.4.0)
74117
rspec (3.4.0)
@@ -84,6 +127,11 @@ GEM
84127
diff-lcs (>= 1.2.0, < 2.0)
85128
rspec-support (~> 3.4.0)
86129
rspec-support (3.4.1)
130+
signet (0.7.3)
131+
addressable (~> 2.3)
132+
faraday (~> 0.9)
133+
jwt (~> 1.5)
134+
multi_json (~> 1.10)
87135
simplecov (0.11.2)
88136
docile (~> 1.1.0)
89137
json (~> 1.8)
@@ -111,6 +159,7 @@ PLATFORMS
111159
DEPENDENCIES
112160
aws-sdk (~> 2)
113161
codeclimate-test-reporter
162+
google-cloud-storage
114163
grape (~> 0.11)
115164
grape-roar (~> 0.3)
116165
mini_magick

config.ru

+9-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,16 @@ $LOAD_PATH.unshift File.expand_path('../lib', __FILE__)
22

33
require "bundler/setup"
44
require "poto"
5-
require "poto/file_repository/s3"
5+
# require "poto/file_repository/s3"
6+
#
7+
# repository = Poto::FileRepository::S3.new(bucket: ENV["AWS_S3_BUCKET"])
68

7-
repository = Poto::FileRepository::S3.new(bucket: ENV["AWS_S3_BUCKET"])
9+
require "poto/file_repository/google/cloud/storage"
10+
client = Google::Cloud::Storage.new(
11+
project: "poto-1266",
12+
keyfile: "Poto-f69886a81372.json"
13+
)
14+
repository = Poto::FileRepository::Google::Cloud::Storage.new(bucket: "poto", client: client)
815

916
map("/") do
1017
run Poto::App

docker-compose.yml

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ web:
77

88
test:
99
build: .
10+
ports:
11+
- '9292:9292'
12+
environment:
13+
PORT: 9292
1014
volumes:
1115
- '.:/app'
1216
command: bundle exec rspec
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
require "base64"
2+
require "google/cloud/storage"
3+
4+
require "poto/file_repository/google/cloud/storage/file_collection_mapper"
5+
6+
module Poto
7+
module FileRepository
8+
module Google
9+
module Cloud
10+
class Storage
11+
attr_reader :bucket, :client
12+
13+
def initialize(bucket:, client:)
14+
@client = client
15+
@bucket = client.bucket(bucket)
16+
end
17+
18+
def url(id)
19+
bucket.file(decode(id)).signed_url
20+
end
21+
22+
def all(prefix:, page:, per_page:)
23+
files = bucket.files(prefix: prefix, token: page, max: per_page)
24+
25+
FileCollectionMapper.new(files, page).call
26+
end
27+
28+
private
29+
30+
def decode(value)
31+
URI.unescape(Base64.decode64(value))
32+
end
33+
end
34+
end
35+
end
36+
end
37+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
require "base64"
2+
3+
require "poto/file_repository/file_collection"
4+
require "poto/file_repository/google/cloud/storage/file_mapper"
5+
6+
module Poto
7+
module FileRepository
8+
module Google
9+
module Cloud
10+
class Storage
11+
class FileCollectionMapper
12+
attr_reader :files, :page
13+
14+
def initialize(files, page)
15+
@files = files
16+
@page = page
17+
end
18+
19+
def call
20+
FileCollection.new(_files, _page, next_page)
21+
end
22+
23+
private
24+
25+
def _files
26+
files.map { |object| FileMapper.new(object).call }
27+
end
28+
29+
def _page
30+
page
31+
end
32+
33+
def next_page
34+
files.token
35+
end
36+
end
37+
end
38+
end
39+
end
40+
end
41+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
require "base64"
2+
3+
require "poto/file_repository/file"
4+
5+
module Poto
6+
module FileRepository
7+
module Google
8+
module Cloud
9+
class Storage
10+
class FileMapper
11+
attr_reader :file
12+
13+
def initialize(file)
14+
@file = file
15+
end
16+
17+
def call
18+
File.new(id, name, size)
19+
end
20+
21+
private
22+
23+
def name
24+
file.name
25+
end
26+
27+
def size
28+
file.size
29+
end
30+
31+
def id
32+
encode file.name
33+
end
34+
35+
def encode(value)
36+
URI.escape Base64.encode64 name
37+
end
38+
end
39+
end
40+
end
41+
end
42+
end
43+
end

0 commit comments

Comments
 (0)