Skip to content

Commit

Permalink
Provide sane defaults if page and per_page are not integers
Browse files Browse the repository at this point in the history
  • Loading branch information
Layton Wedgeworth committed Aug 16, 2008
1 parent 72702a3 commit cbee721
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/youtube_g/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ def initialize(logger=false)
# YouTubeG::Response::VideoSearch
def videos_by(params, options={})
request_params = params.respond_to?(:to_hash) ? params : options
request_params[:page] ||= 1
request_params[:page] = integer_or_default(request_params[:page], 1)

unless request_params[:max_results]
request_params[:max_results] = request_params[:per_page] || 25
request_params[:max_results] = integer_or_default(request_params[:per_page], 25)
end

unless request_params[:offset]
Expand Down Expand Up @@ -77,5 +77,9 @@ def calculate_offset(page, per_page)
page == 1 ? 1 : ((per_page * page) - per_page + 1)
end

def integer_or_default(value, default)
value = value.to_i
value > 0 ? value : default
end
end
end

0 comments on commit cbee721

Please sign in to comment.