Module: RailsOnboarding::ApiMode

Extended by:
ActiveSupport::Concern
Included in:
ApiController
Defined in:
lib/rails_onboarding/api_mode.rb

Overview

API Mode support for headless/API-only applications Provides JSON API endpoints for mobile apps and SPAs

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#api_complete_onboardingObject



124
125
126
127
128
129
130
131
132
133
# File 'lib/rails_onboarding/api_mode.rb', line 124

def api_complete_onboarding
  if current_user.complete_onboarding!
    render_api_success({
      message: "Onboarding completed successfully",
      completed_at: current_user.onboarding_completed_at
    })
  else
    render_api_error("Failed to complete onboarding", errors: current_user.errors.full_messages)
  end
end

#api_complete_stepObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/rails_onboarding/api_mode.rb', line 95

def api_complete_step
  step_name = params[:step_name] || params[:step]

  if current_user.complete_step(step_name)
    render_api_success({
      message: "Step '#{step_name}' completed successfully",
      current_step: current_user.onboarding_current_step,
      progress_percentage: current_user.onboarding_progress_percentage,
      onboarding_completed: current_user.onboarding_completed?
    })
  else
    render_api_error("Failed to complete step '#{step_name}'", errors: current_user.errors.full_messages)
  end
end

#api_dismiss_tooltipObject



164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/rails_onboarding/api_mode.rb', line 164

def api_dismiss_tooltip
  tooltip_id = params[:tooltip_id]

  if current_user.dismiss_tooltip(tooltip_id)
    render_api_success({
      message: "Tooltip dismissed successfully",
      tooltip_id: tooltip_id
    })
  else
    render_api_error("Failed to dismiss tooltip")
  end
end

#api_onboarding_statusObject

Onboarding-specific API methods



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/rails_onboarding/api_mode.rb', line 83

def api_onboarding_status
  render_api_success({
    onboarding_completed: current_user.onboarding_completed?,
    onboarding_skipped: current_user.onboarding_skipped?,
    current_step: current_user.onboarding_current_step,
    progress_percentage: current_user.onboarding_progress_percentage,
    steps: api_format_steps,
    next_step: api_next_step,
    previous_step: api_previous_step
  })
end

#api_request?Boolean

Detect API requests

Returns:

  • (Boolean)


55
56
57
58
59
60
# File 'lib/rails_onboarding/api_mode.rb', line 55

def api_request?
  request.format.json? ||
  request.path.start_with?("/api/") ||
  request.headers["Accept"]&.include?("application/json") ||
  request.headers["Content-Type"]&.include?("application/json")
end

#api_restart_onboardingObject



135
136
137
138
139
140
141
142
143
144
145
# File 'lib/rails_onboarding/api_mode.rb', line 135

def api_restart_onboarding
  if current_user.restart_onboarding!
    render_api_success({
      message: "Onboarding restarted successfully",
      current_step: current_user.onboarding_current_step,
      progress_percentage: 0
    })
  else
    render_api_error("Failed to restart onboarding", errors: current_user.errors.full_messages)
  end
end

#api_skip_stepObject



110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/rails_onboarding/api_mode.rb', line 110

def api_skip_step
  step_name = params[:step_name] || params[:step]

  if current_user.skip_step(step_name)
    render_api_success({
      message: "Step '#{step_name}' skipped successfully",
      current_step: current_user.onboarding_current_step,
      progress_percentage: current_user.onboarding_progress_percentage
    })
  else
    render_api_error("Failed to skip step '#{step_name}'", errors: current_user.errors.full_messages)
  end
end

#api_tooltips_listObject



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/rails_onboarding/api_mode.rb', line 147

def api_tooltips_list
  tooltips = current_user.available_tooltips

  render_api_success({
    tooltips: tooltips.map do |tooltip|
      {
        id: tooltip[:id],
        title: tooltip[:title],
        content: tooltip[:content],
        target: tooltip[:target],
        position: tooltip[:position],
        shown: current_user.tooltip_shown?(tooltip[:id])
      }
    end
  })
end

#authenticate_api_request!Object

API authentication



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/rails_onboarding/api_mode.rb', line 178

def authenticate_api_request!
  return if current_user.present?

  token = extract_api_token

  if token.blank?
    render_api_error("Missing authentication token", status: :unauthorized)
    return
  end

  user = authenticate_with_token(token)

  if user
    # Set current_user for the request
    instance_variable_set(:@current_user, user)
  else
    render_api_error("Invalid authentication token", status: :unauthorized)
  end
end

#current_userObject

Current user accessor for API requests



199
200
201
202
203
204
# File 'lib/rails_onboarding/api_mode.rb', line 199

def current_user
  @current_user ||= begin
    token = extract_api_token
    authenticate_with_token(token) if token.present?
  end
end

#render_api_error(message, status: :unprocessable_entity, errors: {}) ⇒ Object



71
72
73
74
75
76
77
78
79
80
# File 'lib/rails_onboarding/api_mode.rb', line 71

def render_api_error(message, status: :unprocessable_entity, errors: {})
  render json: {
    success: false,
    error: {
      message: message,
      details: errors
    },
    meta: build_api_meta
  }, status: status
end

#render_api_success(data, status: :ok, meta: {}) ⇒ Object

API response helpers



63
64
65
66
67
68
69
# File 'lib/rails_onboarding/api_mode.rb', line 63

def render_api_success(data, status: :ok, meta: {})
  render json: {
    success: true,
    data: serialize_for_api(data),
    meta: build_api_meta(meta)
  }, status: status
end