Merge pull request #4321 from andrewcsmith/feature/4100-add-support-for-posting-to-wordpress-as-a-service

Feature/4100 add support for posting to wordpress as a service

Conflicts:
	Gemfile.lock
This commit is contained in:
Jonne Haß 2013-08-04 17:13:50 +02:00
commit 6689f20710
15 changed files with 94 additions and 1 deletions

View file

@ -32,6 +32,7 @@
* Close publisher when clicking on the page outside of it [#4282](https://github.com/diaspora/diaspora/pull/4282) * Close publisher when clicking on the page outside of it [#4282](https://github.com/diaspora/diaspora/pull/4282)
* Deleting a post deletes it from Tumblr too [#4331](https://github.com/diaspora/diaspora/pull/4331) * Deleting a post deletes it from Tumblr too [#4331](https://github.com/diaspora/diaspora/pull/4331)
* OpenGraph support [#4215](https://github.com/diaspora/diaspora/pull/4215) * OpenGraph support [#4215](https://github.com/diaspora/diaspora/pull/4215)
* Added Wordpress service ability for posts. [#4321](https://github.com/diaspora/diaspora/pull/4321)
# 0.1.1.0 # 0.1.1.0

View file

@ -78,6 +78,7 @@ gem 'omniauth-facebook', '1.4.1'
gem 'omniauth-tumblr', '1.1' gem 'omniauth-tumblr', '1.1'
gem 'omniauth-twitter', '1.0.0' gem 'omniauth-twitter', '1.0.0'
gem 'twitter', '4.8.1' gem 'twitter', '4.8.1'
gem 'omniauth-wordpress','0.1.1', github: 'readmill/omniauth-wordpress'
# Tags # Tags

View file

@ -1,3 +1,10 @@
GIT
remote: git://github.com/readmill/omniauth-wordpress.git
revision: 32ac451f564e5189a863cb5db16aad2d48f7a638
specs:
omniauth-wordpress (0.1.1)
omniauth-oauth2 (~> 1.1.0)
GEM GEM
remote: https://rubygems.org/ remote: https://rubygems.org/
specs: specs:
@ -468,6 +475,7 @@ DEPENDENCIES
omniauth-facebook (= 1.4.1) omniauth-facebook (= 1.4.1)
omniauth-tumblr (= 1.1) omniauth-tumblr (= 1.1)
omniauth-twitter (= 1.0.0) omniauth-twitter (= 1.0.0)
omniauth-wordpress (= 0.1.1)!
opengraph_parser (= 0.2.3) opengraph_parser (= 0.2.3)
rack-cors (= 0.2.8) rack-cors (= 0.2.8)
rack-google-analytics (= 0.11.0) rack-google-analytics (= 0.11.0)

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB

View file

@ -797,6 +797,7 @@ form p.checkbox_select
.social_media_logos-facebook-16x16, .social_media_logos-facebook-16x16,
.social_media_logos-twitter-16x16, .social_media_logos-twitter-16x16,
.social_media_logos-tumblr-16x16, .social_media_logos-tumblr-16x16,
.social_media_logos-wordpress-16x16,
.social_media_logos-email-16x16, .social_media_logos-email-16x16,
.social_media_logos-feed-16x16, .social_media_logos-feed-16x16,
.social_media_logos-website-16x16 .social_media_logos-website-16x16
@ -2885,6 +2886,7 @@ a.toggle_selector
.social_media_logos-facebook-24x24, .social_media_logos-facebook-24x24,
.social_media_logos-twitter-24x24, .social_media_logos-twitter-24x24,
.social_media_logos-tumblr-24x24, .social_media_logos-tumblr-24x24,
.social_media_logos-wordpress-24x24,
.social_media_logos-email-24x24, .social_media_logos-email-24x24,
.social_media_logos-feed-24x24, .social_media_logos-feed-24x24,
.social_media_logos-website-24x24 .social_media_logos-website-24x24

View file

@ -0,0 +1,32 @@
class Services::Wordpress < Service
include ActionView::Helpers::TextHelper
include MarkdownifyHelper
MAX_CHARACTERS = 1000
attr_accessor :username, :password, :host, :path
# uid = blog_id
def provider
"wordpress"
end
def post(post, url='')
res = Faraday.new(:url => "https://public-api.wordpress.com").post do |req|
req.url "/rest/v1/sites/#{self.uid}/posts/new"
req.body = post_body(post).to_json
req.headers['Authorization'] = "Bearer #{self.access_token}"
req.headers['Content-Type'] = 'application/json'
end
JSON.parse res.env[:body]
end
def post_body(post, url='')
post_text = markdownify(post.text)
post_title = truncate(strip_markdown(post.text(:plain_text => true)), :length => 40, :separator => ' ')
{:title => post_title, :content => post_text.html_safe}
end
end

View file

@ -92,6 +92,10 @@ defaults:
enable: false enable: false
key: key:
secret: secret:
wordpress:
enable: false
key:
secret:
mail: mail:
enable: false enable: false
sender_address: 'no-reply@example.org' sender_address: 'no-reply@example.org'

View file

@ -328,6 +328,12 @@ configuration: ## Section
#enable: true #enable: true
#key: 'abcdef' #key: 'abcdef'
#secret: 'changeme' #secret: 'changeme'
## OAuth credentials for Wordpress
wordpress: ## Section
#enable: true
#client_id: 'abcdef'
#secret: 'changeme'
## Settings relevant to administrators ## Settings relevant to administrators
admins: ## Section admins: ## Section

View file

@ -26,4 +26,8 @@ Rails.application.config.middleware.use OmniAuth::Builder do
} }
} }
end end
if AppConfig.services.wordpress.enable?
provider :wordpress, AppConfig.services.wordpress.client_id, AppConfig.services.wordpress.secret
end
end end

