All Downloads are FREE. Search and download functionalities are using the official Maven repository.

fr.braindead.npmjava.command.NpmView Maven / Gradle / Ivy

The newest version!
package fr.braindead.npmjava.command;

import com.google.gson.*;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: leiko
 * Date: 21/01/14
 * Time: 14:09
 */
public class NpmView {
    
    public JsonObject execute(String pattern) throws Exception {
        URL url = new URL("http://registry.npmjs.org/"+pattern);
        URLConnection conn = url.openConnection();
        InputStream is = conn.getInputStream();
        JsonParser jsonParser = new JsonParser();
        JsonObject data = (JsonObject) jsonParser.parse(new InputStreamReader(is));
        JsonObject viewResult = new JsonObject();

        if (data.has("error")) {
            throw new Exception("Unable to find "+pattern+" in npm registry");

        } else {
            JsonObject versions = data.getAsJsonObject("versions");
            JsonPrimitive latest = data.getAsJsonObject("dist-tags").getAsJsonPrimitive("latest");
            JsonArray arrayVersions = new JsonArray();
            for (Map.Entry entry : versions.entrySet()) {
                arrayVersions.add(new JsonPrimitive(entry.getKey()));
            }
            viewResult.add("versions", arrayVersions);
            viewResult.add("version", latest);
            // TODO add all other information based on "npm view" specification
            return viewResult;
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy