Skip to content

Commit

Permalink
#3 - returns empty set when github responds with 404
Browse files Browse the repository at this point in the history
  • Loading branch information
RatoX committed May 27, 2015
1 parent 22e60d7 commit 9e29626
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
26 changes: 17 additions & 9 deletions app/lib/github/github.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,26 @@ def initialize organization, repository, access_token
end

def posts filter
raw_data = ''
if(filter[:month])
raw_data = @client.contents @full_name, path:path(filter)
else
raw_data = @client.search_code "repo:#{@organization}/#{@repository} path:#{path(filter)} extension:md"
raw_data = raw_data[:items]
end

raw_data = search(filter)
raw_data = filter_by_title(raw_data, filter[:title])
raw_data.map { |item| to_post(item) }
end

def search filter
raw_data = []
begin
if(filter[:month])
raw_data = @client.contents @full_name, path:path(filter)
else
raw_data = @client.search_code "repo:#{@organization}/#{@repository} path:#{path(filter)} extension:md"
raw_data = raw_data[:items]
end
rescue Octokit::NotFound
raw_data = []
end
raw_data
end

def filter_by_title raw_data, title = nil
return raw_data if title.nil?

Expand All @@ -46,7 +54,7 @@ def post id
private
def path filter
path = "_posts/#{filter.year}"
path += ("/%02d" % filter.month) if filter.month
path += ("/%02d" % filter.month.to_i) if filter.month
path
end

Expand Down
8 changes: 8 additions & 0 deletions specs/integration/api/posts_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ def app
end
end

it 'should return empty' do
get "/organization/#{@organization}/#{@repository}/posts?year=1990&month=1"
response = JSON.parse(last_response.body)

assert response.kind_of?(Array)
assert response.empty?
end

it 'should return a single post by path' do
path = '_posts/2015/01/2015-01-19-a-fe-nao-costuma.md'
get "/organization/#{@organization}/#{@repository}/post?path=#{path}"
Expand Down

0 comments on commit 9e29626

Please sign in to comment.