Skip to content

Commit

Permalink
Make Twitter::Base objects immutable
Browse files Browse the repository at this point in the history
  • Loading branch information
sferik committed Sep 24, 2013
1 parent f56698c commit 69b1ef7
Show file tree
Hide file tree
Showing 6 changed files with 4 additions and 56 deletions.
3 changes: 1 addition & 2 deletions lib/twitter/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ class Base
alias to_h attrs
alias to_hash attrs
alias to_hsh attrs
def_delegators :attrs, :delete, :update

# Define methods that retrieve the value from attributes
#
Expand Down Expand Up @@ -73,7 +72,7 @@ def self.define_attribute_method(key1, klass=nil, key2=nil)
else
attrs = @attrs.dup
value = attrs.delete(key1)
Twitter.const_get(klass).new(value.update(key2 => attrs))
Twitter.const_get(klass).new(value.merge(key2 => attrs))
end
else
NullObject.instance
Expand Down
9 changes: 0 additions & 9 deletions lib/twitter/relationship.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,5 @@ def initialize(attrs={})
@attrs = attrs[:relationship]
end

# Update the attributes of a Relationship
#
# @param attrs [Hash]
# @return [Twitter::Relationship]
def update(attrs)
@attrs.update(attrs[:relationship]) unless attrs[:relationship].nil?
self
end

end
end
7 changes: 3 additions & 4 deletions lib/twitter/rest/api/users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,10 @@ module Users
# @option options [String] :lang The language which Twitter should render in for this user. The language must be specified by the appropriate two letter ISO 639-1 representation. Currently supported languages are provided by {https://dev.twitter.com/docs/api/1.1/get/help/languages GET help/languages}.
def settings(options={})
request_method = options.size.zero? ? :get : :post
settings = object_from_response(Twitter::Settings, request_method, "/1.1/account/settings.json", options)
response = send(request_method.to_sym, "/1.1/account/settings.json", options)
# https://dev.twitter.com/issues/59
trend_location = Array(settings.attrs[:trend_location]).first
settings.update(:trend_location => trend_location)
settings
response.update(:trend_location => Array(response[:trend_location]).first)
Twitter::Settings.from_response(response)
end

# Returns the requesting user if authentication was successful, otherwise raises {Twitter::Error::Unauthorized}
Expand Down
16 changes: 0 additions & 16 deletions spec/twitter/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,4 @@
end
end

describe "#delete" do
it "deletes an attribute and returns its value" do
base = Twitter::Base.new(:id => 1)
expect(base.delete(:id)).to eq(1)
expect(base.attrs[:id]).to be_nil
end
end

describe "#update" do
it "returns a hash of attributes" do
base = Twitter::Base.new(:id => 1)
base.update(:id => 2)
expect(base.attrs[:id]).to eq(2)
end
end

end
17 changes: 0 additions & 17 deletions spec/twitter/rate_limit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,4 @@
end
end

describe "#update" do
before do
Timecop.freeze(Time.utc(2012, 6, 6, 17, 22, 0))
end
after do
Timecop.return
end
it "updates a rate limit" do
rate_limit = Twitter::RateLimit.new("x-rate-limit-reset" => "1339019097")
expect(rate_limit.reset_in).to be_an Integer
expect(rate_limit.reset_in).to eq(15777)
rate_limit.update({"x-rate-limit-reset" => "1339019098"})
expect(rate_limit.reset_in).to be_an Integer
expect(rate_limit.reset_in).to eq(15778)
end
end

end
8 changes: 0 additions & 8 deletions spec/twitter/relationship_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,4 @@
end
end

describe "#update" do
it "updates a relationship" do
relationship = Twitter::Relationship.new(:relationship => {:target => {:id => 7505382}})
relationship.update(:relationship => {:target => {:id => 14100886}})
expect(relationship.target.id).to eq(14100886)
end
end

end

0 comments on commit 69b1ef7

Please sign in to comment.