How to get Swagger UI to use my titles? - swagger

I am using Swagger-php and Swagger-UI and it all works just fine but for one annoyance. The UI that Swagger-UI creates has the expected click-to-expand sections for my API routes, but the title of each one appears to be the JSON file generated and not any name I can give it. After the title, the description is the one I give in my annotation, but the title I seem to have no control over.
So if I have routes that begin with a resourcePath of /foo, and a description that says "Foo API Functions," the UI looks like:
foo.json : Foo API Functions
I don't want "foo.json" I'd much rather specify what this says. Like just "Foo" or even "Foo Functions" and then change my description to something more meaningful like, "This is where you find the foo functions."
Am I missing which annotation to use for this?

If you manually edit the api-docs.json file. You can replace the .json with .{format} and all will display correctly and function correctly. Not sure why the .{format} is not inserted by default. Slightly annoying.

I too ran into this problem, but I couldn't find anything on either theswagger-php or swagger-ui github pages mentioning this. So, I wrote a short build script (assuming a Unix-like OS) as a work around, which will first build all the docs using swagger.phar and then run sed to do an inline string replace on the problematic string. Here is what I did below:
#!/bin/bash
# write API documentation from "src" directory to the "docs" directory
/usr/bin/php swagger.phar src -o docs
# replace instances of "json" with "{format}" to fix swagger-php formatting issue
sed -i -e 's/json/{format}/g' docs/api-docs.json

Fixed in swagger-php 0.9.1
I don't know why swagger-ui strips out ".{format}" but not ".json"
The .{format} was not inserted by default because it might be confusing. It suggests the presence of different formats and swagger-php only supports the json format.

Related

Jekyll auto-generated titles from file names

I would like to write files like:
ideas.md
social-pomodoro.md
ynm.md
And not have to write titles, eg:
---
title: Ideas
---
But titles should be generated from filenames using something similar to Rails #humanize method.
As this isn't a built-in functionality of Jekyll (at time of writing), I suppose you have 2 options:
Write a your post, and then use the {{ post.path }} tag and manipulate the path to get the filename and save that as the post title.
This would however require you to write frontmatter and other post details. Here's a related SO question.
(recommended) Use a script to generate the filename based on the title.
This is similar but a reverse of what you wanted, in which a script would take in parameters such as post title and post tags, and generate a file with the correct Jekyll post filename (name-of-post-2015-01-30) (I think Jekyll posts need dates to avoid collisions). The script will also generate the appropriate YAML front matter so you can get writing right away and not deal with repetitive overhead. All you would have to do it something like ./post.sh "Title" "optional_tags and you're good to go!
Here is a Python script written by Josh Branchadu that creates the file with details like dates and filenames all taken care of. There's also a similar bash version as well. Lastly a a script that uses the Thor Ruby in automating Jekyll posts that should let you create a post in the most painless way possible.

Slash at the end of url

