wordpress-com 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +18 -0
- data/Gemfile +7 -0
- data/LICENSE +22 -0
- data/README.md +77 -0
- data/Rakefile +2 -0
- data/examples/authorizer.rb +38 -0
- data/examples/authorizer_form.haml +22 -0
- data/examples/authorizer_result.haml +15 -0
- data/lib/wordpress-com.rb +43 -0
- data/lib/wordpress-com/access_token.rb +26 -0
- data/lib/wordpress-com/version.rb +3 -0
- data/wordpress-com.gemspec +23 -0
- metadata +106 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Maciej Pasternacki
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
# Wordpress::Com
|
2
|
+
|
3
|
+
Library to access WordPress.com's REST API
|
4
|
+
|
5
|
+
https://developer.wordpress.com/
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'wordpress-com'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install wordpress-com
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
To authorize your token, register your application at
|
24
|
+
https://developers.wordpress.com/. Make sure that redirect URL is
|
25
|
+
yours - user will be redirected there, and it has to match. Then do this:
|
26
|
+
|
27
|
+
require 'wordpress-com'
|
28
|
+
wpc = WordpressCom.new(client_id, client_secret)
|
29
|
+
url = wpc.authorize(redirect_uri)
|
30
|
+
# or: wpc.authorize(redirect_uri, :blog => 'http://foobar.wordpress.com/')
|
31
|
+
|
32
|
+
Make your user redirect to the returned `url`. User will be redirected
|
33
|
+
to the `redirect_url` and given a `code` GET parameter. Use this
|
34
|
+
parameter to obtain a token:
|
35
|
+
|
36
|
+
wpc = WordpressCom.new(client_id, client_secret)
|
37
|
+
wpc.get_token(code, redirect_uri)
|
38
|
+
|
39
|
+
You can then save complete authorization data, store it somewhere, and
|
40
|
+
reuse it later on. The authorization data is JSON and YAML safe.
|
41
|
+
|
42
|
+
wordpress_auth = wpc.serialize
|
43
|
+
|
44
|
+
wpc = WordPressCom.deserialize(wordpress_auth)
|
45
|
+
|
46
|
+
If you are writing a desktop or headless application and want to just
|
47
|
+
have the authorization data without any hassle, you can use provided
|
48
|
+
`examples/authorizer.rb` Sinatra app. Set your redirect URL to
|
49
|
+
`http://localhost:4567/' and run the app:
|
50
|
+
|
51
|
+
$ cd examples/
|
52
|
+
$ ruby authorizer.rb
|
53
|
+
|
54
|
+
Browse to http://localhost:4567/, follow the instructions, and after
|
55
|
+
successfully authorizing you will see serialized authentication data
|
56
|
+
as a YAML snippet.
|
57
|
+
|
58
|
+
To actually call the API, use `.request`, `.get`, `.post`, `.put`, and
|
59
|
+
`.delete` methods of the WordPressCom instance, or use the `token`
|
60
|
+
attribute to get to OAuth2 token itself:
|
61
|
+
|
62
|
+
wpc.post('posts/new', :body => {
|
63
|
+
:title => "Hello, World!",
|
64
|
+
:content => "Lorem ipsum dolor sit amet",
|
65
|
+
:tags => 'foo,bar,xyzzy'})
|
66
|
+
|
67
|
+
The request methods are automatically prefixed with
|
68
|
+
`/rest/v1/sites/$site_id/`. To get rid of the `/sites/$site_id/` part,
|
69
|
+
provide `:root_path => true` keyword parameter.
|
70
|
+
|
71
|
+
## Contributing
|
72
|
+
|
73
|
+
1. Fork it
|
74
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
75
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
76
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
77
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'sinatra'
|
2
|
+
require 'wordpress-com'
|
3
|
+
|
4
|
+
set :views, File.dirname(__FILE__)
|
5
|
+
enable :sessions
|
6
|
+
|
7
|
+
get '/' do
|
8
|
+
if params[:code]
|
9
|
+
wpc = WordpressCom.new(session[:client_id], session[:client_secret])
|
10
|
+
wpc.get_token(params[:code], session[:redirect_uri])
|
11
|
+
session[:wpc] = wpc.serialize
|
12
|
+
redirect to('/')
|
13
|
+
elsif session[:wpc]
|
14
|
+
haml :authorizer_result
|
15
|
+
else
|
16
|
+
haml :authorizer_form
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
post '/' do
|
21
|
+
session[:client_id] = params[:client_id]
|
22
|
+
session[:client_secret] = params[:client_secret]
|
23
|
+
session[:blog] = params[:blog]
|
24
|
+
session[:redirect_uri] = request.url
|
25
|
+
|
26
|
+
wpc = WordpressCom.new(session[:client_id], session[:client_secret])
|
27
|
+
redirect to(
|
28
|
+
if session[:blog]
|
29
|
+
wpc.authorize_url(request.url, :blog => session[:blog])
|
30
|
+
else
|
31
|
+
wpc.authorize_url(request.url)
|
32
|
+
end )
|
33
|
+
end
|
34
|
+
|
35
|
+
post '/forget' do
|
36
|
+
session.clear
|
37
|
+
redirect to('/')
|
38
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
!!! 5
|
2
|
+
%html
|
3
|
+
%head
|
4
|
+
%title WordPress.com authorizer
|
5
|
+
%link{:rel => "stylesheet",
|
6
|
+
:href => "http://www.blueprintcss.org/blueprint/screen.css",
|
7
|
+
:type => "text/css",
|
8
|
+
:media => "screen, projection"}
|
9
|
+
%body
|
10
|
+
.container
|
11
|
+
%h1 Example authorizer for WordPress.com REST API
|
12
|
+
%form(method="POST")
|
13
|
+
%table
|
14
|
+
- [[:client_id, "Client ID"], [:client_secret, "Client secret"], [:blog, 'WordPress.com Blog URL <span class="quiet">(optional)</span>' ]].each do |name, title|
|
15
|
+
%tr
|
16
|
+
%td(style="text-align: right")
|
17
|
+
%label{ :for => name }= title
|
18
|
+
%td
|
19
|
+
%input.text{ :name=> name, :value => session[name] }
|
20
|
+
%tr
|
21
|
+
%td(colspan="2" style="text-align: center")
|
22
|
+
%input(type="submit" value="Get authorization!")
|
@@ -0,0 +1,15 @@
|
|
1
|
+
!!! 5
|
2
|
+
%html
|
3
|
+
%head
|
4
|
+
%title WordPress.com authorizer
|
5
|
+
%link{:rel => "stylesheet",
|
6
|
+
:href => "http://www.blueprintcss.org/blueprint/screen.css",
|
7
|
+
:type => "text/css",
|
8
|
+
:media => "screen, projection"}
|
9
|
+
%body
|
10
|
+
.container
|
11
|
+
%h1 Example authorizer for WordPress.com REST API
|
12
|
+
%h2 Authorized successfully
|
13
|
+
%pre.success= YAML::dump(session[:wpc])
|
14
|
+
%form(method="POST" action="/forget")
|
15
|
+
%input(type="submit" value="Forget it!")
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'oauth2'
|
2
|
+
|
3
|
+
require "wordpress-com/version"
|
4
|
+
require "wordpress-com/access_token"
|
5
|
+
|
6
|
+
class WordpressCom
|
7
|
+
extend Forwardable
|
8
|
+
attr_reader :client, :token
|
9
|
+
def_delegators :@token, :request, :get, :post, :put, :delete
|
10
|
+
|
11
|
+
def initialize(client_id, client_secret, token=nil)
|
12
|
+
@client = OAuth2::Client.new(client_id, client_secret,
|
13
|
+
:site => 'https://public-api.wordpress.com/',
|
14
|
+
:authorize_url => '/oauth2/authorize',
|
15
|
+
:token_url => '/oauth2/token')
|
16
|
+
@token = AccessToken.from_hash(client, token) if token
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.deserialize(data)
|
20
|
+
self.new(*data)
|
21
|
+
end
|
22
|
+
|
23
|
+
def authorize_url(redirect_uri, opts={})
|
24
|
+
opts[:redirect_uri] = @redirect_uri = redirect_uri
|
25
|
+
client.auth_code.authorize_url(opts)
|
26
|
+
end
|
27
|
+
|
28
|
+
def get_token(code, redirect_uri=nil)
|
29
|
+
redirect_uri ||= @redirect_uri
|
30
|
+
@token = AccessToken.from_hash( @client,
|
31
|
+
client.auth_code.get_token(code,
|
32
|
+
:redirect_uri => redirect_uri,
|
33
|
+
:parse => :json).to_hash)
|
34
|
+
end
|
35
|
+
|
36
|
+
def serialize
|
37
|
+
[ client.id, client.secret, token.to_hash ]
|
38
|
+
end
|
39
|
+
|
40
|
+
def blog_id
|
41
|
+
token['blog_id']
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require "oauth2"
|
2
|
+
|
3
|
+
class OAuth2::AccessToken
|
4
|
+
# Returns serialization of the token as a Hash
|
5
|
+
def to_hash
|
6
|
+
rv = { :access_token => token }
|
7
|
+
rv[:refresh_token] = refresh_token if refresh_token
|
8
|
+
rv[:expires_in] = expires_in if expires_in
|
9
|
+
rv[:expires_at] = expires_at if expires_at
|
10
|
+
rv[:mode] = options[:mode] if options[:mode] != :header
|
11
|
+
rv[:header_format] = options[:header_format] if options[:header_format] != 'Bearer %s'
|
12
|
+
rv[:param_name] = options[:param_name] if options[:param_name] != 'bearer_token'
|
13
|
+
rv.merge(params)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
class WordpressCom
|
18
|
+
class AccessToken < OAuth2::AccessToken
|
19
|
+
def request(verb, path, opts={}, &block)
|
20
|
+
full_path = "/rest/v1/"
|
21
|
+
full_path << "sites/#{self['blog_id']}/" unless opts.delete('root_path')
|
22
|
+
full_path << path.gsub(/^\/*/, '')
|
23
|
+
super(verb, full_path, opts, &block)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/wordpress-com/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Maciej Pasternacki"]
|
6
|
+
gem.email = ["maciej@pasternacki.net"]
|
7
|
+
gem.description = 'Ruby client for WordPress.com REST API'
|
8
|
+
gem.summary = 'WordPress.com REST API client'
|
9
|
+
gem.homepage = "https://github.com/mpasternacki/wordpress-com/"
|
10
|
+
gem.licenses = ['MIT']
|
11
|
+
|
12
|
+
gem.files = `git ls-files`.split($\)
|
13
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
14
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
15
|
+
gem.name = "wordpress-com"
|
16
|
+
gem.require_paths = ["lib"]
|
17
|
+
gem.version = WordpressCom::VERSION
|
18
|
+
|
19
|
+
gem.add_dependency "json"
|
20
|
+
gem.add_dependency "oauth2"
|
21
|
+
|
22
|
+
gem.add_development_dependency "pry"
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: wordpress-com
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Maciej Pasternacki
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-06-23 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: json
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: oauth2
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: pry
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
description: Ruby client for WordPress.com REST API
|
63
|
+
email:
|
64
|
+
- maciej@pasternacki.net
|
65
|
+
executables: []
|
66
|
+
extensions: []
|
67
|
+
extra_rdoc_files: []
|
68
|
+
files:
|
69
|
+
- .gitignore
|
70
|
+
- Gemfile
|
71
|
+
- LICENSE
|
72
|
+
- README.md
|
73
|
+
- Rakefile
|
74
|
+
- examples/authorizer.rb
|
75
|
+
- examples/authorizer_form.haml
|
76
|
+
- examples/authorizer_result.haml
|
77
|
+
- lib/wordpress-com.rb
|
78
|
+
- lib/wordpress-com/access_token.rb
|
79
|
+
- lib/wordpress-com/version.rb
|
80
|
+
- wordpress-com.gemspec
|
81
|
+
homepage: https://github.com/mpasternacki/wordpress-com/
|
82
|
+
licenses:
|
83
|
+
- MIT
|
84
|
+
post_install_message:
|
85
|
+
rdoc_options: []
|
86
|
+
require_paths:
|
87
|
+
- lib
|
88
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
95
|
+
none: false
|
96
|
+
requirements:
|
97
|
+
- - ! '>='
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: '0'
|
100
|
+
requirements: []
|
101
|
+
rubyforge_project:
|
102
|
+
rubygems_version: 1.8.24
|
103
|
+
signing_key:
|
104
|
+
specification_version: 3
|
105
|
+
summary: WordPress.com REST API client
|
106
|
+
test_files: []
|