* 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 |
This commit is contained in:
Vinothan Shankar 2015-09-05 23:21:38 +01:00 committed by Jonne Haß
parent d38741d5eb
commit 2ace9b47b1
2 changed files with 14 additions and 10 deletions

View file

@ -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)

View file

@ -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