Class: Api::FeedsController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/api/feeds_controller.rb

Overview

Controller to access the Feed model.

Instance Method Summary collapse

Instance Method Details

#createObject

Subscribe the authenticated user to the feed passed in the params param.


76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'app/controllers/api/feeds_controller.rb', line 76

def create
  feed_url = feed_params[:url]
  @job_state = current_user.enqueue_subscribe_job feed_url

  if @job_state.present?
    head :ok
  else
    Rails.logger.error "Could not subscribe user #{current_user.id} to feed #{feed_url}, returning a 404"
    head 404
  end

rescue => e
  handle_error e
end

#destroyObject

Unsubscribe the authenticated user from the feed passed in the params param.


94
95
96
97
98
99
100
# File 'app/controllers/api/feeds_controller.rb', line 94

def destroy
  @feed = Feed.find params[:id]
  current_user.enqueue_unsubscribe_job @feed
  head :ok
rescue => e
  handle_error e
end

#indexObject

Return JSON with:

  • the list of feeds subscribed by the current user, if no :folder_id param is received.

  • the list of feeds in a folder owned by the current user, if a :folder_id param is received.

In both cases the :include_read param controls whether all feeds are returned (if true), or only feeds with unread entries (if false).


20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/controllers/api/feeds_controller.rb', line 20

def index
  @include_read = param_str_to_boolean :include_read, params

  if params[:folder_id].present?
    # Retrieve subscribed feeds in the passed folder
    index_folder
  else
    # Retrieve subscribed feeds regardless of folder
    index_all
  end
rescue => e
  handle_error e
end

#showObject

Return a JSON document describing a given feed, as long as the currently authenticated user is suscribed to it.

If the requests asks for a feed the current user is not suscribed to, the response is a 404 error code (Not Found).


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/controllers/api/feeds_controller.rb', line 39

def show
  @subscription = FeedSubscription.find_by user_id: current_user.id, feed_id: params[:id]

  if @subscription.present?
    # If feed subscription has not changed, return a 304
    if stale? etag: EtagCalculator.etag(@subscription.updated_at),
              last_modified: @subscription.updated_at
      @feed = current_user.feeds.find params[:id]
      @folder_id = @feed.user_folder(current_user)&.id || 'none'
      @unread_count = current_user.feed_unread_count @feed
      respond_with @feed, @folder_id, @unread_count
    end
  else
    Rails.logger.info "User #{current_user.id} - #{current_user.email} has no feeds to return, returning a 404"
    head 404
  end
rescue => e
  handle_error e
end

#updateObject

Fetch a feed and save in the database any new entries, as long as the currently authenticated user is suscribed to it.

If the request asks to refresh a feed the user is not suscribed to, the response is a 404 error code (Not Found).


64
65
66
67
68
69
70
71
# File 'app/controllers/api/feeds_controller.rb', line 64

def update
  @feed = current_user.feeds.find params[:id]
  current_user.refresh_feed @feed

  head :ok
rescue => e
  handle_error e
end