wordpress_api 0.1.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.document ADDED
@@ -0,0 +1,3 @@
1
+ README
2
+ lib/**/*.rb
3
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,6 @@
1
+ *.gem
2
+ .bundle
3
+ .rspec
4
+ .idea
5
+ /Gemfile.lock
6
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'http://rubygems.org'
2
+
3
+ platforms :jruby do
4
+ gem 'jruby-openssl', '~> 0.7'
5
+ end
6
+
7
+ gemspec
data/README ADDED
@@ -0,0 +1 @@
1
+ A Ruby wrapper for the new Wordpress REST API.
data/Rakefile ADDED
@@ -0,0 +1,19 @@
1
+ #!/usr/bin/env rake
2
+
3
+ require 'bundler'
4
+ Bundler::GemHelper.install_tasks
5
+
6
+ require 'rspec/core/rake_task'
7
+ RSpec::Core::RakeTask.new(:spec)
8
+
9
+ task :test => :spec
10
+ task :default => :spec
11
+
12
+ require 'rdoc/task'
13
+ require File.expand_path('../lib/wordpress/version', __FILE__)
14
+ RDoc::Task.new do |rdoc|
15
+ rdoc.rdoc_dir = 'rdoc'
16
+ rdoc.title = "wordpress #{Wordpress::VERSION::STRING}"
17
+ rdoc.rdoc_files.include('README*')
18
+ rdoc.rdoc_files.include('lib/**/*.rb')
19
+ end
data/lib/wordpress.rb ADDED
@@ -0,0 +1,31 @@
1
+ require 'omniauth-oauth2'
2
+
3
+ module Wordpress
4
+
5
+ class << self
6
+ attr_accessor :token, :secret
7
+
8
+ # config/initializers/wordpress.rb (for instance)
9
+ #
10
+ # Wordpress.configure do |config|
11
+ # config.token = 'consumer_token'
12
+ # config.secret = 'consumer_secret'
13
+ # config.default_profile_fields = ['education', 'positions']
14
+ # end
15
+ #
16
+ # elsewhere
17
+ #
18
+ # client = Wordpress::Client.new
19
+ def configure
20
+ yield self
21
+ true
22
+ end
23
+ end
24
+
25
+ autoload :Api, "wordpress/api"
26
+ autoload :Client, "wordpress/client"
27
+ autoload :Mash, "wordpress/mash"
28
+ autoload :Errors, "wordpress/errors"
29
+ autoload :Helpers, "wordpress/helpers"
30
+ autoload :Version, "wordpress/version"
31
+ end
@@ -0,0 +1,6 @@
1
+ module Wordpress
2
+ module Api
3
+ autoload :Reader, "wordpress/api/reader"
4
+ autoload :Writer, "wordpress/api/writer"
5
+ end
6
+ end
@@ -0,0 +1,40 @@
1
+ module Wordpress
2
+ module Api
3
+
4
+ module Reader
5
+
6
+ def profile(options={})
7
+ path = "/me"
8
+ Mash.from_json(get path, options)
9
+ end
10
+
11
+ def blog(blog_id, options={})
12
+ path = "/sites/#{blog_id}"
13
+ Mash.from_json(get path, options)
14
+ end
15
+
16
+ def posts(blog_id, options={})
17
+ path = "/sites/#{blog_id}/posts"
18
+ Mash.from_json(get path, options)
19
+ end
20
+
21
+ def likes(blog_id, post_id, options={})
22
+ path = "/sites/#{blog_id}/posts/#{post_id}/likes"
23
+ Mash.from_json(get path, options)
24
+ end
25
+
26
+ def comments(blog_id, post_id, options={})
27
+ path = "/sites/#{blog_id}/posts/#{post_id}/replies"
28
+ Mash.from_json(get path, options)
29
+ end
30
+
31
+ def followers(blog_id, options={})
32
+ path = "/sites/#{blog_id}/follows/mine"
33
+ Mash.from_json(get path, options)
34
+ end
35
+
36
+
37
+ end
38
+
39
+ end
40
+ end
@@ -0,0 +1,58 @@
1
+ module Wordpress
2
+ module Api
3
+
4
+ module Writer
5
+
6
+ def create_post blog_id, content, options={}
7
+ path = "/sites/#{blog_id}/posts/new"
8
+ Mash.from_json(post(path, content.to_json, options))
9
+ end
10
+
11
+ def delete_post blog_id, post_id, options={}
12
+ path = "/sites/#{blog_id}/posts/#{post_id}/delete"
13
+ Mash.from_json(post(path, "", options))
14
+ end
15
+
16
+ def repost blog_id, post_id, options={}
17
+ path = "/sites/#{blog_id}/posts/#{post_id}/reblogs/new"
18
+ Mash.from_json(post path, "", options)
19
+ end
20
+
21
+ def create_comment blog_id, post_id, content, options={}
22
+ path = "/sites/#{blog_id}/posts/#{post_id}/replies/new"
23
+ Mash.from_json(post path, content.to_json, options)
24
+ end
25
+
26
+ def create_comment_tree blog_id, comment_id, content, options={}
27
+ path = "/sites/#{blog_id}/comments/#{comment_id}/replies/new"
28
+ Mash.from_json(post path, content.to_json, options)
29
+ end
30
+
31
+ def delete_comment blog_id, comment_id, options={}
32
+ path = "/sites/#{blog_id}/comments/#{comment_id}/delete"
33
+ Mash.from_json(post path, "", options)
34
+ end
35
+
36
+ def create_like blog_id, post_id, options={}
37
+ path = "/sites/#{blog_id}/posts/#{post_id}/likes/new"
38
+ Mash.from_json(post path, "", options)
39
+ end
40
+
41
+ def delete_like blog_id, post_id, options={}
42
+ path = "/sites/#{blog_id}/posts/#{post_id}/likes/mine/delete"
43
+ Mash.from_json(post path, "", options)
44
+ end
45
+
46
+ def follow blog_id, options={}
47
+ path = "/sites/#{blog_id}/follows/new"
48
+ Mash.from_json(post(path, "", options))
49
+ end
50
+
51
+ def unfollow blog_id, options={}
52
+ path = "/sites/#{blog_id}/follows/mine/delete"
53
+ Mash.from_json(post(path, "", options))
54
+ end
55
+ end
56
+
57
+ end
58
+ end
@@ -0,0 +1,22 @@
1
+ require 'cgi'
2
+
3
+ module Wordpress
4
+
5
+ class Client
6
+ include Helpers::Request
7
+ include Helpers::Authorization
8
+ include Api::Reader
9
+ include Api::Writer
10
+
11
+ attr_reader :consumer_token, :consumer_secret, :consumer_options
12
+
13
+ def initialize(ctoken=Wordpress.token, csecret=Wordpress.secret, options={})
14
+ @consumer_token = ctoken
15
+ @consumer_secret = csecret
16
+ @consumer_options = options
17
+ end
18
+
19
+
20
+ end
21
+
22
+ end
@@ -0,0 +1,18 @@
1
+ module Wordpress
2
+ module Errors
3
+ class WordpressError < StandardError
4
+ attr_reader :data
5
+ def initialize(data)
6
+ @data = data
7
+ super
8
+ end
9
+ end
10
+
11
+ class UnauthorizedError < WordpressError; end
12
+ class GeneralError < WordpressError; end
13
+
14
+ class UnavailableError < StandardError; end
15
+ class ServerError < StandardError; end
16
+ class NotFoundError < StandardError; end
17
+ end
18
+ end
@@ -0,0 +1,6 @@
1
+ module Wordpress
2
+ module Helpers
3
+ autoload :Authorization, "wordpress/helpers/authorization"
4
+ autoload :Request, "wordpress/helpers/request"
5
+ end
6
+ end
@@ -0,0 +1,46 @@
1
+ module Wordpress
2
+ module Helpers
3
+
4
+ module Authorization
5
+
6
+ DEFAULT_OAUTH_OPTIONS = {
7
+ :authorize_path => "/oauth2/authorize",
8
+ :api_host => "https://public-api.wordpress.com",
9
+ :auth_host => "https://public-api.wordpress.com",
10
+ }
11
+
12
+ def consumer
13
+ @consumer ||= ::OAuth2::Client.new(@consumer_token, @consumer_secret, parse_oauth_options)
14
+ end
15
+
16
+ def access_token
17
+ @access_token ||= ::OAuth2::AccessToken.new(consumer, @auth_token)
18
+ end
19
+
20
+ def authorize_from_access(atoken)
21
+ @auth_token = atoken
22
+ end
23
+
24
+ private
25
+ def parse_oauth_options
26
+ {
27
+ :authorize_url => full_oauth_url_for(:authorize, :auth_host),
28
+ :site => @consumer_options[:site] || @consumer_options[:api_host] || DEFAULT_OAUTH_OPTIONS[:api_host],
29
+ :parse => :json
30
+ }
31
+ end
32
+
33
+ def full_oauth_url_for(url_type, host_type)
34
+ if @consumer_options["#{url_type}_url".to_sym]
35
+ @consumer_options["#{url_type}_url".to_sym]
36
+ else
37
+ host = @consumer_options[:site] || @consumer_options[host_type] || DEFAULT_OAUTH_OPTIONS[host_type]
38
+ path = @consumer_options[:"#{url_type}_path".to_sym] || DEFAULT_OAUTH_OPTIONS["#{url_type}_path".to_sym]
39
+ "#{host}#{path}"
40
+ end
41
+ end
42
+
43
+ end
44
+
45
+ end
46
+ end
@@ -0,0 +1,76 @@
1
+ module Wordpress
2
+ module Helpers
3
+
4
+ module Request
5
+
6
+ DEFAULT_HEADERS = {
7
+ :headers => {"Content-Type" => "application/json"}
8
+ }
9
+
10
+ API_PATH = '/rest/v1'
11
+
12
+ protected
13
+
14
+ def get(path, options={})
15
+ response = access_token.get("#{API_PATH}#{path}", DEFAULT_HEADERS.merge(options))
16
+ raise_errors(response)
17
+ response.body
18
+ end
19
+
20
+ def post(path, body, options={})
21
+ response = access_token.post("#{API_PATH}#{path}", DEFAULT_HEADERS.merge(options).merge({:body => body}))
22
+ raise_errors(response)
23
+ response.body
24
+ end
25
+
26
+ def put(path, options={})
27
+ response = access_token.put("#{API_PATH}#{path}", DEFAULT_HEADERS.merge(options))
28
+ raise_errors(response)
29
+ response.body
30
+ end
31
+
32
+ def delete(path, options={})
33
+ response = access_token.delete("#{API_PATH}#{path}", DEFAULT_HEADERS.merge(options))
34
+ raise_errors(response)
35
+ response
36
+ end
37
+
38
+ private
39
+
40
+ def raise_errors(response)
41
+ OAuth2::Response
42
+ case response.status.to_i
43
+ when 401
44
+ data = Mash.from_json(response.body)
45
+ raise Wordpress::Errors::UnauthorizedError.new(data), "(#{data.status}): #{data.message}"
46
+ when 400, 403
47
+ data = Mash.from_json(response.body)
48
+ raise Wordpress::Errors::GeneralError.new(data), "(#{data.status}): #{data.message}"
49
+ when 404
50
+ raise Wordpress::Errors::NotFoundError, "(#{response.code}): #{response.message}"
51
+ when 500
52
+ raise Wordpress::Errors::ServerError, "(#{response.code}): #{response.message}"
53
+ when 502..503
54
+ raise Wordpress::Errors::UnavailableError, "(#{response.code}): #{response.message}"
55
+ end
56
+ end
57
+
58
+ def to_query(options)
59
+ options.inject([]) do |collection, opt|
60
+ collection << "#{opt[0]}=#{opt[1]}"
61
+ collection
62
+ end * '&'
63
+ end
64
+
65
+ def to_uri(path, options)
66
+ uri = URI.parse(path)
67
+
68
+ if options && options != {}
69
+ uri.query = to_query(options)
70
+ end
71
+ uri.to_s
72
+ end
73
+ end
74
+
75
+ end
76
+ end
@@ -0,0 +1,68 @@
1
+ require 'hashie'
2
+ require 'multi_json'
3
+
4
+ module Wordpress
5
+ class Mash < ::Hashie::Mash
6
+
7
+ # a simple helper to convert a json string to a Mash
8
+ def self.from_json(json_string)
9
+ result_hash = ::MultiJson.decode(json_string)
10
+ new(result_hash)
11
+ end
12
+
13
+ # returns a Date if we have year, month and day, and no conflicting key
14
+ def to_date
15
+ if !self.has_key?('to_date') && contains_date_fields?
16
+ Date.civil(self.year, self.month, self.day)
17
+ else
18
+ super
19
+ end
20
+ end
21
+
22
+ def timestamp
23
+ value = self['timestamp']
24
+ if value.kind_of? Integer
25
+ value = value / 1000 if value > 9999999999
26
+ Time.at(value)
27
+ else
28
+ value
29
+ end
30
+ end
31
+
32
+ protected
33
+
34
+ def contains_date_fields?
35
+ self.year? && self.month? && self.day?
36
+ end
37
+
38
+ # overload the convert_key mash method so that the Wordpress
39
+ # keys are made a little more ruby-ish
40
+ def convert_key(key)
41
+ case key.to_s
42
+ when '_key'
43
+ 'id'
44
+ when '_total'
45
+ 'total'
46
+ when 'values'
47
+ 'all'
48
+ when 'numResults'
49
+ 'total_results'
50
+ else
51
+ underscore(key)
52
+ end
53
+ end
54
+
55
+ # borrowed from ActiveSupport
56
+ # no need require an entire lib when we only need one method
57
+ def underscore(camel_cased_word)
58
+ word = camel_cased_word.to_s.dup
59
+ word.gsub!(/::/, '/')
60
+ word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
61
+ word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
62
+ word.tr!("-", "_")
63
+ word.downcase!
64
+ word
65
+ end
66
+
67
+ end
68
+ end
@@ -0,0 +1,11 @@
1
+ module Wordpress
2
+
3
+ module VERSION #:nodoc:
4
+ MAJOR = 0
5
+ MINOR = 1
6
+ PATCH = 23
7
+ PRE = nil
8
+ STRING = [MAJOR, MINOR, PATCH, PRE].compact.join('.')
9
+ end
10
+
11
+ end
data/wordpress.gemspec ADDED
@@ -0,0 +1,28 @@
1
+ # encoding: utf-8
2
+ require File.expand_path('../lib/wordpress/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+
6
+ gem.authors = ["Magda Sikorska"]
7
+ gem.email = ['madzia.sikorska@gmail.com']
8
+ gem.description = %q{Ruby wrapper for the Wordpress API}
9
+ gem.homepage = 'http://github.com/elrosa/wordpress_api'
10
+ gem.name = 'wordpress_api'
11
+ gem.summary = gem.description
12
+ gem.version = Wordpress::VERSION::STRING
13
+
14
+ gem.files = `git ls-files`.split("\n")
15
+ gem.require_paths = ['lib']
16
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+
18
+ gem.add_dependency 'hashie', '~> 1.2.0'
19
+ gem.add_dependency 'multi_json', '~> 1.0.3'
20
+ gem.add_dependency 'omniauth-oauth2', '~> 1.0.0'
21
+
22
+ gem.add_development_dependency 'json', '~> 1.6'
23
+ gem.add_development_dependency 'rake', '~> 0.9'
24
+ gem.add_development_dependency 'rdoc', '~> 3.8'
25
+ gem.add_development_dependency 'rspec', '~> 2.6'
26
+ gem.add_development_dependency 'webmock', '~> 1.7'
27
+
28
+ end
metadata ADDED
@@ -0,0 +1,151 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wordpress_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.23
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Magda Sikorska
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-08-24 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: hashie
16
+ requirement: &24268220 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 1.2.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *24268220
25
+ - !ruby/object:Gem::Dependency
26
+ name: multi_json
27
+ requirement: &24267060 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 1.0.3
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *24267060
36
+ - !ruby/object:Gem::Dependency
37
+ name: omniauth-oauth2
38
+ requirement: &24265660 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: 1.0.0
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *24265660
47
+ - !ruby/object:Gem::Dependency
48
+ name: json
49
+ requirement: &24263220 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.6'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *24263220
58
+ - !ruby/object:Gem::Dependency
59
+ name: rake
60
+ requirement: &24278180 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ~>
64
+ - !ruby/object:Gem::Version
65
+ version: '0.9'
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *24278180
69
+ - !ruby/object:Gem::Dependency
70
+ name: rdoc
71
+ requirement: &24277100 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ~>
75
+ - !ruby/object:Gem::Version
76
+ version: '3.8'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: *24277100
80
+ - !ruby/object:Gem::Dependency
81
+ name: rspec
82
+ requirement: &24276040 !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ~>
86
+ - !ruby/object:Gem::Version
87
+ version: '2.6'
88
+ type: :development
89
+ prerelease: false
90
+ version_requirements: *24276040
91
+ - !ruby/object:Gem::Dependency
92
+ name: webmock
93
+ requirement: &24275260 !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ~>
97
+ - !ruby/object:Gem::Version
98
+ version: '1.7'
99
+ type: :development
100
+ prerelease: false
101
+ version_requirements: *24275260
102
+ description: Ruby wrapper for the Wordpress API
103
+ email:
104
+ - madzia.sikorska@gmail.com
105
+ executables: []
106
+ extensions: []
107
+ extra_rdoc_files: []
108
+ files:
109
+ - .document
110
+ - .gitignore
111
+ - Gemfile
112
+ - README
113
+ - Rakefile
114
+ - lib/wordpress.rb
115
+ - lib/wordpress/api.rb
116
+ - lib/wordpress/api/reader.rb
117
+ - lib/wordpress/api/writer.rb
118
+ - lib/wordpress/client.rb
119
+ - lib/wordpress/errors.rb
120
+ - lib/wordpress/helpers.rb
121
+ - lib/wordpress/helpers/authorization.rb
122
+ - lib/wordpress/helpers/request.rb
123
+ - lib/wordpress/mash.rb
124
+ - lib/wordpress/version.rb
125
+ - wordpress.gemspec
126
+ homepage: http://github.com/elrosa/wordpress_api
127
+ licenses: []
128
+ post_install_message:
129
+ rdoc_options: []
130
+ require_paths:
131
+ - lib
132
+ required_ruby_version: !ruby/object:Gem::Requirement
133
+ none: false
134
+ requirements:
135
+ - - ! '>='
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ required_rubygems_version: !ruby/object:Gem::Requirement
139
+ none: false
140
+ requirements:
141
+ - - ! '>='
142
+ - !ruby/object:Gem::Version
143
+ version: '0'
144
+ requirements: []
145
+ rubyforge_project:
146
+ rubygems_version: 1.8.11
147
+ signing_key:
148
+ specification_version: 3
149
+ summary: Ruby wrapper for the Wordpress API
150
+ test_files: []
151
+ has_rdoc: