Permalink
Browse files

Get rid of explicit attr declarations

  • Loading branch information...
1 parent a553e4c commit 018587693399e1f78e16e66351427761b612ec61 @jondistad jondistad committed with Jeremy Weiskotten Oct 2, 2010
View
@@ -5,16 +5,23 @@ class Base
base_uri 'api.dribbble.com'
- attr_accessor :created_at
+ def initialize(attributes=nil)
+ @attributes = attributes || {}
+ @attributes.each_key { |k| instance_eval "undef #{k} if __respond_to__?(:#{k})" }
+ @attributes['created_at'] = parse_time(@attributes['created_at'])
+ end
- def initialize(attributes={})
- attributes ||= {}
- attributes.each do |key, value|
- setter = "#{key}="
- self.send setter, value if self.respond_to?(setter)
- end
+ alias __respond_to__? respond_to?
+ def respond_to?(method)
+ @attributes.has_key?(method.to_s) || __respond_to__?(method)
+ end
- after_initialize(attributes)
+ def method_missing(method, *args, &block)
+ if @attributes.has_key?(method.to_s)
+ @attributes[method.to_s]
+ else
+ super
+ end
end
def ==(other)
@@ -26,22 +33,18 @@ def eql?(other)
self == other
end
- def created_at=(timestamp)
- @created_at = parse_time(timestamp)
- end
-
def self.paginated_list(results)
Dribbble::PaginatedList.new(results)
end
- protected
-
- def after_initialize(attributes)
- # no-op, can be overridden
+ def paginated_list(results)
+ self.class.paginated_list(results)
end
+ protected
+
def parse_time(time_string)
- Time.parse(time_string)
+ Time.parse(time_string) if time_string
end
end
end
View
@@ -0,0 +1,4 @@
+module Dribbble
+ class Comment < Base
+ end
+end
@@ -1,17 +1,22 @@
module Dribbble
class PaginatedList < Array
attr_reader :total, :pages, :per_page, :page
- def initialize(response)
- @total = response['total']
- @pages = response['pages']
- @per_page = response['per_page']
- @page = response['page']
+ def initialize(results)
+ @total = results['total']
+ @pages = results['pages']
+ @per_page = results['per_page']
+ @page = results['page']
- result_key, result_class = response.has_key?('shots') \
- ? ['shots', Dribbble::Shot] \
- : ['players', Dribbble::Player]
+ result_key, result_class =
+ if results.has_key?('shots')
+ ['shots', Dribbble::Shot]
+ elsif results.has_key?('players')
+ ['players', Dribbble::Player]
+ else
+ ['comments', Dribbble::Comment]
+ end
- super(response[result_key].map { |attrs| result_class.new attrs })
+ super((results[result_key] || []).map { |attrs| result_class.new attrs })
end
def inspect
View
@@ -11,24 +11,24 @@ def self.find(id)
# Fetches this player's shots.
# Options: :page, :per_page
def shots(options={})
- Shot.paginated_list(self.class.get("/players/#{@id}/shots", :query => options))
+ paginated_list(self.class.get("/players/#{@id}/shots", :query => options))
end
# Fetches shots by players that this player follows.
def shots_following(options={})
- Shot.paginated_list(self.class.get("/players/#{@id}/shots/following", :query => options))
+ paginated_list(self.class.get("/players/#{@id}/shots/following", :query => options))
end
def draftees(options={})
- Player.paginated_list(self.class.get("/players/#{@id}/draftees", :query => options))
+ paginated_list(self.class.get("/players/#{@id}/draftees", :query => options))
end
def followers(options={})
- Player.paginated_list(self.class.get("/players/#{@id}/followers", :query => options))
+ paginated_list(self.class.get("/players/#{@id}/followers", :query => options))
end
def following(options={})
- Player.paginated_list(self.class.get("/players/#{@id}/following", :query => options))
+ paginated_list(self.class.get("/players/#{@id}/following", :query => options))
end
end
end
View
@@ -1,16 +1,18 @@
module Dribbble
class Shot < Base
- attr_accessor :id, :title, :url, :image_url, :image_teaser_url, :width, :height, :player,
- :views_count, :likes_count, :comments_count, :rebounds_count
-
- def after_initialize(attributes)
- @player = Dribbble::Player.new(attributes['player'])
+ def initialize(attributes={})
+ super
+ @attributes['player'] = Dribbble::Player.new(@attributes['player'])
end
def self.find(id)
new(get("/shots/#{id}"))
end
+ def comments(options={})
+ paginated_list(get("/shots/#{@id}/comments", :query => options))
+ end
+
# Options: :page, :per_page
def self.debuts(options={})
paginated_list(get("/shots/debuts", :query => options))
View
@@ -1,5 +1,6 @@
require 'httparty'
require 'dribbble/paginated_list'
require 'dribbble/base'
+require 'dribbble/comment'
require 'dribbble/shot'
require 'dribbble/player'
View
@@ -0,0 +1,11 @@
+require 'helper'
+
+class BaseTest < Test::Unit::TestCase
+ def test_attributes_are_methods
+ base = Dribbble::Base.new('foo' => 'bar', 'baz' => 'qux')
+ assert base.respond_to?(:foo)
+ assert base.respond_to?(:baz)
+ assert_equal 'bar', base.foo
+ assert_equal 'qux', base.baz
+ end
+end
@@ -46,4 +46,18 @@ def test_initialize_collecting_players
assert_equal 2, list.size
list.each { |p| assert p.is_a?(Dribbble::Player), "#{p.inspect} is not a Dribbble::Player" }
end
+
+ def test_initialize_collecting_comments
+ comments_results = {
+ "comments"=> [{}, {}],
+ "total"=>201,
+ "page"=>1,
+ "pages"=>101,
+ "per_page"=>2
+ }
+
+ list = Dribbble::PaginatedList.new(comments_results)
+ assert_equal 2, list.size
+ list.each { |p| assert p.is_a?(Dribbble::Comment), "#{p.inspect} is not a Dribbble::Comment" }
+ end
end
View
@@ -41,6 +41,11 @@ def test_find
assert_kind_of Dribbble::Player, shot.player
end
+ def test_comments
+ shot = Dribbble::Shot.find(21603)
+ assert false, "Not yet implemented!"
+ end
+
def test_debuts
shots = Dribbble::Shot.debuts
assert_kind_of Array, shots

0 comments on commit 0185876

Please sign in to comment.