From 2ace9b47b1de118c5766d780fb4b7f28719668fd Mon Sep 17 00:00:00 2001 From: Vinothan Shankar Date: Sat, 5 Sep 2015 23:21:38 +0100 Subject: [PATCH] * app/models/services/tumblr.rb (Services::Tumblr#post): Post only to primary blog Hardly a total fix, but it's a start. Based on meitar/diaspora:b5c01598. * app/models/services/tumblr.rb: style: double-quote string Line 22. Change from single-quotes. * app/models/services/tumblr.rb: parse URL only for blogs we care about ...which in this case is just the primary. This also fixes the failed test in 7079fda (and by extension, 7a1d3a9). * spec/models/services/tumblr_spec.rb: Add "primary" flag to test user-info * spec/models/services/tumblr_spec.rb: Add a secondary blog to test user-info This should not cause a request to be issued, so we can continue to expect the request for the primary blog only. Any request for the secondary indicates a bug in the service implementation. * spec/models/services/tumblr_spec.rb: Make the second blog returned the primary Just to catch if it's picking the first one * app/models/services/tumblr.rb: use blogs.find instead of blogs.each Since we're only handling the primary blog, this should be easy enough * app/models/services/tumblr.rb: Remove redundant if block The condition it would have caught is dealt with by the ifnone on the find * app/models/services/tumblr.rb: Style: remove space between { and | --- app/models/services/tumblr.rb | 16 +++++++++------- spec/models/services/tumblr_spec.rb | 8 +++++--- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/app/models/services/tumblr.rb b/app/models/services/tumblr.rb index 66dafe78c..3b1abb69e 100644 --- a/app/models/services/tumblr.rb +++ b/app/models/services/tumblr.rb @@ -16,16 +16,18 @@ class Services::Tumblr < Service def post(post, url='') body = build_tumblr_post(post, url) user_info = JSON.parse(client.get("/v2/user/info").body) - blogs = user_info["response"]["user"]["blogs"].map { |blog| URI.parse(blog['url']) } + blogs = user_info["response"]["user"]["blogs"] + primaryblog = blogs.find(blogs[0]) {|blog| blog["primary"] } tumblr_ids = {} - blogs.each do |blog| - resp = client.post("/v2/blog/#{blog.host}/post", body) - if resp.code == "201" - tumblr_ids[blog.host.to_s] = JSON.parse(resp.body)["response"]["id"] - end + + blogurl = URI.parse(primaryblog["url"]) + resp = client.post("/v2/blog/#{blogurl.host}/post", body) + if resp.code == "201" + tumblr_ids[blogurl.host.to_s] = JSON.parse(resp.body)["response"]["id"] + end + post.tumblr_ids = tumblr_ids.to_json post.save - end end def build_tumblr_post(post, url) diff --git a/spec/models/services/tumblr_spec.rb b/spec/models/services/tumblr_spec.rb index c6ecb9c3e..263e9c187 100644 --- a/spec/models/services/tumblr_spec.rb +++ b/spec/models/services/tumblr_spec.rb @@ -11,17 +11,19 @@ describe Services::Tumblr, :type => :model do describe '#post' do it 'posts a status message to tumblr and saves the returned ids' do - response = double(body: '{"response": {"user": {"blogs": [{"url": "http://foo.tumblr.com"}]}}}') + response = double(body: '{"response": {"user": {"blogs": + [{"primary": false, "url": "http://foo.tumblr.com"}, + {"primary": true, "url": "http://bar.tumblr.com"}]}}}') expect_any_instance_of(OAuth::AccessToken).to receive(:get) .with("/v2/user/info") .and_return(response) response = double(code: "201", body: '{"response": {"id": "bla"}}') expect_any_instance_of(OAuth::AccessToken).to receive(:post) - .with("/v2/blog/foo.tumblr.com/post", @service.build_tumblr_post(@post, '')) + .with("/v2/blog/bar.tumblr.com/post", @service.build_tumblr_post(@post, "")) .and_return(response) - expect(@post).to receive(:tumblr_ids=).with({"foo.tumblr.com" => "bla"}.to_json) + expect(@post).to receive(:tumblr_ids=).with({"bar.tumblr.com" => "bla"}.to_json) @service.post(@post) end