Skip to content

Commit

Permalink
boxbilling_api provider: handle paths that have slug field
Browse files Browse the repository at this point in the history
  • Loading branch information
Raul Rodriguez committed Feb 11, 2015
1 parent 7cbe721 commit caf0f39
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion providers/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,14 @@ def whyrun_supported?
PATH_UNIQUE_KEYS = {
'admin/staff' => :email,
'admin/client' => :email,
'admin/email/template' => :action_code
'admin/email/template' => :action_code,
'admin/forum' => :slug,
'admin/forum/topic' => :slug,
'admin/kb/article' => :slug,
'admin/kb/category' => :slug,
'admin/news' => :slug,
'admin/product' => :slug,
'admin/product/addon' => :slug
}

PATH_MISSING_ACTIONS = {
Expand Down Expand Up @@ -71,6 +78,16 @@ def filter_path(path)
path.gsub(%r{(^/*|/*$)}, '').gsub(/\/+/, '/')
end

def slugify(string)
string
.strip
.downcase
.gsub(/[^a-z0-9-]/, '-')
.gsub(/-+/, '-')
.gsub(/^-+/, '')
.gsub(/-+$/, '')
end

# Get the final action string name for a path (from symbol)
# Examples:
# (admin/client, :create) -> create
Expand Down Expand Up @@ -118,10 +135,12 @@ def get_unique_key_field_from_path(path)

# Get "primary keys" from data Hash
def get_primary_keys_from_data(data)
data = data.dup
id_fields = [
get_primary_key_field_from_path(new_resource.path),
get_unique_key_field_from_path(new_resource.path)
].compact
data[:slug] ||= slugify(data[:title]) if id_fields.include?(:slug)
data.select do |key, _value|
id_fields.include?(key)
end
Expand Down

0 comments on commit caf0f39

Please sign in to comment.