I think (correct me if I am wrong) that it is better to put a / at the end of most of url. Like this: http://www.myweb/file/
And not put / at the end of filenames: http://www.myweb/name.html
I have to correct that in a website with a lot of links. Is there a way I can do that in a fast way. For instance in some programs like Dreamweaver I can use find and replace.
The second case is quite easy with Dreamweaver:
- Find: .html/"
- Replace: .html"
But how can I say something like:
- Find: all the links that end with a directory. Like http://www.myweb/file
- Replace: the same link but with a / at the end. Like http://www.myweb/file/
Your approach may work but it is based on the assumption that all files have a file extension.
There is a distinct difference between the urls http://www.myweb/file and http://www.myweb/file/ because the latter could resolve to http://www.myweb/file/index.php, or any other in the default set configured in your web server. That URL could also reference a perfectly valid file which doesn't contain a file extension, such as if it were a REST endpoint.
So you are correct insofar as you should explicitly add a "/" if you are referring to a directory, for example if you are expecting the web server to look up the correct index page to respond, or doing a directory listing.
To replace the incorrect URLS, regular expressions are your friend.
To find all files which have an erroneous "/" you could use /\.(html|php|jpg|png)\//, adding as many different file extensions into that pipe-separated list as you like. You can then replace that with .$1 or .\1 depending on your tool.
An example of doing this with Perl would be:
perl -pi -e 's/\.(html|php|jpg|png)\//.\1/g' theFileYouWantToCheck.html
Of (if you're using a Linux-based system) you can automate that nicely with find:
find path/to/html/root -type f -name "*.html* | xargs perl -pi -e 's/\.(html|php|jpg|png)\//.\1/g'
which will find all html files in the directory and do an inline find and replace. Assuming you're using version control, it's then easy to see the changes it's applied :)
Update
Solving the problem for adding a slash to directories isn't trivial. The approach I'd take:
Write a script to recurse through your website structure locally, making a list of all files
Parse the HTML files to extract all href=".*" and replace them with href=".*/" only if the end of the URL isn't present in the list extracted by the first script.
Any text-based find and replace is not going to be aware of whether the link is actually to a file or not.

I can't find where a string is getting defined -- any tricks to find its source?

I'm using:
Rails 3.2x
Spree 1.2
Ruby 1.9.3x
I'm trying to edit the title of one of my pages, and I cannot find where it is getting defined. It is showing up in my base ERB file as 'title', but that name is sufficiently generic to make it next to impossible to find where it is defined.
I have prodded everywhere I can think, I've tried searching for "title =", but nothing is working. I tried calling source_location on it, but that appears to only work on methods.
Any tricks for finding where a variable is defined?
I can't think of an elegant way. A dumb-but-probably-effective way would be to dump stack trace in your erb, then see what those locations are doing and if title is defined there. It has to enter somewhere between the start of program and invoking your erb.
When I can't find something, I use grep -ri some_string . at the command-line to recursively search all the content of the directory.
It's also a good tactic to let your editor search all the source code, since the ones worth using have the ability to search through all files in a directory.
it is created from a mixture of product names, a site config, and something else
An alternate trick is to add a HTML-comment section in your ERB file, and put the pertinent information for the components used to create the title into that section. Then, let the pages be generated and look inside the page's content to determine what table and row ID it is, the site_config filename, etc.
You really should be able to figure it out based on the parts that are concatenated to build the title and then search your database or files. That information isn't magically created out of thin air by Rails; Someone had to tell Rails how to define the title. But, people move on, or they don't document correctly, so try the embedded information trick.

Maintaining GREP variables through find and replace

I've recently taken on a project of document conversion to HTML. That is, a client gives me a .DOC file, and I need to convert the contents to one long HTML file - no styling, no CSS, just clean HTML with paragraph tags, header tags tags, etc.
I found an application that does a pretty good job of automating the first part of it. The problem is that I need to do some advanced find and replace based on strings using variables.
For instance, I have footnotes that were converted properly. They're currently displayed as superscript numbers with the
I'd like to change how the footnote is displayed. Instead of a superscript number 6 for the 6th footnote, I'd like it to show (Note 6)
To do that on the entire document (hundreds of footnotes), I'm wondering if I can do something like:
FIND:
<sup><a name="FN[0-9]" href="FNR[0-9]">[0-9]</a></sup>
REPLACE:
<a name="FN%1" href="FNR%2">(Note %3)</a>
The problem is, I can't find a Find and Replace tool that lets me maintain the variables in the replace area. All I get is the superscript 6 appearing as (Note %3), as well as every other footnote doing the same thing.
Anyone have any ideas on how I can accomplish my task efficiently?
In Perl it would look roughly like this on the command line (I have NOT tested this):
perl -i -p -e's{<sup><a name="(FN\d)" href="(FNR\d)">(\d)</a></sup>}{<a name="$1" href="$2">(Note $3)</a>}' filenames....
-i says "Edit this file in place", -p means "print each line after we do whatever is in the -e switch".
That's assuming you're only looking for a single digit where you have [0-9]. If you want to match FN427, then you change (FN\d) to (FN\d+), for example.
This also assumes that the HTML that are you parsing looks EXACTLY LIKE THAT. If you get some HTML that is <a href=... name=... (with the attributes in opposite order than you have) then it will break. In that case, you'll want to use an HTML parser.
I hope that gives you enough to start with.

Vim html.erb snippets?? snipMate Need a vim tip

When I'm in an html.erb file, I get no snipMate snippets.
I would like both HTML and Ruby, or just HTML would be fine,
How would I do this?
Would I need to write a set of snippets?
If so, is there a way of pulling in existing snippets without copying them?
Is there a way of telling vim to go into html mode when it sees .html erb?
You can use an autocmd to set the filetype to html when opening a ".html.erb" file. This could have unwanted side effects for plugins that work for ".erb" files.
autocmd BufNewFile,BufRead *.html.erb set filetype=html
You can also load more than one set of snippets by using a dotted filetype:
autocmd BufNewFile,BufRead *.html.erb set filetype=html.eruby
See :help snippet-syntax in the snipMate help for more info.
Snippets are stored in directory called snippets somewhere in your ~/.vim folder.
If you look there, there is usually one file per filetype, there is a c.snippets, a ruby.snippets, so it seems what you have to do is to create an erb.snippets there with what you want.
Eventually you could copy the content of ruby.snippets and html.snippets into your new erb.snippets.
Alternatively you can search on github, some people have posted their own erb.snippets configuration. For example, there is a nice collection there :
https://github.com/scrooloose/snipmate-snippets
The best thing would to try first to open a snippet file and look at the syntax, it is pretty easy to create your own snippet depending on what you use the most.
I am currently on a promoting tour for UltiSnips on StackOverflow. UltiSnips supports extending other file types, your erb.snippets would look like this:
extends html, ruby, rails
snippet temp "A snippet only in Erb"
erb rules ${1}
endsnippet
A conversion script for snipMate snippets is shipped with UltiSnips, so switching is easy.
I used the autocommand method to the set the filetype, but then I got html syntax errors for things like this:
<%= image_tag("logo.png", :alt => "Sample App", :class => "round") %>
The last two angle brackets would be highlighted in red, which drove me bonkers. So, I created a symlink called eruby.snippets that points to html.snippets. That worked like a champ and now I don't have to make changes in two places. I also have an eruby-rails snippet directory for non-html eruby snippets.
This is on a Mac OS X system. Note that an alias won't work. You need to hit the terminal and use the ln command. Not sure about doing this on a Windoze system.
You can assign multiple snippets scopes to a single filetype. (I've found that altering the filetype tends to break some syntax highlighting).
You can check that the filetype for erb files is indeed 'eruby' with:
:set filetype?
If you're using the maintained fork of snipmate, it looks like you'll want both the eruby.snippets and eruby-rails.snippets from the snipmate-snippets repository (owned by honza, but I don't have enough reputation to link to it here) (see the INSTALL section of the snipmate README for proper setup).
If you are using the maintained fork, I believe setting g:snipMate.scope_aliases in your .vimrc with the following will work for your example:
let g:snipMate = {}
let g:snipMate.scope_aliases = {}
let g:snipMate.scope_aliases['eruby'] = 'eruby,eruby-rails'
I've added a pull request to snipmate to have their documentation updated.
Jumping on the UltiSnips bandwagon after trying SnipMate for a while. Like SirVer mentioned, having the html, ruby, etc snippets available within an *.erb file was as simple as adding the extend line to the eruby.snippets file.
With the original snipMate plugin, create a file ~/.vim/ftplugin/erb_snippets.vim and put the following into it:
silent call ExtractSnipsFile(g:snippets_dir . 'html.snippets', &l:filetype)
silent call ExtractSnipsFile(g:snippets_dir . 'ruby.snippets', &l:filetype)

Resources