-
-
Save pinglamb/1338795 to your computer and use it in GitHub Desktop.
{ | |
"host": "api.twitter.com", | |
"api_groups": [ | |
{ | |
"name": "Timelines", | |
"apis": [ | |
{ | |
"name": "GET statuses/public_timeline", | |
"type": "json", | |
"path": "/1/statuses/public_timeline.json", | |
"method": "GET", | |
"params": { | |
"trim_user": "*boolean", | |
"include_entities": "*boolean" | |
} | |
}, | |
{ | |
"name": "GET statuses/user_timeline", | |
"type": "json", | |
"path": "/1/statuses/user_timeline.json", | |
"method": "GET", | |
"params": { | |
"screen_name": "*string", | |
"count": "*number", | |
"page": "*number" | |
} | |
} | |
] | |
}, | |
{ | |
"name": "Tweets", | |
"apis": [ | |
{ | |
"name": "GET statuses/show/:id", | |
"type": "json", | |
"path": "/1/statuses/show/:id.json", | |
"method": "GET", | |
"url_params": { | |
"id": { | |
"value": "*number", | |
"required": true | |
} | |
}, | |
"params": { | |
"trim_user": "*boolean", | |
"include_entities": "*boolean" | |
} | |
} | |
] | |
} | |
] | |
} |
@mystcolor I study how Rails did that and eventually I GOT IT. It uses a StringScanner to scan through the whole path definition (e.g. /statuses/show/:id) and label each part of the path as different type, e.g. /:\w+/
belongs to SYMBOL
type. The logic is included in following snippet:
# https://github.com/rails/journey/blob/master/lib/journey/scanner.rb#L33
def scan
case
# /
when text = @ss.scan(/\//)
[:SLASH, text]
when text = @ss.scan(/\*/)
[:STAR, text]
when text = @ss.scan(/\(/)
[:LPAREN, text]
when text = @ss.scan(/\)/)
[:RPAREN, text]
when text = @ss.scan(/\|/)
[:OR, text]
when text = @ss.scan(/\./)
[:DOT, text]
when text = @ss.scan(/:\w+/) # It is RegExp matches pattern :var
[:SYMBOL, text]
when text = @ss.scan(/[\w-]+/)
[:LITERAL, text]
# any char
when text = @ss.scan(/./)
[:LITERAL, text]
end
end
For each type, it has its own method to construct the corresponding part of the final path:
# https://github.com/rails/journey/blob/master/lib/journey/visitors.rb#L107
# Someone called visit_SYMBOL('id')
def visit_SYMBOL node
key = node.to_sym
# Check if options contain :id
if options.key? key
value = options[key]
consumed[key] = value
Router::Utils.escape_path(value) # If found, return the escaped value
else
"\0" # Otherwise leave it blank
end
end
Therefore, I think you need to use RegExp /:\w+/
to match the path, check if it is contained in url_params
and replace it with either the specified value or user input or leave it blank (\0
)
P.S. Rails scanning logic is so complicated because Rails can construct url for path definition like :controller/:action(/:id)
, which accepts params {:controller => 'xxxxx', :action => 'xxxxx', :id => 'xxxxx'}
, with param :id
optional (because it is surrounded by brackets).
@mystcolor How do you handle JSON pretty print? Maybe you can use a web view to render the JSON and use @yachi's extracted JSON pretty print Javascript. Or iOS has library to do so.
@pinglamb iOS default doesn't contains pretty printing JSON object, however the parsed object is pretty printed by iOS. At initial state I think I will try to stick with the parsed object first but of course this only works if the response is really parsible. I don't have a solution for pretty printing the raw JSON object yet, is there anywhere I can find @yachi 's solution?
I posted the JS and CSS here: https://gist.github.com/1341849
By calling Javascript JSON_PRETTY(json)
, the json string will be converted to html for pretty printing.
great, will see how it integrates in the iOS project. I've made some screenshot on the sample app, have a look if free.
https://github.com/projaito/DamnAPI-iOS
Great. I added a screenshot for the Android version as well. https://github.com/projaito/DamnAPI-Android
I think ios json parser also throws error when parsing "\d", "\b". did you do any preprocessing before parsing?
it doesn't. I have a pretty printing extension on chrome and looks like it doesnt too.
@mystcolor JT, you are right, in Android (and also the Chrome Extension) maps:
\d
tod
\b
to[blank]
So I escaped the backslash (\d
becomes \\d
). Any comment on this approach?
JT, seems it is not a good approach as \d
, \b
, etc have special meaning in JSON. In Android, it becomes to same situation after several conversations between String and JSON. Any other approach?
@pinglamb How about using [0-9]+
instead of \d
? But I don't have a solution for \b
yet, maybe there's some regex can also express that?
Another way maybe there's a need to specify some magic keyword for that, such as number
, string
, uuid
, appkey
.
If you really want a regexp, true|false
or 0|1
can be.
I think the logic should be matching the value with magic keywords and the problem is what keywords we pick. How about we use JT's suggestion number
, string
, uuid
, appkey
and see what the final list is and later on replace them with some special symbols (maybe some prefix, i.e. ?number
).
Just found that the problem of \\d
becomes d
eventually is because I haven't update the raw JSON path.
@yachi @pinglam
any tips on parsing :var?
I noticed that restkit use (var) instead