Skip to content

Commit

Permalink
Fix Metrics/AbcSize
Browse files Browse the repository at this point in the history
...through some light refactorings.
  • Loading branch information
Koronen committed Dec 8, 2015
1 parent 45504b3 commit a8d2cf5
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 56 deletions.
4 changes: 0 additions & 4 deletions .rubocop_todo.yml
Expand Up @@ -12,10 +12,6 @@ Lint/RescueException:
- 'app/tasks/fetch_feed.rb'
- 'app/utils/feed_discovery.rb'

# Offense count: 5
Metrics/AbcSize:
Max: 37

# Offense count: 1
# Configuration parameters: CountComments.
Metrics/ClassLength:
Expand Down
35 changes: 20 additions & 15 deletions app/fever_api/response.rb
Expand Up @@ -16,28 +16,33 @@

module FeverAPI
class Response
def initialize(params)
@response = { api_version: 3 }
ACTIONS = [
Authentication,

@response.merge! Authentication.new.call(params)
ReadFeeds,
ReadGroups,
ReadFeedsGroups,
ReadFavicons,
ReadItems,
ReadLinks,

@response.merge! ReadFeeds.new.call(params)
@response.merge! ReadGroups.new.call(params)
@response.merge! ReadFeedsGroups.new.call(params)
@response.merge! ReadFavicons.new.call(params)
@response.merge! ReadItems.new.call(params)
@response.merge! ReadLinks.new.call(params)
SyncUnreadItemIds,
SyncSavedItemIds,

@response.merge! SyncUnreadItemIds.new.call(params)
@response.merge! SyncSavedItemIds.new.call(params)
WriteMarkItem,
WriteMarkFeed,
WriteMarkGroup
]

@response.merge! WriteMarkItem.new.call(params)
@response.merge! WriteMarkFeed.new.call(params)
@response.merge! WriteMarkGroup.new.call(params)
def initialize(params)
@params = params
end

def to_json
@response.to_json
base_response = { api_version: 3 }
ACTIONS
.inject(base_response) { |a, e| a.merge!(e.new.call(@params)) }
.to_json
end
end
end
28 changes: 16 additions & 12 deletions app/fever_api/write_mark_item.rb
Expand Up @@ -13,20 +13,24 @@ def initialize(options = {})
end

def call(params = {})
if params[:mark] == "item"
case params[:as]
when "read"
@read_marker_class.new(params[:id]).mark_as_read
when "unread"
@unread_marker_class.new(params[:id]).mark_as_unread
when "saved"
@starred_marker_class.new(params[:id]).mark_as_starred
when "unsaved"
@unstarred_marker_class.new(params[:id]).mark_as_unstarred
end
end
mark_item_as(params[:id], params[:as]) if params[:mark] == "item"

{}
end

private

def mark_item_as(id, as)
case as
when "read"
@read_marker_class.new(id).mark_as_read
when "unread"
@unread_marker_class.new(id).mark_as_unread
when "saved"
@starred_marker_class.new(id).mark_as_starred
when "unsaved"
@unstarred_marker_class.new(id).mark_as_unstarred
end
end
end
end
26 changes: 16 additions & 10 deletions app/tasks/fetch_feed.rb
Expand Up @@ -5,7 +5,6 @@
require_relative "../commands/feeds/find_new_stories"

class FetchFeed

USER_AGENT = "Stringer (https://github.com/swanson/stringer)"

def initialize(feed, parser: Feedjira::Feed, logger: nil)
Expand All @@ -16,15 +15,7 @@ def initialize(feed, parser: Feedjira::Feed, logger: nil)

def fetch
begin
options = {
user_agent: USER_AGENT,
if_modified_since: @feed.last_fetched,
timeout: 30,
max_redirects: 2,
compress: true
}

raw_feed = @parser.fetch_and_parse(@feed.url, options)
raw_feed = fetch_raw_feed

if raw_feed == 304
@logger.info "#{@feed.url} has not been modified since last fetch" if @logger
Expand All @@ -45,11 +36,26 @@ def fetch
end

private

def fetch_raw_feed
@parser.fetch_and_parse(@feed.url, options)
end

def new_entries_from(raw_feed)
finder = FindNewStories.new(raw_feed, @feed.id, @feed.last_fetched, latest_entry_id)
finder.new_stories
end

def options
{
user_agent: USER_AGENT,
if_modified_since: @feed.last_fetched,
timeout: 30,
max_redirects: 2,
compress: true
}
end

def latest_entry_id
return @feed.stories.first.entry_id unless @feed.stories.empty?
end
Expand Down
26 changes: 19 additions & 7 deletions app/utils/opml_parser.rb
Expand Up @@ -2,13 +2,10 @@

class OpmlParser
def parse_feeds(contents)
doc = Nokogiri.XML(contents)

feeds_with_groups = Hash.new { |h, k| h[k] = [] }

doc.xpath('//body/outline').each do |outline|

if outline.attributes['xmlUrl'].nil? # it's a group!
outlines_in(contents).each do |outline|
if outline_is_group?(outline)
group_name = extract_name(outline.attributes).value
feeds = outline.xpath('./outline')
else # it's a top-level feed, which means it's a feed without group
Expand All @@ -17,16 +14,31 @@ def parse_feeds(contents)
end

feeds.each do |feed|
feeds_with_groups[group_name] << { name: extract_name(feed.attributes).value,
url: feed.attributes['xmlUrl'].value }
feeds_with_groups[group_name] << feed_to_hash(feed)
end
end

feeds_with_groups
end

private

def outlines_in(contents)
Nokogiri.XML(contents).xpath('//body/outline')
end

def outline_is_group?(outline)
outline.attributes['xmlUrl'].nil?
end

def extract_name(attributes)
attributes['title'] || attributes['text']
end

def feed_to_hash(feed)
{
name: extract_name(feed.attributes).value,
url: feed.attributes['xmlUrl'].value
}
end
end
18 changes: 10 additions & 8 deletions spec/factories/story_factory.rb
Expand Up @@ -26,14 +26,16 @@ def as_fever_json
end

def self.build(params = {})
FakeStory.new(
default_params = {
id: rand(100),
title: params[:title] || Faker::Lorem.sentence,
permalink: params[:permalink] || Faker::Internet.url,
body: params[:body] || Faker::Lorem.paragraph,
feed: params[:feed] || FeedFactory.build,
is_read: params[:is_read] || false,
is_starred: params[:is_starred] || false,
published: params[:published] || Time.now)
title: Faker::Lorem.sentence,
permalink: Faker::Internet.url,
body: Faker::Lorem.paragraph,
feed: FeedFactory.build,
is_read: false,
is_starred: false,
published: Time.now
}
FakeStory.new(default_params.merge(params))
end
end

0 comments on commit a8d2cf5

Please sign in to comment.