Search in sources :

Example 1 with MenigaFeed

use of com.meniga.sdk.models.feed.MenigaFeed in project mobile-sdk-android by meniga.

the class MenigaFeedConverter method responseBodyConverter.

@Override
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
    Type typeOfFeed = new TypeToken<MenigaFeed>() {
    }.getType();
    if (typeOfFeed.equals(type)) {
        return new Converter<ResponseBody, MenigaFeed>() {

            @Override
            public MenigaFeed convert(ResponseBody resBody) throws IOException {
                MenigaFeed feed = new MenigaFeed();
                JsonArray arr = getAsArray(resBody.byteStream());
                for (JsonElement element : arr) {
                    feed.add(feedItemFactory.getMenigaFeetItem((JsonObject) element));
                }
                try {
                    InputStreamReader isr = new InputStreamReader(resBody.byteStream());
                    JsonElement jelement = new JsonParser().parse(isr);
                    JsonObject jobject = jelement.getAsJsonObject();
                    if (jobject.has("meta")) {
                        JsonObject meta = jobject.getAsJsonObject("meta");
                        feed.setHasMoreData(meta.get("hasMoreData").getAsBoolean());
                    } else {
                        feed.setHasMoreData(true);
                    }
                } catch (Exception ex) {
                    feed.setHasMoreData(true);
                }
                return feed;
            }
        };
    }
    return null;
}
Also used : JsonArray(com.google.gson.JsonArray) Type(java.lang.reflect.Type) InputStreamReader(java.io.InputStreamReader) JsonElement(com.google.gson.JsonElement) Converter(retrofit2.Converter) JsonObject(com.google.gson.JsonObject) MenigaFeed(com.meniga.sdk.models.feed.MenigaFeed) IOException(java.io.IOException) ResponseBody(okhttp3.ResponseBody) JsonParser(com.google.gson.JsonParser)

Example 2 with MenigaFeed

use of com.meniga.sdk.models.feed.MenigaFeed in project mobile-sdk-android by meniga.

the class MenigaFeedTest method test.

@Test
public void test() throws Exception {
    // Create a MockWebServer. These are lean enough that you can create a new
    // instance for every unit test.
    MockWebServer server = new MockWebServer();
    // Schedule some responses.
    server.enqueue(new MockResponse().setBody(FileImporter.getJsonFileFromRaw("feed.json")));
    // Start the server.
    server.start();
    // Ask the server for its URL. You'll need this to make HTTP requests.
    HttpUrl baseUrl = server.url("/v1");
    MenigaSettings settings = new MenigaSettings.Builder().endpoint(baseUrl).build();
    MenigaSDK.init(settings);
    Task<MenigaFeed> feedTaskTask = MenigaFeed.fetch(new DateTime(), new DateTime()).getTask();
    feedTaskTask.waitForCompletion();
    MenigaFeed feed = feedTaskTask.getResult();
    Assert.assertNotNull(feed);
    Assert.assertEquals(12, feed.size());
    // Shut down the server. Instances cannot be reused.
    server.shutdown();
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) MockWebServer(okhttp3.mockwebserver.MockWebServer) MenigaFeed(com.meniga.sdk.models.feed.MenigaFeed) HttpUrl(okhttp3.HttpUrl) DateTime(org.joda.time.DateTime) Test(org.junit.Test)

Aggregations

MenigaFeed (com.meniga.sdk.models.feed.MenigaFeed)2 JsonArray (com.google.gson.JsonArray)1 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 JsonParser (com.google.gson.JsonParser)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 Type (java.lang.reflect.Type)1 HttpUrl (okhttp3.HttpUrl)1 ResponseBody (okhttp3.ResponseBody)1 MockResponse (okhttp3.mockwebserver.MockResponse)1 MockWebServer (okhttp3.mockwebserver.MockWebServer)1 DateTime (org.joda.time.DateTime)1 Test (org.junit.Test)1 Converter (retrofit2.Converter)1