Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,14 @@ taxonomy_name = term.taxonomy.name
posts = term.posts
```

#### OAuth
#### OAuth & OAuth2

Provide a symbol-keyed hash of `token`, `token_secret`, `consumer_key` and `consumer_secret` on configuration.

```ruby
WpApiClient.configure do |api_client|
api_client.oauth_credentials = oauth_credentials_hash
api_client.oauth_credentials = oauth_credentials_hash #OAuth
api_client.oauth2_token = oauth_token #OAuth2
end

client = WpApiClient.get_client
Expand Down
12 changes: 11 additions & 1 deletion lib/wp_api_client/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ def get(url, params = {})
end
end

def post(url, params = {})
if @concurrent_client
@concurrent_client.post(api_path_from(url), params)
else
response = @connection.post(api_path_from(url), params)
@headers = response.headers
native_representation_of response.body
end
end

def concurrently
@concurrent_client ||= ConcurrentClient.new(@connection)
yield @concurrent_client
Expand All @@ -23,7 +33,7 @@ def concurrently
result
end

private
private

def api_path_from(url)
url.split('wp/v2/').last
Expand Down
5 changes: 5 additions & 0 deletions lib/wp_api_client/concurrent_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ def get(url, params = {})
@queue << [api_path_from(url), params]
end

def post(url, params = {})
@queue ||= []
@queue << [api_path_from(url), params]
end

def run
responses = @connection.get_concurrently(@queue)
responses.map { |r| native_representation_of(r.body) }
Expand Down
1 change: 1 addition & 0 deletions lib/wp_api_client/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class Configuration
attr_accessor :endpoint
attr_accessor :embed
attr_accessor :oauth_credentials
attr_accessor :oauth2_token
attr_accessor :debug
attr_accessor :cache
attr_accessor :basic_auth
Expand Down
8 changes: 7 additions & 1 deletion lib/wp_api_client/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ def initialize(configuration)
@queue = []
@conn = Faraday.new(url: configuration.endpoint) do |faraday|

if configuration.oauth_credentials
if configuration.oauth2_token
faraday.request :oauth2, configuration.oauth2_token
elsif configuration.oauth_credentials
faraday.use FaradayMiddleware::OAuth, configuration.oauth_credentials
end

Expand Down Expand Up @@ -47,6 +49,10 @@ def get(url, params = {})
@conn.get url, parse_params(params)
end

def post(url, params = {})
@conn.post url, params
end

# requests come in as url/params pairs
def get_concurrently(requests)
responses = []
Expand Down
70 changes: 38 additions & 32 deletions spec/post_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,45 +37,51 @@

describe "meta function" do

before :all do
WpApiClient.reset!
oauth_credentials = get_test_oauth_credentials

WpApiClient.configure do |api_client|
api_client.oauth_credentials = oauth_credentials
[:oauth, :oauth2].each do |auth_type|
before :all do
WpApiClient.reset!
oauth_credentials = get_test_oauth_credentials

WpApiClient.configure do |api_client|
if auth_type == :oauth
api_client.oauth_credentials = oauth_credentials
elsif auth_type == :oauth2
api_client.oauth2_token = oauth_credentials
end
end
@api = WpApiClient.get_client
end
@api = WpApiClient.get_client
end

after :all do
WpApiClient.reset!
end

it "returns an individual meta value" do
VCR.use_cassette('single_post_auth', record: :new_episodes) do
@post = WpApiClient.get_client.get("posts/1")
expect(@post.meta(:example_metadata_field)).to eq "example_meta_value"
after :all do
WpApiClient.reset!
end
end

it "caches" do
VCR.use_cassette('single_post_auth', record: :new_episodes) do
@post = WpApiClient.get_client.get("posts/1")
meta_value = @post.meta(:example_metadata_field)
it "returns an individual meta value" do
VCR.use_cassette('single_post_auth', record: :new_episodes) do
@post = WpApiClient.get_client.get("posts/1")
expect(@post.meta(:example_metadata_field)).to eq "example_meta_value"
end
end
VCR.turned_off do
::WebMock.disable_net_connect!
expect {
@post.meta(:example_associated_post_id)
}.to_not raise_error

it "caches" do
VCR.use_cassette('single_post_auth', record: :new_episodes) do
@post = WpApiClient.get_client.get("posts/1")
meta_value = @post.meta(:example_metadata_field)
end
VCR.turned_off do
::WebMock.disable_net_connect!
expect {
@post.meta(:example_associated_post_id)
}.to_not raise_error
end
end
end

it "returns the right items from cache" do
VCR.use_cassette('single_post_auth', record: :new_episodes) do
@post = WpApiClient.get_client.get("posts/1")
expect(@post.meta(:example_metadata_field)).to eq "example_meta_value"
expect(@post.meta(:example_associated_post_id)).to eq "100"
it "returns the right items from cache" do
VCR.use_cassette('single_post_auth', record: :new_episodes) do
@post = WpApiClient.get_client.get("posts/1")
expect(@post.meta(:example_metadata_field)).to eq "example_meta_value"
expect(@post.meta(:example_associated_post_id)).to eq "100"
end
end
end
end
Expand Down
37 changes: 20 additions & 17 deletions spec/relationships_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,27 @@
end
end

describe "relationships with metadata", vcr: {cassette_name: 'single_post_auth', record: :new_episodes} do
before :each do
# we need oAuth for this
WpApiClient.reset!
oauth_credentials = get_test_oauth_credentials
WpApiClient.configure do |api_client|
api_client.oauth_credentials = oauth_credentials
end
@api = WpApiClient.get_client
post = @api.get("posts/1")
@relationship = WpApiClient::Relationship.new(post.resource, "https://api.w.org/meta")
end
describe "relationships with metadata" do
[:oauth, :oauth2].each do |auth_type|
subject(:api) {
WpApiClient.reset!
oauth_credentials = get_test_oauth_credentials
WpApiClient.configure do |api_client|
if auth_type == :oauth
api_client.oauth_credentials = oauth_credentials
elsif auth_type == :oauth2
api_client.oauth2_token = oauth_credentials
end
end
WpApiClient.get_client
}

it "returns an hash of meta" do
relations = @relationship.get_relations
expect(relations).to be_a Hash
expect(relations["example_metadata_field"]).to be_a String
it "returns an hash of meta", vcr: {cassette_name: 'single_post_auth', record: :new_episodes} do
post = api.get("posts/1")
relations = WpApiClient::Relationship.new(post.resource, "https://api.w.org/meta").get_relations
expect(relations).to be_a Hash
expect(relations["example_metadata_field"]).to be_a String
end
end
end

end
10 changes: 10 additions & 0 deletions spec/wp_api_configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@
WpApiClient.get_client.get('posts/1')
end

it "can set up OAuth2 token", vcr: {cassette_name: :oauth_test} do
oauth_credentials = get_test_oauth_credentials

WpApiClient.configure do |api_client|
api_client.oauth2_token = oauth_credentials
end

WpApiClient.get_client.get('posts/1')
end

it "can set up link relationships", vcr: {cassette_name: :single_post} do
WpApiClient.configure do |api_client|
api_client.define_mapping("http://my.own/mapping", :post)
Expand Down