ct_wordpress-client 0.2.8 → 0.2.10

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.
Files changed (53) hide show
  1. checksums.yaml +15 -0
  2. data/.coveralls.yml +1 -0
  3. data/.gitignore +24 -0
  4. data/.travis.yml +8 -0
  5. data/CONTRIBUTING.md +16 -0
  6. data/Gemfile +3 -0
  7. data/LICENSE +20 -0
  8. data/README.md +80 -0
  9. data/Rakefile +5 -0
  10. data/lib/faraday_middleware/gzip.rb +22 -0
  11. data/lib/wordpress-client.rb +43 -0
  12. data/lib/wordpress/api.rb +62 -0
  13. data/lib/wordpress/api/comments.rb +48 -0
  14. data/lib/wordpress/api/follow.rb +20 -0
  15. data/lib/wordpress/api/freshly_pressed.rb +10 -0
  16. data/lib/wordpress/api/me.rb +18 -0
  17. data/lib/wordpress/api/media.rb +15 -0
  18. data/lib/wordpress/api/notifications.rb +25 -0
  19. data/lib/wordpress/api/oauth.rb +22 -0
  20. data/lib/wordpress/api/posts.rb +79 -0
  21. data/lib/wordpress/api/sites.rb +12 -0
  22. data/lib/wordpress/api/taxonomy.rb +45 -0
  23. data/lib/wordpress/api/tests.rb +25 -0
  24. data/lib/wordpress/base.rb +51 -0
  25. data/lib/wordpress/client.rb +68 -0
  26. data/lib/wordpress/configuration.rb +6 -0
  27. data/lib/wordpress/errors.rb +25 -0
  28. data/lib/wordpress/object/array.rb +30 -0
  29. data/lib/wordpress/object/base.rb +94 -0
  30. data/lib/wordpress/object/comment.rb +11 -0
  31. data/lib/wordpress/object/comments.rb +8 -0
  32. data/lib/wordpress/object/media.rb +7 -0
  33. data/lib/wordpress/object/meta_object.rb +65 -0
  34. data/lib/wordpress/object/oauth.rb +7 -0
  35. data/lib/wordpress/object/post.rb +10 -0
  36. data/lib/wordpress/object/posts.rb +8 -0
  37. data/lib/wordpress/object/site.rb +8 -0
  38. data/lib/wordpress/object/test.rb +7 -0
  39. data/lib/wordpress/object/user.rb +8 -0
  40. data/lib/wordpress/ostruct.rb +64 -0
  41. data/lib/wordpress/request.rb +29 -0
  42. data/lib/wordpress/version.rb +14 -0
  43. data/spec/spec_helper.rb +15 -0
  44. data/spec/wordpress-client_spec.rb +25 -0
  45. data/spec/wordpress/api_spec.rb +90 -0
  46. data/spec/wordpress/base_spec.rb +63 -0
  47. data/spec/wordpress/client_spec.rb +91 -0
  48. data/spec/wordpress/object/base_spec.rb +56 -0
  49. data/spec/wordpress/object/meta_object_spec.rb +20 -0
  50. data/spec/wordpress/ostruct_spec.rb +58 -0
  51. data/spec/wordpress/request_spec.rb +15 -0
  52. data/wordpress-client.gemspec +26 -0
  53. metadata +158 -90
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ MTliZDY3OWVjNmJhMTQzOWZmYzA2MGM2ZTFjNWViZTc5YTM1MDcxOA==
5
+ data.tar.gz: !binary |-
6
+ ZjYyZWEzMmI4YjNhMDBlY2U1NTdkMjM3ZGJhMGQzOTk1YmE1YzAzZg==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ ZTBhNDk4MzRjOWFkM2NiNDI0OTM4ZWRmZGM2ZmFmYjQwMDIyNjU2OTA1NmQ2
10
+ MmZkOGE1YTA5YmUzZDJkMGY3ODg0ODgzNThlM2UyZmU0NWE3NWU5OTNlZWVh
11
+ OTJjNWY2NWZhM2Q4OGYzMGFjNDdkMjlmOTlmMGI1MDk1NWI1Zjg=
12
+ data.tar.gz: !binary |-
13
+ ZDRhM2VjY2JmZDczMDVlZTM4MjNhYWNhMzFhNjIzNjIxODA5ZmUzNmYzMTA1
14
+ M2Y3OTZmZTI2ZWNjYmFhYTRmMDFlYTQ3ODE4MmFhYmEwNDE4OTYxYTE1NGQ2
15
+ ZGFiYWExOWY0YjNkNDAwN2UwNGUwMjMyN2QxYjBhZDNhNjA5MTU=
data/.coveralls.yml ADDED
@@ -0,0 +1 @@
1
+ service_name: travis-ci
data/.gitignore ADDED
@@ -0,0 +1,24 @@
1
+ .rvmrc
2
+ .rspec
3
+ .bundle
4
+ .config
5
+
6
+ coverage
7
+ vendor
8
+ log
9
+ tmp
10
+ coverage
11
+ spec/tmp
12
+ spec/reports
13
+
14
+ Gemfile.lock
15
+
16
+ # YARD artifacts
17
+ .yardoc
18
+ _yardoc
19
+ doc/
20
+
21
+ *.gem
22
+ *.rbc
23
+ **.orig
24
+ **.org
data/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ branches:
3
+ only:
4
+ - master
5
+ rvm:
6
+ - 1.9.3
7
+ - 2.0.0
8
+ - 2.1.0
data/CONTRIBUTING.md ADDED
@@ -0,0 +1,16 @@
1
+ Contributor Policy
2
+ =================
3
+
4
+ Commit bit. If you have a commit accepted into the project then you get full git access to the repo. If I don't give you this in a timely manner just send me a message.
5
+
6
+ Testing
7
+ ======
8
+
9
+ Please don't commit code without tests. You can bootstrap the development environment by running `bundle install`. After that, running `rake` should just work. If it doesn't then file a bug.
10
+
11
+ Versioning
12
+ =========
13
+
14
+ Don't bump the version in any changes you make or pull in to the project. I'll maintain rights to push the gem to rubygems.org and make releases when appropriate.
15
+
16
+ And please keep the README up to date. Thank you!
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2013 dtaniwaki
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,80 @@
1
+ # wordpress-client
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/wordpress-client.png)](https://rubygems.org/gems/wordpress-client) [![Build Status](https://travis-ci.org/dtaniwaki/wordpress-client.png)](https://travis-ci.org/dtaniwaki/wordpress-client) [![Coverage Status](https://coveralls.io/repos/dtaniwaki/wordpress-client/badge.png)](https://coveralls.io/r/dtaniwaki/wordpress-client)
4
+
5
+ wordpress-client is a [Wordpress Rest API Wrapper](http://developer.wordpress.com/docs/api/).
6
+
7
+ ## Requirements
8
+
9
+ I test with ruby 1.9.3 and 2.1.0. Other versions/VMs are untested but might work fine.
10
+
11
+ ## Installation
12
+
13
+ To your system
14
+
15
+ ```ruby
16
+ gem install wordpress-client
17
+ ```
18
+
19
+ Or add wordpress-client to your `Gemfile` and `bundle install`:
20
+
21
+ ```ruby
22
+ gem 'wordpress-client'
23
+ ```
24
+
25
+ ## Usage
26
+
27
+ Create new client
28
+
29
+ ```ruby
30
+ require 'wordpress-client'
31
+
32
+ # Shared client
33
+ client = Wordpress.client
34
+
35
+ # New client
36
+ client = Wordpress::Client.new
37
+ ```
38
+
39
+ Call API without access token
40
+
41
+ ```ruby
42
+ # Get posts of a specific domain without access token
43
+ posts = client.get_posts('wordpress-site-domain')
44
+ posts.each do |post|
45
+ # Do something
46
+ end
47
+
48
+ # Get all the posts of a specific domain without access token
49
+ page = 1
50
+ loop do
51
+ posts = client.get_posts('wordpress-site-domain', :page => page)
52
+ posts.each do |post|
53
+ # Do something
54
+ end
55
+ break if posts.size == 0
56
+ page += 1
57
+ end
58
+ ```
59
+
60
+ Call API with access token
61
+
62
+ ```ruby
63
+ # Get user by access token
64
+ client.access_token = 'YOUR_ACCESS_TOKEN' # if you have any
65
+ user = client.me
66
+ site = user.site
67
+ posts = site.posts
68
+ ```
69
+
70
+ ## TODO
71
+ - Support create/update/delete from Wordpress::Object
72
+ - Add oauth method
73
+
74
+ ## Contributing
75
+
76
+ See the [contributing guide](https://github.com/dtaniwaki/wordpress-client/blob/master/CONTRIBUTING.md).
77
+
78
+ ## Copyright
79
+
80
+ Copyright (c) 2013-2013 Daisuke Taniwaki. See [LICENSE](https://github.com/dtaniwaki/wordpress-client/blob/master/LICENSE) for details.
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ require 'rspec/core/rake_task'
2
+
3
+ RSpec::Core::RakeTask.new(:spec)
4
+
5
+ task :default => :spec
@@ -0,0 +1,22 @@
1
+ require 'faraday'
2
+ require 'zlib'
3
+
4
+ module FaradayMiddleware
5
+ class Gzip < Faraday::Response::Middleware
6
+
7
+ def on_complete(env)
8
+ encoding = env[:response_headers]['content-encoding'].to_s.downcase
9
+ case encoding
10
+ when 'gzip'
11
+ env[:body] = Zlib::GzipReader.new(StringIO.new(env[:body]), :encoding => 'ASCII-8BIT').read
12
+ env[:response_headers].delete('content-encoding')
13
+ when 'deflate'
14
+ env[:body] = Zlib::Inflate.inflate(env[:body])
15
+ env[:response_headers].delete('content-encoding')
16
+ end
17
+ end
18
+
19
+ end
20
+ end
21
+
22
+ Faraday::Response.register_middleware :gzip => FaradayMiddleware::Gzip
@@ -0,0 +1,43 @@
1
+ unless Kernel.respond_to?(:require_relative)
2
+ module Kernel
3
+ def require_relative(relative_path)
4
+ desired_path = File.expand_path('../' + relative_path.to_str, caller[0])
5
+ shortest = desired_path
6
+ $:.each do |path|
7
+ path += '/'
8
+ if desired_path.index(path) == 0
9
+ candidate = desired_path.sub(path, '')
10
+ shortest = candidate if candidate.size < shortest.size
11
+ end
12
+ end
13
+ require shortest
14
+ end
15
+ end
16
+ end
17
+
18
+ require 'logger'
19
+ require 'wordpress/version'
20
+ require 'wordpress/client'
21
+
22
+ module Wordpress
23
+ class << self
24
+ attr_writer :logger
25
+
26
+ def client
27
+ @client ||= Wordpress::Client.new
28
+ end
29
+
30
+ def logger
31
+ @logger ||= Logger.new(STDOUT)
32
+ end
33
+
34
+ def configure(&block)
35
+ block.call(configuration)
36
+ configuration
37
+ end
38
+
39
+ def configuration
40
+ @configuration ||= Configuration.new
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,62 @@
1
+ require 'wordpress/errors'
2
+ require 'wordpress/request'
3
+ Dir.glob(File.expand_path('../api/*.rb', __FILE__)) do |path|
4
+ require path
5
+ end
6
+
7
+ module Wordpress
8
+ module API
9
+ def self.included(base)
10
+ base.send(:include, InstanceMethods)
11
+ base.extend(ClassMethods)
12
+ base.class_eval do
13
+ include Posts
14
+ include Comments
15
+ include Sites
16
+ include Me
17
+ include Tests
18
+ include Follow
19
+ include Taxonomy
20
+ include Notifications
21
+ include FreshlyPressed
22
+ include Oauth
23
+ include Media
24
+ end
25
+ end
26
+
27
+ module ClassMethods
28
+ end
29
+
30
+ module InstanceMethods
31
+ def exec_api(request, &response)
32
+ json = call(request, :bearer_token_request => true)
33
+
34
+ if !json['error'].nil?
35
+ raise Wordpress::ResponseError, json
36
+ end
37
+
38
+ if response.nil?
39
+ Wordpress::OpenStruct.new(json)
40
+ else
41
+ instance_exec(json, &response)
42
+ end
43
+ end
44
+
45
+ private
46
+
47
+ def validate_keys!(params, valid_keys)
48
+ params.keys.each do |key|
49
+ raise Wordpress::ArgumentError, "#{key} is not valid for this method." unless valid_keys.include?(key)
50
+ end
51
+ end
52
+
53
+ def default_keys
54
+ [:http_envelope, :pretty, :meta, :callback].freeze
55
+ end
56
+
57
+ def search_keys
58
+ [:number, :offset, :page, :order, :order_by, :after, :before].freeze
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,48 @@
1
+ require "wordpress/object/comments"
2
+ require "wordpress/object/comment"
3
+
4
+ module Wordpress::API
5
+ module Comments
6
+ def get_comments(site, params = {})
7
+ validate_keys! params, [:context, :type, :status] + default_keys + search_keys
8
+ exec_api(Wordpress::Request.new(:get, "/rest/v1.1/sites/#{site}/comments", params)) do |json|
9
+ Wordpress::Object::Comments.new(self, json)
10
+ end
11
+ end
12
+
13
+ def get_post_comments(site, post_id, params = {})
14
+ validate_keys! params, [:context] + default_keys
15
+ exec_api(Wordpress::Request.new(:get, "/rest/v1.1/sites/#{site}/posts/#{post_id}/replies", params)) do |json|
16
+ Wordpress::Object::Comments.new(self, json)
17
+ end
18
+ end
19
+
20
+ def get_comment(site, comment_id, params = {})
21
+ validate_keys! params, [:context] + default_keys
22
+ exec_api(Wordpress::Request.new(:get, "/rest/v1.1/sites/#{site}/comments/#{comment_id}", params)) do |json|
23
+ Wordpress::Object::Comment.new(self, json)
24
+ end
25
+ end
26
+
27
+ def update_comment(site, comment_id, data, params = {})
28
+ validate_keys! params, [:context] + default_keys
29
+ exec_api(Wordpress::Request.new(:post, "/rest/v1.1/sites/#{site}/comments/#{comment_id}", params, data)) do |json|
30
+ Wordpress::Object::Comment.new(self, json)
31
+ end
32
+ end
33
+
34
+ def create_comment(site, post_id, data, params = {})
35
+ validate_keys! params, [:context] + default_keys
36
+ exec_api(Wordpress::Request.new(:post, "/rest/v1.1/sites/#{site}/posts/#{post_id}/replies/new", params, data)) do |json|
37
+ Wordpress::Object::Comment.new(self, json)
38
+ end
39
+ end
40
+
41
+ def delete_comment(site, comment_id, data, params = {})
42
+ validate_keys! params, [:context] + default_keys
43
+ exec_api(Wordpress::Request.new(:post, "/rest/v1.1/sites/#{site}/comments/#{comment_id}/delete", params, data)) do |json|
44
+ Wordpress::Object::Comment.new(self, json)
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,20 @@
1
+ require "wordpress/ostruct"
2
+
3
+ module Wordpress::API
4
+ module Follow
5
+ def create_follow(site, data, params = {})
6
+ validate_keys! params, [:context] + default_keys
7
+ exec_api(Wordpress::Request.new(:post, "/rest/v1.1/sites/#{site}/follows/new", params, data))
8
+ end
9
+
10
+ def delete_follow(site, data, params = {})
11
+ validate_keys! params, [:context] + default_keys
12
+ exec_api(Wordpress::Request.new(:post, "/rest/v1.1/sites/#{site}/follows/mine/delete", params, data))
13
+ end
14
+
15
+ def get_follow(site, params = {})
16
+ validate_keys! params, [:context] + default_keys
17
+ exec_api(Wordpress::Request.new(:get, "/rest/v1.1/sites/#{site}/follows/mine", params))
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,10 @@
1
+ require "wordpress/ostruct"
2
+
3
+ module Wordpress::API
4
+ module FreshlyPressed
5
+ def freshly_pressed(params = {})
6
+ validate_keys! params, [:number, :after, :before, :content_width, :thumb_width, :thumb_height] + default_keys
7
+ exec_api(Wordpress::Request.new(:get, "/rest/v1.1/freshly-pressed", params))
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,18 @@
1
+ require "wordpress/ostruct"
2
+ require "wordpress/object/user"
3
+
4
+ module Wordpress::API
5
+ module Me
6
+ def me(params = {})
7
+ validate_keys! params, default_keys
8
+ exec_api(Wordpress::Request.new(:get, "/rest/v1.1/me", params)) do |json|
9
+ Wordpress::Object::User.new(self, json)
10
+ end
11
+ end
12
+
13
+ def get_my_likes(params = {})
14
+ validate_keys! params, default_keys
15
+ exec_api(Wordpress::Request.new(:get, "/rest/v1.1/me/likes", params))
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,15 @@
1
+ require "wordpress/ostruct"
2
+ require "wordpress/object/media"
3
+ module Wordpress::API
4
+ module Media
5
+
6
+ def add_media(site, params = {})
7
+ validate_keys! params, [:context, :media, :media_urls,:attrs] + default_keys
8
+ exec_api(Wordpress::Request.new(:post, "/rest/v1.1/sites/#{site}/media/new",nil, params)) do |json|
9
+ Wordpress::Object::Media.new( self, json )
10
+ end
11
+ end
12
+
13
+ end
14
+ end
15
+
@@ -0,0 +1,25 @@
1
+ require "wordpress/ostruct"
2
+
3
+ module Wordpress::API
4
+ module Notifications
5
+ def get_notifications(params = {})
6
+ validate_keys! params, [:ids, :fields, :type, :unread, :read, :number, :since, :before, :num_note_items, :trap] + default_keys
7
+ exec_api(Wordpress::Request.new(:get, "/rest/v1.1/notifications", params))
8
+ end
9
+
10
+ def get_notification(note_id, params = {})
11
+ validate_keys! params, [:context, :fields, :num_note_items] + default_keys
12
+ exec_api(Wordpress::Request.new(:get, "/rest/v1.1/notifications/#{note_id}", params))
13
+ end
14
+
15
+ def see_notifications(data, params = {})
16
+ validate_keys! params, [:context] + default_keys
17
+ exec_api(Wordpress::Request.new(:post, "/rest/v1.1/notifications/seen", params, data))
18
+ end
19
+
20
+ def read_notifications(data, params = {})
21
+ validate_keys! params, [:context] + default_keys
22
+ exec_api(Wordpress::Request.new(:post, "/rest/v1.1/notifications/read", params, data))
23
+ end
24
+ end
25
+ end