Skip to content

Commit

Permalink
Merge pull request #101 from trevorrjohn/http_basic_auth
Browse files Browse the repository at this point in the history
Allow for HTTP Basic Auth for Remote docs
  • Loading branch information
jejacks0n authored Apr 13, 2019
2 parents e3a7bbc + 536a065 commit 4c1e8bf
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ following parameters that can be used to customize apitome. Note: Restart rails

<dt> root </dt><dd>
This defaults to Rails.root if left nil. If you're providing documentation for an engine using a dummy application
it can be useful to set this to your engines root.. E.g. `Application::Engine.root`
it can be useful to set this to your engines root. (E.g. `Application::Engine.root`)
<br/>
<b>default:</b> <code>nil</code>
</dd>
Expand Down Expand Up @@ -138,6 +138,13 @@ following parameters that can be used to customize apitome. Note: Restart rails
<b>default:</b> <code>nil</code>
</dd>

<dt> http_basic_authentication </dt><dd>
If using remote urls you can fetch the remote docs using HTTP Basic Authentication by configuring this to be an
array of the user and password. (E.g. `['user', 'password']`)
<br/>
<b>default:</b> <code>nil</code>
</dd>

<dt> precompile_assets </dt><dd>
By default all assets that ship with this library are precompiled by the asset pipeline. If you would prefer to
control this yourself, you can disable it by changing this to false.
Expand All @@ -151,6 +158,10 @@ following parameters that can be used to customize apitome. Note: Restart rails
<b>default:</b> <code>true</code>
</dd>

When you install Apitime an initializer file (app/config/initializers/apitome.rb) is generated that contains good
documentation for each configuration directive. Otherwise you can get a refresher by checking the
[Apitome Configuration](https://github.com/modeset/apitome/wiki/Apitome-Configuration) article.

## Customization
You can put custom views and partials in your own `views/apitome/docs` -- check
[here for examples](tree/master/app/views/apitome/docs). You can put any partial in your own path and they will be
Expand Down
12 changes: 10 additions & 2 deletions app/controllers/apitome/docs_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ def simulate
def file_for(file, readme: false)
if Apitome.configuration.remote_url
file = readme ? file : "#{Apitome.configuration.doc_path}/#{file}"
file = "#{Apitome.configuration.remote_url}/#{file}"
file = URI.encode("#{Apitome.configuration.remote_url}/#{file}")
else
file = Apitome.configuration.root.join(Apitome.configuration.doc_path, file)
raise Apitome::FileNotFoundError.new("Unable to find #{file}") unless File.exist?(file)
end

open(file).read
open(file, file_opts).read
end

def resources
Expand All @@ -58,6 +58,14 @@ def set_example(resource)
@example = JSON.parse(file_for("#{resource}.json"))
end

def file_opts
if Apitome.configuration.remote_url && Apitome.configuration.http_basic_authentication
{ http_basic_authentication: Apitome.configuration.http_basic_authentication }
else
{}
end
end

def formatted_readme
return unless Apitome.configuration.readme
rendered_markdown(file_for(Apitome.configuration.readme, readme: true))
Expand Down
8 changes: 5 additions & 3 deletions lib/apitome/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ class Configuration
:js_override,
:readme,
:single_page,
:remote_url,
:url_formatter,
:remote_url,
:http_basic_authentication,
:precompile_assets,
:simulated_response
:simulated_response,
]

@@mount_at = "/api/docs"
Expand All @@ -33,8 +34,9 @@ class Configuration
@@js_override = nil
@@readme = "../api.md"
@@single_page = true
@@remote_url = nil
@@url_formatter = -> (str) { str.gsub(/\.json$/, "").underscore.gsub(/[^0-9a-z]+/i, "-") }
@@remote_url = nil
@@http_basic_authentication = nil
@@precompile_assets = true
@@simulated_response = true

Expand Down
6 changes: 6 additions & 0 deletions lib/generators/apitome/install/templates/initializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@
# URL is used as the base location and should be set to where the readme is located. If left nil, local is assumed.
config.remote_url = nil

# If the remote_docs is set to true, and the remote URL is protected by
# HTTP Basic Authentication you can set the user and password here as an array.
# Usage: `http_basic_authentication = ['user', 'password']`.
# This defaults to nil.
config.http_basic_authentication = nil

# If you would like to precompile your own assets, you can disable auto-compilation.
config.precompile_assets = true

Expand Down

0 comments on commit 4c1e8bf

Please sign in to comment.