Skip to content

Commit

Permalink
Add User, Status, Place, Point, and Polygon classes
Browse files Browse the repository at this point in the history
  • Loading branch information
sferik committed Oct 8, 2011
1 parent 1245742 commit b92b0ca
Show file tree
Hide file tree
Showing 18 changed files with 307 additions and 12 deletions.
4 changes: 2 additions & 2 deletions lib/twitter/api.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
require 'twitter/authentication'
require 'twitter/authenticatable'
require 'twitter/configuration'
require 'twitter/connection'
require 'twitter/request'

module Twitter
class API
include Authentication
include Authenticatable
include Connection
include Request

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
module Twitter
module Authentication
private
module Authenticatable

# Authentication hash
# Credentials hash
#
# @return [Hash]
def authentication
def credentials
{
:consumer_key => consumer_key,
:consumer_secret => consumer_secret,
Expand All @@ -18,7 +17,7 @@ def authentication
#
# @return [Boolean]
def authenticated?
authentication.values.all?
credentials.values.all?
end
end
end
12 changes: 12 additions & 0 deletions lib/twitter/base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module Twitter
class Base

def initialize(hash={})
hash.each do |key, value|
instance_variable_set(:"@#{key}", value) unless value.nil?
end
self
end

end
end
4 changes: 3 additions & 1 deletion lib/twitter/client/user.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'twitter/error/not_found'
require 'twitter/user'

module Twitter
class Client
Expand All @@ -21,7 +22,8 @@ def user(*args)
options = args.last.is_a?(Hash) ? args.pop : {}
user = args.pop || get_screen_name
merge_user_into_options!(user, options)
get("/1/users/show.json", options)
user = get("/1/users/show.json", options)
Twitter::User.new(user)
end

# Returns true if the specified user exists
Expand Down
2 changes: 1 addition & 1 deletion lib/twitter/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def connection(options={})
Faraday.new(merged_options) do |builder|
builder.use Twitter::Request::Phoenix if options[:phoenix]
builder.use Twitter::Request::MultipartWithFile
builder.use Twitter::Request::TwitterOAuth, authentication if authenticated?
builder.use Twitter::Request::TwitterOAuth, credentials if authenticated?
builder.use Faraday::Request::Multipart
builder.use Faraday::Request::UrlEncoded
builder.use Twitter::Request::Gateway, gateway if gateway
Expand Down
12 changes: 12 additions & 0 deletions lib/twitter/creatable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require 'time'

module Twitter
module Creatable
# Time when the user was created
#
# @return [Time]
def created_at
Time.parse(@created_at) if @created_at
end
end
end
16 changes: 16 additions & 0 deletions lib/twitter/geo_factory.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require 'twitter/point'
require 'twitter/polygon'

module Twitter
class GeoFactory
def self.new(geo)
type = geo[:type]
if type
Twitter.const_get(type.to_sym).new(geo)
else
raise ArgumentError, "argument must have a :type key"
end
end

end
end
13 changes: 13 additions & 0 deletions lib/twitter/place.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require 'twitter/base'
require 'twitter/geo_factory'

module Twitter
class Place < Twitter::Base
attr_reader :attributes, :country, :country_code, :full_name, :id, :name,
:place_type, :url

def bounding_box
Twitter::GeoFactory.new(@bounding_box) if @bounding_box
end
end
end
19 changes: 19 additions & 0 deletions lib/twitter/point.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require 'twitter/base'

module Twitter
class Point < Twitter::Base
attr_reader :coordinates

def latitude
@coordinates[0]
end
alias :lat :latitude

def longitude
@coordinates[1]
end
alias :long :longitude
alias :lng :longitude

end
end
7 changes: 7 additions & 0 deletions lib/twitter/polygon.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'twitter/base'

module Twitter
class Polygon < Twitter::Base
attr_reader :coordinates
end
end
26 changes: 26 additions & 0 deletions lib/twitter/status.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require 'twitter/base'
require 'twitter/creatable'
require 'twitter/geo_factory'
require 'twitter/place'
require 'twitter/user'

module Twitter
class Status < Twitter::Base
include Twitter::Creatable
attr_reader :id, :in_reply_to_screen_name, :in_reply_to_status_id,
:in_reply_to_user_id, :favorited, :retweet_count, :retweeted, :source,
:text, :truncated
alias :favorited? :favorited
alias :retweeted? :retweeted
alias :truncated? :truncated

def geo
Twitter::GeoFactory.new(@geo) if @geo
end

def place
Twitter::Place.new(@place) if @place
end

end
end
44 changes: 44 additions & 0 deletions lib/twitter/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
require 'twitter/authenticatable'
require 'twitter/base'
require 'twitter/creatable'
require 'twitter/status'

module Twitter
class User < Twitter::Base
include Twitter::Authenticatable
include Twitter::Creatable
attr_reader :contributors_enabled, :default_profile,
:default_profile_image, :description, :favourites_count,
:follow_request_sent, :followers_count, :following, :friends_count,
:geo_enabled, :id, :is_translator, :lang, :listed_count, :location,
:name, :notifications, :profile_background_color,
:profile_background_image_url, :profile_background_image_url_https,
:profile_background_tile, :profile_image_url, :profile_image_url_https,
:profile_link_color, :profile_sidebar_border_color,
:profile_sidebar_fill_color, :profile_text_color,
:profile_use_background_image, :protected, :screen_name,
:show_all_inline_media, :statuses_count, :time_zone, :url, :utc_offset,
:verified
alias :contributors_enabled? :contributors_enabled
alias :default_profile? :default_profile
alias :default_profile_image? :default_profile_image
alias :follow_request_sent? :follow_request_sent
alias :following? :following
alias :geo_enabled? :geo_enabled
alias :is_translator? :is_translator
alias :notifications? :notifications
alias :profile_background_tile? :profile_background_tile
alias :profile_use_background_image? :profile_use_background_image
alias :protected? :protected
alias :show_all_inline_media? :show_all_inline_media
alias :verified? :verified

# Get a user's status
#
# @return [Status]
def status
Twitter::Status.new(@status) if @status
end

end
end
5 changes: 2 additions & 3 deletions spec/twitter/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,16 @@
}
end

context "during initialization"

context "during initialization" do
it "should override module configuration" do
api = Twitter::API.new(@configuration)
@keys.each do |key|
api.send(key).should == @configuration[key]
end
end
end

context "after initilization" do

it "should override module configuration after initialization" do
api = Twitter::API.new
@configuration.each do |key, value|
Expand Down
25 changes: 25 additions & 0 deletions spec/twitter/geo_factory_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require 'helper'

describe Twitter::GeoFactory do

describe ".new" do

it "should generate a Point" do
@geo = Twitter::GeoFactory.new(:type => "Point")
@geo.should be_a Twitter::Point
end

it "should generate a Polygon" do
@geo = Twitter::GeoFactory.new(:type => "Polygon")
@geo.should be_a Twitter::Polygon
end

it "should raise an ArgumentError when type is not specified" do
lambda do
Twitter::GeoFactory.new
end.should raise_error(ArgumentError)
end

end

end
18 changes: 18 additions & 0 deletions spec/twitter/place_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require 'helper'

describe Twitter::Place do

describe "#bounding_box" do

it "should return a Twitter::Place when set" do
place = Twitter::Place.new(:bounding_box => {:type => "Polygon", :coordinates => [[[-122.40348192, 37.77752898], [-122.387436, 37.77752898], [-122.387436, 37.79448597], [-122.40348192, 37.79448597]]]})
place.bounding_box.should be_a Twitter::Polygon
end

it "should return nil when not set" do
place = Twitter::Place.new
place.bounding_box.should be_nil
end

end
end
24 changes: 24 additions & 0 deletions spec/twitter/point_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require 'helper'

describe Twitter::Point do

before do
@point = Twitter::Point.new(:coordinates => [-122.399983, 37.788299])
end

describe "#latitude" do

it "should return the latitude" do
@point.latitude.should == -122.399983
end

end

describe "#longitude" do

it "should return the longitude" do
@point.longitude.should == 37.788299
end

end
end
47 changes: 47 additions & 0 deletions spec/twitter/status_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
require 'helper'

describe Twitter::Status do

describe "#created_at" do

it "should return a Time when set" do
status = Twitter::Status.new(:created_at => "Mon Jul 16 12:59:01 +0000 2007")
status.created_at.should be_a Time
end

it "should return nil when not set" do
status = Twitter::Status.new
status.created_at.should be_nil
end

end

describe "#geo" do

it "should return a Twitter::Point when set" do
status = Twitter::Status.new(:geo => {:type => "Point"})
status.geo.should be_a Twitter::Point
end

it "should return nil when not set" do
status = Twitter::Status.new
status.geo.should be_nil
end

end

describe "#place" do

it "should return a Twitter::Place when set" do
status = Twitter::Status.new(:place => {})
status.place.should be_a Twitter::Place
end

it "should return nil when not set" do
status = Twitter::Status.new
status.place.should be_nil
end

end

end
32 changes: 32 additions & 0 deletions spec/twitter/user_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require 'helper'

describe Twitter::User do

describe "#created_at" do

it "should return a Time when created_at is set" do
user = Twitter::User.new(:created_at => "Mon Jul 16 12:59:01 +0000 2007")
user.created_at.should be_a Time
end

it "should return nil when created_at is not set" do
user = Twitter::User.new
user.created_at.should be_nil
end

end

describe "#status" do
it "should return a Status when status is set" do
status = Twitter::User.new(:status => {:text => "Hello"}).status
status.should be_a Twitter::Status
end

it "should return nil when status is not set" do
status = Twitter::User.new.status
status.should be_nil
end

end

end

0 comments on commit b92b0ca

Please sign in to comment.