wp-api-client 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c50b50f037a644caac4f4b7a62151dc611b97744
4
- data.tar.gz: 6c5bad2367af9c5965f5d2ccd7aeccd06b24bb08
3
+ metadata.gz: 3c79a386294cc7fb4cba0a13895836546f44d6bd
4
+ data.tar.gz: 95f7761c08ea70c9d00d7a93f5581cc1428f2393
5
5
  SHA512:
6
- metadata.gz: 1d095bc06ef8e4d2a3575c321ba965f6e55ef05526b2d6a4f1133bd9576eccdf0ca5838a5fa7a6a608cb946a8e8190c28c5bbebeb888a3f514f0cf37b3ae67d3
7
- data.tar.gz: ea828e89a0b046c617fae2a6ec55639326a0ed579dd63853210c5af8482e6d6b7e273bd806eed244e784746d3682584c90a0cd837e575edac7f39e59dce0dd99
6
+ metadata.gz: ee69105e832eb279cf28c6b8d29ecb38a83e5ceb3d086b8b56297bf62285f6087f85dfa91d62c26d99bf601b5e07b98edf153de79023f7ff3d5cece72097cb5d
7
+ data.tar.gz: 48bc17fffdde6b9a2f7badf75d4864ea2809646451a35f4f55abc99b6f9e808eb8eb954e117dd3f2cb0d73e93c10c8ae89007234d3baf169b389d8758b4d4a96
data/README.md CHANGED
@@ -4,6 +4,8 @@ This unambitious client provides read-only access for WP-API v2.
4
4
 
5
5
  It supports authentication via OAuth or Basic Auth.
6
6
 
7
+ It can make concurrent requests.
8
+
7
9
  It does not support comments, users or POST requests.
8
10
 
9
11
  It requires **Ruby 2.3** and is tested against **WP-API 2.0-beta12**.
@@ -72,7 +74,7 @@ posts.total_available
72
74
  next_page = @api.get(posts.next_page)
73
75
  # => #<WpApiClient::Collection:0x00bbcafe938827 @resources=[#<WpApiClient::Entities::Post...
74
76
 
75
- page_after_that = @api.get(next_page.page_after_that)
77
+ page_after_that = @api.get(next_page.next_page)
76
78
  # => #<WpApiClient::Collection:0x00bbcafe938827 @resources=[#<WpApiClient::Entities::Post...
77
79
  ```
78
80
 
@@ -158,6 +160,23 @@ end
158
160
  client = WpApiClient.get_client
159
161
  ```
160
162
 
163
+ ## Concurrency
164
+
165
+ WP-API is _slow_: a typical request takes 0.5s. To mitigate this, I recommend
166
+ caching all your responses sensibly, and when you need to fetch, do so concurrently
167
+ as far as is possible.
168
+
169
+ ```ruby
170
+ results = []
171
+ client.concurrently do |api|
172
+ results << api.get('post/1')
173
+ results << api.get('post/2')
174
+ results << api.get('post/3')
175
+ end
176
+ results
177
+ # => [#<WpApiClient::Entities::Post>, #<WpApiClient::Entities::Post, #<WpApiClient::Entities::Post>]
178
+ ```
179
+
161
180
  ## Testing and compatibility
162
181
 
163
182
  This library comes with VCR cassettes recorded against a local WP installation
@@ -11,6 +11,7 @@ require "wp_api_client/entities/image"
11
11
  require "wp_api_client/entities/types"
12
12
 
13
13
  require "wp_api_client/client"
14
+ require "wp_api_client/concurrent_client"
14
15
  require "wp_api_client/connection"
15
16
  require "wp_api_client/collection"
16
17
  require "wp_api_client/relationship"
@@ -6,10 +6,21 @@ module WpApiClient
6
6
  end
7
7
 
8
8
  def get(url, params = {})
9
- response = @connection.get(api_path_from(url), params)
10
- @headers = response.headers
9
+ if @concurrent_client
10
+ @concurrent_client.get(api_path_from(url), params)
11
+ else
12
+ response = @connection.get(api_path_from(url), params)
13
+ @headers = response.headers
14
+ native_representation_of response.body
15
+ end
16
+ end
11
17
 
12
- native_representation_of response.body
18
+ def concurrently
19
+ @concurrent_client ||= ConcurrentClient.new(@connection)
20
+ yield @concurrent_client
21
+ result = @concurrent_client.run
22
+ @concurrent_client = nil
23
+ result
13
24
  end
14
25
 
15
26
  private
@@ -24,7 +24,7 @@ module WpApiClient
24
24
  def previous_page
25
25
  @links[:prev] && @links[:prev]
26
26
  end
27
-
27
+
28
28
  private
29
29
 
30
30
  # https://www.snip2code.com/Snippet/71914/Parse-link-headers-from-Github-API-in-Ru