View file

@ -24,7 +24,7 @@ module Configuration
return @configured_services unless @configured_services.nil? return @configured_services unless @configured_services.nil?
@configured_services = [] @configured_services = []
[:twitter, :tumblr, :facebook].each do |service| [:twitter, :tumblr, :facebook, :wordpress].each do |service|
@configured_services << service if services.send(service).enable? @configured_services << service if services.send(service).enable?
end end

View file

@ -61,10 +61,12 @@ describe Configuration::Methods do
services.stub(:twitter).and_return(enabled) services.stub(:twitter).and_return(enabled)
services.stub(:tumblr).and_return(enabled) services.stub(:tumblr).and_return(enabled)
services.stub(:facebook).and_return(disabled) services.stub(:facebook).and_return(disabled)
services.stub(:wordpress).and_return(disabled)
@settings.stub(:services).and_return(services) @settings.stub(:services).and_return(services)
@settings.configured_services.should include :twitter @settings.configured_services.should include :twitter
@settings.configured_services.should include :tumblr @settings.configured_services.should include :tumblr
@settings.configured_services.should_not include :facebook @settings.configured_services.should_not include :facebook
@settings.configured_services.should_not include :wordpress
end end
end end

View file

@ -0,0 +1,33 @@
require 'spec_helper'
describe Services::Wordpress do
before do
@user = alice
@post = @user.post(:status_message,
:text => "Hello there. This is a **Wordpress** post that we hope to turn into something else.",
:to => @user.aspects.first.id)
@service = Services::Wordpress.new(:nickname => "andrew",
:access_token => "abc123",
:uid => "123")
@user.services << @service
end
describe "#post" do
it 'posts a status message to wordpress' do
stub_request(:post, "https://public-api.wordpress.com/rest/v1/sites/123/posts/new").to_return(:status => 200, :body => {:ID => 68}.to_json, :headers => {})
@service.post(@post)
end
end
describe "#post_body" do
it "truncates content for use in title" do
@service.post_body(@post)[:title].should eq("Hello there. This is a Wordpress post...")
end
it "converts markdown tags" do
@service.post_body(@post)[:content].should match("<strong>Wordpress</strong>")
end
end
end