Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a Wildcard URL Matcher #55

Closed
wants to merge 2 commits into from
Closed

Adding a Wildcard URL Matcher #55

wants to merge 2 commits into from

Conversation

groomsy
Copy link

@groomsy groomsy commented Aug 11, 2014

I am using a Hypermedia API adhering to the Siren specification. Every response should be the same object type. Therefore, I need an any URL wildcard for modelClassesByResourcePath. I have decided to use the ** identifier.

@sodastsai
Copy link
Member

Hi @groomsy, so in your case, the modelClassesByResourcePath method would be implemented like

+ (NSDictionary *)modelClassesByResourcePath {
    return @{
        @"statuses/*": [Tweet class],
        @"users/*": [TwitterUser class],
        @"friends/ids.json": [UserIdentifierCollection class],
        @"followers/ids.json": [UserIdentifierCollection class],
        @"**": [AnyOtherModel class],
    };
}

, right?

@groomsy
Copy link
Author

groomsy commented Jul 8, 2015

@sodastsai It's been a while since I've looked at this, but if memory serves me correctly, I wanted to have a root wildcard for only one model. So any endpoint, one class. I'm not sure if your example would work (I didn't have it in mind when I opened this PR).

@sodastsai
Copy link
Member

okay. your original idea would be

+ (NSDictionary *)modelClassesByResourcePath {
    return @{
        @"**": [TheModel class],
    };
}

so, all the response processed by OVCHTTPSessionManager or OVCHTTPRequestOperationManager will be serialized into TheModel objects.

and my comment extends above idea, the ** works like a fallback option when the request doesn't match any of previous resource paths.

I just wanna clarify your idea. :)
If my idea is the same as yours (or could fit your requirements), I'd go to check (and extend if necessary) your PR and then merge back.

@groomsy
Copy link
Author

groomsy commented Jul 9, 2015

That sounds great. It covers my limited need and makes it more extensible for future implementations. 👍

@stefreak
Copy link
Contributor

A while ago I contributed a very simple change to make this possible without a wildcard matcher: #74

sadly, I didn't really bother to document that behaviour somewhere...

sodastsai added a commit that referenced this pull request Jul 15, 2015
Explicit is better than implicit. (From the Zen of Python)
Thus I think it's better to point out that you want to match all URL
starting with a prefix into a model class.

So, the example in #94 would be refined into:
```swift
override class func modelClassesByResourcePath() -> [NSObject: AnyObject] {
    let modelClasses: [NSObject: AnyObject] = [
        "inventory/**": Item.self,
    ]
    return modelClasses
}
```
(Add a `**` to match any further paths, which is a feature proposed in #55)
@sodastsai
Copy link
Member

Merged by 9130140.

@groomsy: Thanks, 👍

@stefreak: your contribution is great. But groomsy's provides a usage like this:

NSDictionary *modelClassesByPath = @{
    @"test/*": [OVCTestModel class],
    @"test2/**": [OVCAlternativeModel class],
};

and the result would be

URL Model Class
http://example.com/api/v1/test/whatever42 OVCTestModel
http://example.com/api/v1/test/whatever/42 nil
http://example.com/api/v1/test2/whatever42 OVCAlternativeModel
http://example.com/api/v1/test2/whatever/42 OVCAlternativeModel

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants