wp-api-client 0.2.3 → 0.3.0

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: 3c79a386294cc7fb4cba0a13895836546f44d6bd
4
- data.tar.gz: 95f7761c08ea70c9d00d7a93f5581cc1428f2393
3
+ metadata.gz: e6daac342406211d577fc07b8948ab96358589a8
4
+ data.tar.gz: 3df23ccd037feb4eef033e416a50942babf49672
5
5
  SHA512:
6
- metadata.gz: ee69105e832eb279cf28c6b8d29ecb38a83e5ceb3d086b8b56297bf62285f6087f85dfa91d62c26d99bf601b5e07b98edf153de79023f7ff3d5cece72097cb5d
7
- data.tar.gz: 48bc17fffdde6b9a2f7badf75d4864ea2809646451a35f4f55abc99b6f9e808eb8eb954e117dd3f2cb0d73e93c10c8ae89007234d3baf169b389d8758b4d4a96
6
+ metadata.gz: 6f02a6f121e4cb8be779c49e4457fc9029f483955017a2e077e0a448bf1754646bb28fb31261a9b52f7ec0deed31d1eb001f7938b28da274937780cea001dca6
7
+ data.tar.gz: 70289be078d5a64ac1db18376cc79c22fea58f24d4a8acf400a7adf61da962c3ca535859e1a76d34c434705ac6527c6cbb5cd532b52db36dbdc2f924703d2d5b
data/lib/wp_api_client.rb CHANGED
@@ -1,3 +1,5 @@
1
+ require "ostruct"
2
+
1
3
  require "wp_api_client/version"
2
4
  require "wp_api_client/configuration"
3
5
 
@@ -8,6 +10,7 @@ require "wp_api_client/entities/meta"
8
10
  require "wp_api_client/entities/taxonomy"
9
11
  require "wp_api_client/entities/term"
10
12
  require "wp_api_client/entities/image"
13
+ require "wp_api_client/entities/error"
11
14
  require "wp_api_client/entities/types"
12
15
 
13
16
  require "wp_api_client/client"
@@ -17,6 +20,7 @@ require "wp_api_client/collection"
17
20
  require "wp_api_client/relationship"
18
21
 
19
22
  module WpApiClient
23
+
20
24
  def self.get_client
21
25
  @client ||= Client.new(Connection.new(configuration))
22
26
  end
@@ -27,4 +31,14 @@ module WpApiClient
27
31
  end
28
32
 
29
33
  class RelationNotDefined < StandardError; end
34
+ class ErrorResponse < StandardError
35
+
36
+ attr_reader :error
37
+ attr_reader :status
38
+
39
+ def initialize(json)
40
+ @error = OpenStruct.new(json)
41
+ @status = @error.data["status"]
42
+ end
43
+ end
30
44
  end
@@ -24,7 +24,11 @@ module WpApiClient
24
24
  def previous_page
25
25
  @links[:prev] && @links[:prev]
26
26
  end
27
-
27
+
28
+ def method_missing(method, *args)
29
+ @resources.send(method, *args)
30
+ end
31
+
28
32
  private
29
33
 
30
34
  # https://www.snip2code.com/Snippet/71914/Parse-link-headers-from-Github-API-in-Ru
@@ -23,6 +23,7 @@ module WpApiClient
23
23
  attr_accessor :debug
24
24
  attr_accessor :cache
25
25
  attr_accessor :basic_auth
26
+ attr_accessor :proxy
26
27
 
27
28
  def initialize
28
29
  @endpoint = 'http://localhost:8080/wp-json/wp/v2'
@@ -32,6 +32,10 @@ module WpApiClient
32
32
  faraday.use :http_cache, store: configuration.cache, shared_cache: false
33
33
  end
34
34
 
35
+ if configuration.proxy
36
+ faraday.proxy configuration.proxy
37
+ end
38
+
35
39
  faraday.use Faraday::Response::RaiseError
36
40
  faraday.response :json, :content_type => /\bjson$/
37
41
  faraday.adapter :typhoeus
@@ -1,4 +1,5 @@
1
1
  require 'open-uri'
2
+ require 'ostruct'
2
3
 
3
4
  module WpApiClient
4
5
  module Entities
@@ -12,10 +13,10 @@ module WpApiClient
12
13
  end
13
14
 
14
15
  def initialize(resource)
15
- unless resource.is_a? Hash
16
+ unless resource.is_a? Hash or resource.is_a? OpenStruct
16
17
  raise ArgumentError.new('Tried to initialize a WP-API resource with something other than a Hash')
17
18
  end
18
- @resource = resource
19
+ @resource = OpenStruct.new(resource)
19
20
  end
20
21
 
21
22
  def links
@@ -31,6 +32,10 @@ module WpApiClient
31
32
  relations
32
33
  end
33
34
  end
35
+
36
+ def method_missing(method, *args)
37
+ @resource.send(method, *args)
38
+ end
34
39
  end
35
40
  end
36
41
  end
@@ -0,0 +1,14 @@
1
+ module WpApiClient
2
+ module Entities
3
+ class Error
4
+
5
+ def self.represents?(json)
6
+ json.key?("code") and json.key?("message")
7
+ end
8
+
9
+ def initialize(json)
10
+ raise WpApiClient::ErrorResponse.new(json)
11
+ end
12
+ end
13
+ end
14
+ end
@@ -7,13 +7,6 @@ module WpApiClient
7
7
  json["key"] and json["value"]