@@ -0,0 +1,15 @@
1
+ module WpApiClient
2
+ class ConcurrentClient < Client
3
+
4
+ def get(url, params = {})
5
+ @queue ||= []
6
+ @queue << [api_path_from(url), params]
7
+ end
8
+
9
+ def run
10
+ responses = @connection.get_concurrently(@queue)
11
+ responses.map { |r| native_representation_of(r.body) }
12
+ end
13
+
14
+ end
15
+ end
@@ -1,14 +1,18 @@
1
1
  require 'faraday'
2
2
  require 'faraday_middleware'
3
3
  require 'faraday-http-cache'
4
+ require 'typhoeus'
5
+ require 'typhoeus/adapters/faraday'
4
6
 
5
7
  module WpApiClient
6
8
  class Connection
7
9
 
8
10
  attr_accessor :headers
11
+ attr_reader :concurrent
9
12
 
10
13
  def initialize(configuration)
11
14
  @configuration = configuration
15
+ @queue = []
12
16
  @conn = Faraday.new(url: configuration.endpoint) do |faraday|
13
17
 
14
18
  if configuration.oauth_credentials
@@ -21,21 +25,47 @@ module WpApiClient
21
25
 
22
26
  if configuration.debug
23
27
  faraday.response :logger
28
+ faraday.use :instrumentation
24
29
  end
25
30
 
26
31
  if configuration.cache
27
- faraday.use :http_cache, store: configuration.cache
32
+ faraday.use :http_cache, store: configuration.cache, shared_cache: false
28
33
  end
29
34
 
30
35
  faraday.use Faraday::Response::RaiseError
31
36
  faraday.response :json, :content_type => /\bjson$/
32
- faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
37
+ faraday.adapter :typhoeus
33
38
  end
34
39
  end
35
40
 
36
41
  # translate requests into wp-api urls
37
42
  def get(url, params = {})
38
- @conn.get url, params.merge(@configuration.request_params)
43
+ @conn.get url, parse_params(params)
39
44
  end
45
+
46
+ # requests come in as url/params pairs
47
+ def get_concurrently(requests)
48
+ responses = []
49
+ @conn.in_parallel do
50
+ requests.map do |r|
51
+ responses << get(r[0], r[1])
52
+ end
53
+ end
54
+ responses
55
+ end
56
+
57
+ private
58
+
59
+ def parse_params(params)
60
+ params = @configuration.request_params.merge(params)
61
+ # if _embed is present at all it will have the effect of embedding —
62
+ # even if it's set to "false"
63
+ if params[:_embed] == false
64
+ params.delete(:_embed)
65
+ end
66
+ params
67
+ end
68
+
69
+
40
70
  end
41
71
  end
@@ -101,7 +101,14 @@ Available mappings are :post, :term, and :meta.}
101
101
  unless position.nil?
102
102
  location = @resource["_links"].dig(relationship, position.to_i, "href")
103
103
  else
104
- location = @resource["_links"][relationship]["href"]
104
+ if @resource["_links"][relationship].is_a? Array
105
+ # If the resources are linked severally, crank through and
106
+ # retrieve them one by one as an array
107
+ return @resource["_links"][relationship].map { |link| WpApiClient.get_client.get(link["href"]) }
108
+ else
109
+ # Otherwise, get the single link to the lot
110
+ location = @resource["_links"][relationship]["href"]
111
+ end
105
112
  end
106
113
  WpApiClient.get_client.get(location) if location
107
114
  end
@@ -1,3 +1,3 @@
1
1
  module WpApiClient
2
- VERSION = "0.2.2"
2
+ VERSION = "0.2.3"
3
3
  end
@@ -32,6 +32,7 @@ Gem::Specification.new do |spec|
32
32
  spec.add_dependency "faraday_middleware", "~> 0.10"
33
33
  spec.add_dependency "faraday-http-cache", "~> 1.2"
34
34
  spec.add_dependency "simple_oauth", "~> 0.3"
35
+ spec.add_dependency "typhoeus", "~> 1.0"
35
36
 
36
37
  spec.add_development_dependency "bundler", "~> 1.11"
37
38
  spec.add_development_dependency "rake", "~> 10.0"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wp-api-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Duncan Brown
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-03-06 00:00:00.000000000 Z
11
+ date: 2016-03-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0.3'
69
+ - !ruby/object:Gem::Dependency
70
+ name: typhoeus
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.0'
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: bundler
71
85
  requirement: !ruby/object:Gem::Requirement
@@ -142,6 +156,7 @@ files:
142
156
  - lib/wp_api_client.rb
143
157
  - lib/wp_api_client/client.rb
144
158
  - lib/wp_api_client/collection.rb
159
+ - lib/wp_api_client/concurrent_client.rb
145
160
  - lib/wp_api_client/configuration.rb
146
161
  - lib/wp_api_client/connection.rb
147
162
  - lib/wp_api_client/entities/base.rb