Moya: Load stubs from files

Created on 4 Sep 2016  路  5Comments  路  Source: Moya/Moya

Hey there - I'm probably missing something suuuper obvious but is there an idiomatic Moya way for me to load stubs using .json local files?

Or should I just roll something myself and give an escaped string to sampleData.

Cheers! 馃榾

question

Most helpful comment

I don't know about being idiomatic, but you could try this approach:

var sampleData: NSData {
    let name: String

    switch self {
    case .Search:
        name = "sample_search"
        break
    }

    guard let path = NSBundle.mainBundle().pathForResource(name, ofType: "json"),
        data = NSData(contentsOfFile: path) else {
            return NSData()
    }

    return data
}

Provided you have a file named sample_search.json in your bundle.

All 5 comments

I don't know about being idiomatic, but you could try this approach:

var sampleData: NSData {
    let name: String

    switch self {
    case .Search:
        name = "sample_search"
        break
    }

    guard let path = NSBundle.mainBundle().pathForResource(name, ofType: "json"),
        data = NSData(contentsOfFile: path) else {
            return NSData()
    }

    return data
}

Provided you have a file named sample_search.json in your bundle.

Aaaand I just realised that was exactly the way to go. Just wanted to make sure there wasn't some nice way I could give a filename to sampleData rather than doing that myself. I think the escaped JSON string in the README example for sampleData made me wonder whether I had to provide some kind of escaped variant of the data also.

Thanks for your help either way - appreciate it 馃槂

@tomj How do you think we could improve our documentation around this? Any suggestions?

@ashfurrow hope #603 is up to scratch 馃槃

Looks great!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

syq7970 picture syq7970  路  3Comments

dimpiax picture dimpiax  路  3Comments

PlutusCat picture PlutusCat  路  3Comments

Tynox picture Tynox  路  3Comments

ghost picture ghost  路  3Comments