-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add User, Status, Place, Point, and Polygon classes
- Loading branch information
Showing
18 changed files
with
307 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |