Skip to content

Commit

Permalink
add EventSource model, improve import rake task
Browse files Browse the repository at this point in the history
  • Loading branch information
fwolfst committed Feb 4, 2021
1 parent 0c01b24 commit e8337bd
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 21 deletions.
30 changes: 27 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,48 @@
Proof-of-concept rendering ical (ics) files with `simple_calendar` and Ruby on
Rails 6 using Ruby 3.

## Configuration

## Database creation

## Database initialization

## Configuration

Configure event sources:

```bash
# prefix with RAILS_ENV=production as needed
rails r 'EventSource.create!(name: "MyEventSource", url: "https://myseminardeskurl")'
```

## Tests

None yet.

## Data fetching and cache

Using a `rails` rake task.
Use a `rails` rake task:

```bash
rails sevencal:import_ics
```

Note that there is no error handling yet.

## Deployment instructions

Nothing fancy.

## License
## Contribution

Contributions are welcome, drop me a line / open an issue.

### License

Licensed under the [AGPLv3+](LICENSE.txt), copyright 2021 Felix Wolfsteller.

## Knowledgebase & devlog

### simple_calendar

* Copied the view over to add week number column.
4 changes: 4 additions & 0 deletions app/controllers/pages_controller.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# SPDX-FileCopyrightText: 2021 Felix Wolfsteller
#
# SPDX-License-Identifier: AGPL-3.0-or-later

class PagesController < ApplicationController
def home
@events = Event.all
Expand Down
5 changes: 5 additions & 0 deletions app/models/event.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
# SPDX-FileCopyrightText: 2021 Felix Wolfsteller
#
# SPDX-License-Identifier: AGPL-3.0-or-later

class Event < ApplicationRecord
belongs_to :event_source
end
7 changes: 7 additions & 0 deletions app/models/event_source.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# SPDX-FileCopyrightText: 2021 Felix Wolfsteller
#
# SPDX-License-Identifier: AGPL-3.0-or-later

class EventSource < ApplicationRecord
has_many :events
end
11 changes: 11 additions & 0 deletions db/migrate/20210204190938_create_event_sources.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CreateEventSources < ActiveRecord::Migration[6.1]
def change
create_table :event_sources do |t|
t.string :url
t.string :name
t.datetime :synced_at

t.timestamps
end
end
end
5 changes: 5 additions & 0 deletions db/migrate/20210204191003_add_event_source_to_event.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddEventSourceToEvent < ActiveRecord::Migration[6.1]
def change
add_reference :events, :event_source, null: false, foreign_key: true
end
end
13 changes: 12 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 18 additions & 17 deletions lib/tasks/read_ics.rake
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,24 @@ require 'json'
namespace :sevencal do
desc 'Import events from an ics file'
task import_ics: :environment do

url = 'https://siebenlinden.seminardesk.de/api/eventdates'

uri = URI(url)

response = Net::HTTP.get uri

payload = JSON.parse(response, symbolize_names: true)

dates = payload[:dates]

dates.each do |date|
Event.create!(
start_time: Time.at(date[:beginDate]/1000),
end_time: Time.at(date[:endDate]/1000),
title: date[:title].first[:value]
)
EventSource.find_each do |event_source|
puts "Consuming event source #{event_source.name} [#{event_source.url}]"
now = DateTime.current
uri = URI(event_source.url)
response = Net::HTTP.get uri

payload = JSON.parse(response, symbolize_names: true)
dates = payload[:dates]

dates.each do |date|
event_source.events.create!(
start_time: Time.at(date[:beginDate]/1000),
end_time: Time.at(date[:endDate]/1000),
title: date[:title].first[:value]
)
end
event_source.update!(synced_at: now)
puts "#{dates.count} events created"
end
end
end
Expand Down
11 changes: 11 additions & 0 deletions test/fixtures/event_sources.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

one:
url: MyString
name: MyString
synced_at: 2021-02-04 20:09:38

two:
url: MyString
name: MyString
synced_at: 2021-02-04 20:09:38
7 changes: 7 additions & 0 deletions test/models/event_source_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require "test_helper"

class EventSourceTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

0 comments on commit e8337bd

Please sign in to comment.