corpshort 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ce1c5cb4260354eab2378678868bca210fe36dd8871011a3271218610288388c
4
- data.tar.gz: ec55ae522fc7e049ac7c038a25c74d6fe1c76b2018e3b666ebf27fbc0942feb2
3
+ metadata.gz: 05aa697749fa6aa8a335a07eca4da3e91aeb0ceb231b699d7e47a019c2894a2f
4
+ data.tar.gz: ffe3816d9caade8ba866f69c2fbe8f8145aecd6271ec47821d4a9c5a7f7c95e1
5
5
  SHA512:
6
- metadata.gz: b4aa037b354953e3fbef0a231c88808d65e1c2a46a6637090975c3640f91efa237a8454f1a8a853c3f9290584227657cb3cb3443df2a03a11209464ffad1e0a3
7
- data.tar.gz: 78d5510996b5c6221553bfd932f620b3b908cc776b6524f2ad26adff8c5fee2a66dc3aec8a5b55882768103114d2accf7562abec915d6a276f63e230b786fd15
6
+ metadata.gz: 39c7de94d9c1291c6b54eb7820947466d8e2a4bc4b660e553c14f7c2c32c95171ec7da50218f7ba920fab5d951842b9f1d4036eb5278465be18c37e20000bb3d
7
+ data.tar.gz: 3bd7f264425161ec818b349c8aae7b52d0305faf93f4484f919fdef96ee522d55e94054f72440d515ac13d5782e5a0d9efd29a9d8ae6411082dac7ea4a81e318
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- corpshort (0.1.0)
4
+ corpshort (0.2.0)
5
5
  aws-sdk-dynamodb
6
6
  erubi
7
7
  prawn
@@ -15,8 +15,8 @@ GEM
15
15
  remote: https://rubygems.org/
16
16
  specs:
17
17
  aws-eventstream (1.0.1)
18
- aws-partitions (1.91.0)
19
- aws-sdk-core (3.21.2)
18
+ aws-partitions (1.92.0)
19
+ aws-sdk-core (3.21.3)
20
20
  aws-eventstream (~> 1.0)
21
21
  aws-partitions (~> 1.0)
22
22
  aws-sigv4 (~> 1.0)
@@ -0,0 +1,11 @@
1
+ <p>Ooh! Link you've request doesn't exist. Create it for someone?</p>
2
+
3
+ <section class='new-link'>
4
+ <form action='/+/links' method='POST'>
5
+ <p><input type='text' name='url' placeholder='https://...' class='new-link-url' autocomplete=off></p>
6
+ <p class='new-link-down'>⬇︎</p>
7
+ <p><input type='text' name='linkname' placeholder='Choose a short name (Recommended)' class='new-link-name' autocomplete=off></p>
8
+ <p><input type='submit' value='Shorten'></p>
9
+ </form>
10
+ </section>
11
+
@@ -1,8 +1,8 @@
1
1
  <section class='new-link'>
2
2
  <form action='/+/links' method='POST'>
3
- <p><input type='text' name='url' placeholder='https://...' class='new-link-url'></p>
3
+ <p><input type='text' name='url' placeholder='https://...' value="<%= session[:last_form]&.dig(:url)&.sub(?", '') %>" class='new-link-url' autocomplete=off></p>
4
4
  <p class='new-link-down'>⬇︎</p>
5
- <p><input type='text' name='name' placeholder='Short name' class='new-link-name'></p>
5
+ <p><input type='text' name='linkname' placeholder='Choose a short name (Recommended)' value="<%= session[:last_form]&.dig(:linkname)&.sub(?", '') %>" class='new-link-name' autocomplete=off></p>
6
6
  <p><input type='submit' value='Shorten'></p>
7
7
  </form>
8
8
  </section>
@@ -86,6 +86,12 @@ module Corpshort
86
86
  @backend ||= conf.fetch(:backend)
87
87
  end
88
88
 
89
+ def random_name
90
+ chars_a = [*('A'..'Z')]
91
+ chars_b = [*('a'..'z'), *('0'..'9')]
92
+ [*1.times.map { |_| chars_a.sample }, *3.times.map { |_| chars_b.sample }].shuffle.join
93
+ end
94
+
89
95
  def link_name(name = params[:name])
90
96
  name.tr('_', '-')
91
97
  end
@@ -136,19 +142,40 @@ module Corpshort
136
142
  end
137
143
 
138
144
  post '/+/links' do
139
- unless params[:name] && params[:url]
145
+ unless params[:url]
140
146
  session[:error] = "Name and URL are required"
141
147
  redirect '/'
142
148
  end
143
149
 
150
+ name_given = params[:linkname] && !params[:linkname].strip.empty?
151
+ name = link_name(name_given ? params[:linkname] : random_name)
152
+ retries = 0
144
153
  begin
145
- link = Link.new({name: link_name, url: params[:url]})
154
+ link = Link.new({name: name, url: params[:url]})
146
155
  link.save!(backend, create_only: true)
147
- rescue Corpshort::Link::ValidationError, Corpshort::Backends::Base::ConflictError
156
+ rescue Corpshort::Link::ValidationError
157
+ session[:last_form] = {linkname: link.name, url: link.url}
148
158
  session[:error] = $!.message
149
159
  redirect '/'
160
+ rescue Corpshort::Backends::Base::ConflictError
161
+ if name_given
162
+ session[:last_form] = {linkname: link.name, url: link.url}
163
+ session[:error] = 'Link with the specified name already exists'
164
+ redirect '/'
165
+ else
166
+ name = link_name(random_name)
167
+ retries += 1
168
+ if retries > 20
169
+ session[:error] = 'Could not generate unique name. Try again later.'
170
+ redirect '/'
171
+ else
172
+ sleep 0.1
173
+ retry
174
+ end
175
+ end
150
176
  end
151
177
 
178
+ session[:last_form] = nil
152
179
  redirect "/#{link.name}+"
153
180
  end
154
181
 
@@ -333,10 +360,12 @@ module Corpshort
333
360
  name = name[0..-2]
334
361
  end
335
362
 
336
- @link = backend.get_link(link_name(name))
363
+ @link_name = link_name(name)
364
+ @link = backend.get_link(@link_name)
337
365
 
338
366
  unless @link
339
- halt 404, 'not found'
367
+ status 404
368
+ return erb(:'404')
340
369
  end
341
370
 
342
371
  if show
@@ -83,7 +83,7 @@ module Corpshort
83
83
  exclusive_start_key: last_key ? last_key : nil,
84
84
  key_condition_expression: 'updated_at_partition = :partition',
85
85
  expression_attribute_values: {":partition" => partition.strftime('%Y-%m')},
86
- limit: 1 || limit,
86
+ limit: limit,
87
87
  )
88
88
 
89
89
  unless result.items.empty?
@@ -94,6 +94,7 @@ module Corpshort
94
94
  last_key = nil
95
95
  sleep 0.05
96
96
  end
97
+ return [[], nil]
97
98
  end
98
99
 
99
100
  private
@@ -1,3 +1,3 @@
1
1
  module Corpshort
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: corpshort
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sorah Fukumori
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-06-19 00:00:00.000000000 Z
11
+ date: 2018-06-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sinatra
@@ -194,6 +194,7 @@ files:
194
194
  - README.md
195
195
  - Rakefile
196
196
  - app/public/style.css
197
+ - app/views/404.erb
197
198
  - app/views/edit.erb
198
199
  - app/views/index.erb
199
200
  - app/views/layout.erb