Class: Thoth::PageApiController

Inherits:
Controller
  • Object
show all
Defined in:
lib/thoth/controller/api/page.rb

Instance Method Summary collapse

Methods inherited from Controller

action_missing

Instance Method Details

#check_nameObject

Returns a response indicating whether the specified page name is valid and not already taken. Returns an HTTP 200 response on success.

Query Parameters

name

page name to check

Sample Response

{"valid":true,"unique":true}


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/thoth/controller/api/page.rb', line 45

def check_name
  error_403 unless auth_key_valid?

  unless request[:name] && request[:name].length > 0
    error_400('Missing required parameter: name')
  end

  response['Content-Type'] = 'application/json'

  name = request[:name].to_s

  JSON.generate({
    :valid  => Page.name_valid?(name),
    :unique => Page.name_unique?(name)
  })
end

#set_positionObject

Sets the display position of the specified page. If the new position is already in use by another page, that page’s position (and any others) will be adjusted as necessary. Returns an HTTP 200 response on success. This action only accepts POST requests.

POST Parameters

id

page id

position

new display position

Sample Response

Indicates that the display position for page id 42 was successfully set to 3.

{"id":42,"position":3}


102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/thoth/controller/api/page.rb', line 102

def set_position
  error_403 unless auth_key_valid?
  error_405 unless request.post?

  [:id, :position].each do |param|
    unless request[param] && request[param].length > 0
      error_400("Missing required parameter: #{param}")
    end
  end

  id       = request[:id].to_i
  position = request[:position].to_i

  unless page = Page[id]
    error_400("Invalid page id: #{id}")
  end

  begin
    Page.normalize_positions
    Page.set_position(page, position)

  rescue => e
    error_400("Error setting page position: #{e}")
  end

  JSON.generate({
    :id       => id,
    :position => position
  })
end

#suggest_nameObject

Suggests a valid and unique name for the specified page title. Returns an HTTP 200 response on success.

Query Parameters

title

page title

Sample Response

{"name":"ninjas-are-awesome"}


73
74
75
76
77
78
79
80
81
82
83
# File 'lib/thoth/controller/api/page.rb', line 73

def suggest_name
  error_403 unless auth_key_valid?

  unless request[:title] && request[:title].length > 0
    error_400('Missing required parameter: title')
  end

  response['Content-Type'] = 'application/json'

  JSON.generate({"name" => Page.suggest_name(request[:title])})
end