Skip to content

Commit

Permalink
Add download feed button to hello homepage
Browse files Browse the repository at this point in the history
  • Loading branch information
davidesantangelo committed Mar 8, 2023
1 parent e96bfaa commit ea79632
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 12 deletions.
2 changes: 1 addition & 1 deletion app/assets/builds/tailwind.css

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions app/controllers/hello_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ def index
rails
].sample
@code = "curl -G -H \"Authorization: Token #{@token}\" #{@url}/search.json -d \"q=#{@q}\""
@feeds = Feed.all
end

def download
respond_to do |format|
format.csv do
send_data Feed.to_csv, filename: "feeds-#{Time.zone.now.to_date}.csv"
end
end
end

private
Expand Down
12 changes: 12 additions & 0 deletions app/models/feed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ class Feed < ApplicationRecord
attribute :title, :description, :language, :url
end

def self.to_csv
attributes = %w[url title description language items_count]

CSV.generate(headers: true) do |csv|
csv << attributes

all.each do |feed|
csv << attributes.map { |attr| feed.send(attr) }
end
end
end

def store!
task = tasks.enqueued.create!(task_type: :feed_store, enqueued_at: Time.current)

Expand Down
19 changes: 9 additions & 10 deletions app/views/hello/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,22 @@
</div>
</div>
<div class="relative">
<pre class="text-sm leading-6 text-slate-50 flex ligatures-none overflow-auto"><code class="flex-none min-w-full p-5"><span class="flex"><svg viewBox="0 -9 3 24" aria-hidden="true" class="flex-none overflow-visible text-pink-400 w-auto h-6 mr-3"><path d="M0 0L3 3L0 6" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"></path></svg><span class="flex-auto">GET /search.json -d 'q={q}'</span></span></code></pre>
<pre class="text-sm leading-6 text-slate-50 flex ligatures-none overflow-auto"><code class="flex-none min-w-full p-5"><span class="flex"><svg viewBox="0 -9 3 24" aria-hidden="true" class="flex-none overflow-visible text-pink-400 w-auto h-6 mr-3"><path d="M0 0L3 3L0 6" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"></path></svg><span class="flex-auto"><span class="font-semibold">GET</span> /search.json -d 'q={q}'</span></span></code></pre>
</div>
</div>
<div class="my-0.5">
<%= link_to download_hello_index_path(format: 'csv'), class: "text-xs text-gray-800 hover:underline hover:text-sky-700" do %>
<%= @feeds.size %> feeds
<% end %>
</div>
<div class="mt-6 flex items-center justify-center gap-x-6">
<a href="https://github.com/searq/searq.org" class="rounded-md bg-indigo-600 px-3.5 py-2.5 text-sm font-semibold text-white shadow-sm hover:bg-indigo-500 visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600">
<a href="https://github.com/searq/searq.org" class="rounded-md bg-indigo-600 px-6 py-2.5 text-sm font-semibold text-white shadow-sm hover:bg-indigo-500 visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600">
<i class="fa-brands fa-github mr-1"></i>
GitHub
</a>
<a class="text-sm font-semibold text-gray-900" href="https://github.com/searq/searq.org/wiki">
<a class="text-sm font-semibold text-gray-900 hover:underline hover:text-gray-700" href="https://github.com/searq/searq.org/wiki">
docs <span aria-hidden="true">&rarr;</span>
</a>
</div>
</div>
<footer class="bg-white absolute bottom-0 right-0 left-0 text-center">
<div class="mx-auto max-w-7xl py-6 px-6 lg:px-8">
<div class="mt-8 md:order-1 md:mt-0">
<p class="text-center text-sm leading-5 text-gray-800">Made by <a href="https://twitter.com/daviducolo" class="text-indigo-800 hover:underline">Davide Santangelo</a></p>
</div>
</div>
</footer>

16 changes: 16 additions & 0 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,21 @@
<main class="grid min-h-full place-items-center bg-white px-4">
<%= yield %>
</main>
<footer class="bg-white absolute bottom-0 right-0 left-0 text-center">
<div class="mx-auto max-w-7xl py-6 px-6 lg:px-8">
<div class="mt-8 md:order-1 md:mt-0">
<p class="text-center text-sm leading-5 text-gray-800">Made by <a href="https://twitter.com/daviducolo" class="text-indigo-700 hover:underline">Davide Santangelo</a></p>
</div>
</div>
</footer>
<script src='https://storage.ko-fi.com/cdn/scripts/overlay-widget.js'></script>
<script>
kofiWidgetOverlay.draw('davidesantangelo', {
'type': 'floating-chat',
'floating-chat.donateButton.text': 'Tip Me',
'floating-chat.donateButton.background-color': '#794bc4',
'floating-chat.donateButton.text-color': '#fff'
});
</script>
</body>
</html>
6 changes: 5 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
Rails.application.routes.draw do
mount Sidekiq::Web => '/sidekiq'

resources :hello, only: %i[index]
resources :hello, only: %i[index] do
collection do
get :download
end
end

root 'hello#index'

Expand Down
18 changes: 18 additions & 0 deletions spec/models/feed_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,22 @@
end
end
end

describe 'class methods' do
describe '.to_csv' do
let!(:feeds) { create_list(:feed, 3) }

it 'returns a CSV string' do
expect(Feed.to_csv).to be_a(String)
end
end

describe '.to_synchronize' do
let!(:feed) { create(:feed, synchronized_at: 1.day.ago) }

it 'returns feeds to synchronize' do
expect(Feed.to_synchronize).to include(feed)
end
end
end
end

0 comments on commit ea79632

Please sign in to comment.