8
8
  end
9
9
 
10
- def key
11
- meta["key"]
12
- end
13
-
14
- def value
15
- meta["value"]
16
- end
17
10
  end
18
11
  end
19
12
  end
@@ -11,10 +11,6 @@ module WpApiClient
11
11
  post["title"]["rendered"]
12
12
  end
13
13
 
14
- def slug
15
- post["slug"]
16
- end
17
-
18
14
  def date
19
15
  Time.parse(post["date_gmt"]) if post["date_gmt"]
20
16
  end
@@ -27,10 +23,6 @@ module WpApiClient
27
23
  post["excerpt"]["rendered"]
28
24
  end
29
25
 
30
- def id
31
- post["id"]
32
- end
33
-
34
26
  def terms(taxonomy = nil)
35
27
  relations("https://api.w.org/term", taxonomy)
36
28
  end
@@ -7,10 +7,6 @@ module WpApiClient
7
7
  !json.dig("hierarchical").nil?
8
8
  end
9
9
 
10
- def name
11
- taxonomy["name"]
12
- end
13
-
14
10
  def terms
15
11
  relations("https://api.w.org/items")
16
12
  end
@@ -15,18 +15,6 @@ module WpApiClient
15
15
  relations("http://api.w.org/v2/post_type", post_type)
16
16
  end
17
17
 
18
- def name
19
- term["name"]
20
- end
21
-
22
- def slug
23
- term["slug"]
24
- end
25
-
26
- def id
27
- term["id"]
28
- end
29
-
30
18
  end
31
19
  end
32
20
  end
@@ -5,7 +5,8 @@ module WpApiClient
5
5
  Post,
6
6
  Meta,
7
7
  Term,
8
- Taxonomy
8
+ Taxonomy,
9
+ Error
9
10
  ]
10
11
  end
11
12
  end
@@ -20,7 +20,12 @@ module WpApiClient
20
20
  "https://api.w.org/items" => :terms,
21
21
  "http://api.w.org/v2/post_type" => :post_type,
22
22
  "https://api.w.org/meta" => :meta,
23
- "https://api.w.org/featuredmedia" => :post
23
+ "https://api.w.org/featuredmedia" => :post,
24
+ "wp:term" => :term,
25
+ "wp:items" => :terms,
26
+ "wp:post_type" => :post_type,
27
+ "wp:meta" => :meta,
28
+ "wp:featuredmedia" => :post
24
29
  }
25
30
  end
26
31
 
@@ -64,6 +69,9 @@ module WpApiClient
64
69
  attr_reader :relation
65
70
 
66
71
  def initialize(resource, relation)
72
+ if resource["_links"].keys.include? 'curies'
73
+ relation = convert_uri_to_curie(relation)
74
+ end
67
75
  @resource = resource
68
76
  @relation = relation
69
77
  end
@@ -96,22 +104,40 @@ Available mappings are :post, :term, and :meta.}
96
104
  def load_relation(relationship, position = nil)
97
105
  if objects = @resource.dig("_embedded", relationship)
98
106
  location = position ? objects[position] : objects
99
- WpApiClient::Collection.new(location)
107
+ begin
108
+ WpApiClient::Collection.new(location)
109
+ rescue WpApiClient::ErrorResponse
110
+ load_from_links(relationship, position)
111
+ end
112
+ else
113
+ load_from_links(relationship, position)
114
+ end
115
+ end
116
+
117
+ def load_from_links(relationship, position = nil)
118
+ unless position.nil?
119
+ location = @resource["_links"].dig(relationship, position.to_i, "href")
100
120
  else
101
- unless position.nil?
102
- location = @resource["_links"].dig(relationship, position.to_i, "href")
121
+ if @resource["_links"][relationship].is_a? Array
122
+ # If the resources are linked severally, crank through and
123
+ # retrieve them one by one as an array
124
+ return @resource["_links"][relationship].map { |link| WpApiClient.get_client.get(link["href"]) }
103
125
  else
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
126
+ # Otherwise, get the single link to the lot
127
+ location = @resource["_links"][relationship]["href"]
112
128
  end
113
- WpApiClient.get_client.get(location) if location
114
129
  end
130
+ WpApiClient.get_client.get(location) if location
131
+ end
132
+
133
+ def convert_uri_to_curie(uri)
134
+ uri_curie_mappings = {
135
+ "https://api.w.org/term" => "wp:term",
136
+ "https://api.w.org/items" => "wp:items",
137
+ "https://api.w.org/meta" => "wp:meta",
138
+ "https://api.w.org/featuredmedia" => "wp:featuredmedia"
139
+ }
140
+ uri_curie_mappings.dig(uri) || uri
115
141
  end
116
142
  end
117
143
  end
@@ -1,3 +1,3 @@
1
1
  module WpApiClient
2
- VERSION = "0.2.3"
2
+ VERSION = "0.3.0"
3
3
  end
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.3
4
+ version: 0.3.0
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-10 00:00:00.000000000 Z
11
+ date: 2016-04-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -160,6 +160,7 @@ files:
160
160
  - lib/wp_api_client/configuration.rb
161
161
  - lib/wp_api_client/connection.rb
162
162
  - lib/wp_api_client/entities/base.rb
163
+ - lib/wp_api_client/entities/error.rb
163
164
  - lib/wp_api_client/entities/image.rb
164
165
  - lib/wp_api_client/entities/meta.rb
165
166
  - lib/wp_api_client/entities/post.rb