Skip to content

Commit

Permalink
Merge pull request #44 from wponrails/feature/wp_connector_options
Browse files Browse the repository at this point in the history
Use a configuration class scoped to wp-connector
  • Loading branch information
jeroenrietveld committed Aug 19, 2015
2 parents 1f42eb1 + 1203e76 commit e6ce48b
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 10 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ wp-connector
[![Code Climate](https://codeclimate.com/github/hoppinger/wp-connector/badges/gpa.svg)](https://codeclimate.com/github/hoppinger/wp-connector)
[![Test Coverage](https://codeclimate.com/github/hoppinger/wp-connector/badges/coverage.svg)](https://codeclimate.com/github/hoppinger/wp-connector)

This library is part of the [**WP on Rails**](https://github.com/wponrails) project, that limits WP to a means of managing content while using a Rails application to serve public request and provide a basis for customizations.
This library is part of the [**WP on Rails**](https://github.com/wponrails) project, that limits WP to a means of managing content while using a Rails application to serve public request and provide a basis for customizations.

To prepare a WP installation to be used in a **WP on Rails** architecture the [**wp-relinquish**](https://github.com/wponrails/wp-relinquish) plugin is provided. It provides a means to configure WP actions to send notifications as webhook calls to a Rails application equipped with **wp-connector**.

Expand Down Expand Up @@ -48,18 +48,18 @@ In WordPress install both the `wp-relinquish` and `json-rest-api` plugin. The wo

Add the "JSON route" of the WP REST API to your Rails configuration by adding the `wordpress_url` config option to your environment files in `config/environments/` (e.g. `config/environments/development.rb`):
```ruby
Rails.configuration.x.wordpress_url = "http://wordpress-site.dev/"
WpConnector.configuration.wordpress_url = "http://wordpress-site.dev/"
```
Here `wordpress-site.dev` is the domain for your Wordpress site.

This configures some models, `['articles', 'news_articles', 'pages']`, for paginated requests to the WP-REST API.
```ruby
Rails.configuration.x.wp_api_paginated_models = %w(articles news_articles pages)
WpConnector.configuration.wp_api_paginated_models = %w(articles news_articles pages)
```

Also specify the wp-connector API key in the Rails configuration by adding the `wp_connector_api_key` config option to the same environment files in `config/environments/` (e.g. `config/environments/development.rb`):
```ruby
Rails.configuration.x.wp_connector_api_key="H3O5P6P1I5N8G8E4R"
WpConnector.configuration.wp_connector_api_key="H3O5P6P1I5N8G8E4R"
```


Expand Down
2 changes: 1 addition & 1 deletion app/controllers/concerns/wp_preview_tools.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def validate_preview_token(wp_post_model, &block)
# Creates a token to verify previews requests
#
def token(wp_post_model)
hash_inputs = Rails.configuration.x.wp_connector_secret + wp_post_model.slug
hash_inputs = WpConnector.configuration.wp_connector_secret + wp_post_model.slug
Digest::SHA2.new(256).hexdigest hash_inputs
end

Expand Down
4 changes: 2 additions & 2 deletions app/controllers/concerns/wp_webhook_endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def self.included(base)
def wp_id_from_params
# The `parent_ID` has precedence over `ID` as the former contains
# the actual ID used by WP in case multiple versions exist.
params[:parent_ID] || params[:ID]
params[:parent_ID] || params[:ID] || params[:id]
end

#
Expand All @@ -40,7 +40,7 @@ def render_json_200_or_404(success)
end

def require_valid_api_key
head :unauthorized unless params[:api_key] == Rails.configuration.x.wp_connector_api_key
head :unauthorized unless params[:api_key] == WpConnector.configuration.wp_connector_api_key
end

#
Expand Down
6 changes: 3 additions & 3 deletions app/models/concerns/wp_cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def purge(wp_id)
def get_from_wp_api(route, page = -1)
# TODO (dunyakirkali) pass filter through args to get_from_wp_api
posts_per_page = (ENV['PER_PAGE'].to_i == -1 ? -1 : ENV['PER_PAGE'].to_i)
base = Rails.configuration.x.wordpress_url
base = WpConnector.configuration.wordpress_url
unless paginated_models.include?(wp_type)
url = "#{base}?json_route=/#{route}&filter[posts_per_page]=-1"
else
Expand All @@ -125,9 +125,9 @@ def get_from_wp_api(route, page = -1)
# List of paginated models
#
def paginated_models
models = Rails.configuration.x.wp_api_paginated_models
models = WpConnector.configuration.wp_api_paginated_models
if models.empty?
Rails.logger.warn "Please specifiy Rails.configuration.x.wp_api_paginated_models, as the default is DEPRICATED"
Rails.logger.warn "Please specifiy WpConnector.configuration.wp_api_paginated_models, as the default is DEPRICATED"
models = %w( articles news_articles pages media)
end
models
Expand Down
13 changes: 13 additions & 0 deletions lib/wp-connector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,17 @@

module WpConnector
class Engine < ::Rails::Engine; end

class << self
attr_accessor :configuration
end

def self.configure
self.configuration ||= Configuration.new
yield(configuration)
end

class Configuration
attr_accessor :wordpress_url, :wp_connector_api_key, :wp_api_paginated_models
end
end

0 comments on commit e6ce48b

Please sign in to comment.