text: !function(f){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=f();else if("function"==typeof define&&define.amd)define([],f);else{var g;g="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this,g.hydra=f()}}(function(){return function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a="function"==typeof require&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}for(var i="function"==typeof require&&require,o=0;o<r.length;o++)s(r[o]);return s}({1:[function(require,module){var hydra=require("./lib/core"),utils=require("./lib/utils");require("./lib/http-client"),require("./lib/model"),require("./lib/validation"),hydra.loadApi=function(url){return hydra.httpClient.request("GET",url).then(function(response){return hydra.api(response.body,url)}).then(function(api){return api.iri=url,api})},hydra.documentFromResponse=function(response){return hydra.httpClient.apiLink(response.headers,response.request.url).then(function(apiUrl){return hydra.loadApi(apiUrl)}).then(function(api){return hydra.document(api,response.body,response.request.url)}).then(function(document){return hydra.httpClient.contentLocation(response.headers,response.request.url).then(function(contentLocation){return document.iri=contentLocation||response.request.url,document})})},hydra.loadDocument=function(url){return hydra.httpClient.request("GET",url).then(function(response){return hydra.documentFromResponse(response,url)})},hydra.utils=utils,hydra.defaults={},hydra.defaults.invokeOperation=hydra.httpClient.jsonLdInvoke,hydra.defaults.validateClass=hydra.simpleValidateClass,hydra.defaults.model={},hydra.defaults.model.createInvoke=hydra.model.createHttpJsonLdInvoke,hydra.defaults.model.invokeOperation=hydra.httpClient.rawJsonLdInvoke,module.exports=hydra},{"./lib/core":2,"./lib/http-client":3,"./lib/model":4,"./lib/utils":5,"./lib/validation":6}],2:[function(require,module){var utils=require("./utils"),_=utils.require("lodash"),jsonldp=utils.require("jsonld").promises(),hydra={};hydra.ns={vocab:"http://www.w3.org/ns/hydra/core#",apiDocumentation:"http://www.w3.org/ns/hydra/core#apiDocumentation",member:"http://www.w3.org/ns/hydra/core#member"};var rdfs={ns:{comment:"http://www.w3.org/2000/01/rdf-schema#comment",label:"http://www.w3.org/2000/01/rdf-schema#label",range:"http://www.w3.org/2000/01/rdf-schema#range"}};hydra.Api=function(def){var self=this;this.iri=utils.iri(def),this.init=function(){var classFrameDef={"@context":{"@vocab":hydra.ns.vocab,label:rdfs.ns.label},"@type":"Class"},operationFrameDef={"@context":{"@vocab":hydra.ns.vocab,label:rdfs.ns.label},"@type":"Operation"};return Promise.all([jsonldp.frame(def,classFrameDef).then(function(classDef){self.classDef=classDef}),jsonldp.frame(def,operationFrameDef).then(function(operationDef){self.operationDef=operationDef})]).then(function(){var inits=[];return self.classes=self.classDef["@graph"].map(function(def){var instance=new hydra.Class(self,def);return inits.push(instance.init()),instance}),self.findClass=_.find.bind(null,self.classes,"iri"),self.operations=self.operationDef["@graph"].map(function(def){var instance=new hydra.Operation(self,def);return inits.push(instance.init()),instance}),self.findOperation=_.find.bind(null,self.operations,"iri"),Promise.all(inits).then(function(){return self})})}},hydra.Document=function(api,def,iri){var self=this;this.api=api,this.iri=iri||utils.iri(def),this.init=function(){return Promise.resolve().then(function(){return def=utils.unwrap(def),"@type"in def?(self.classes=_.values(def["@type"]).filter(function(type){return!!self.api.findClass(type)}).map(function(type){return new hydra.ClassDocument(self,self.api.findClass(type),def)}),self.properties=self.classes.map(function(documentClass){return documentClass["abstract"].properties.filter(function(abstractProperty){return abstractProperty.iri in def}).map(function(abstractProperty){return new hydra.PropertyDocument(self,abstractProperty,def[abstractProperty.iri])})}).reduce(function(properties,classProperties){return properties.concat(classProperties)},[]),self.findProperty=_.find.bind(null,self.properties,"iri"),self.findOperation=function(){if(1===arguments.length){var method=arguments[0];return self.classes.map(function(documentClass){return documentClass.findOperation(method)}).shift()}var iri=arguments[0],method=arguments[1],documentProperty=self.findProperty(iri);return documentProperty?documentProperty.findOperation(method):void 0},self):Promise.reject("type missing")})}},hydra.Class=function(api,def){var self=this;this.api=api,this.iri=def["@id"],this.label=def.label,this.init=function(){return Promise.resolve().then(function(){return self.operations=utils.toArray(def.supportedOperation).map(function(operationDef){return self.api.findOperation(operationDef["@id"])}),self.findOperation=_.find.bind(null,self.operations,"method"),self.properties=utils.toArray(def.supportedProperty).map(function(propertyDef){return new hydra.Property(self.api,propertyDef)}),self.findProperty=_.find.bind(null,self.properties,"iri"),self})},this.validate=hydra.defaults.validateClass},hydra.ClassDocument=function(document,abstract,def){this.document=document,this.iri=abstract.iri,this["abstract"]=abstract,this.label=this["abstract"].label,this.operations=abstract.operations.map(function(operation){return new hydra.OperationDocument(document,operation)}),this.properties=abstract.properties.filter(function(property){return property.iri in def}).map(function(property){return new hydra.PropertyDocument(document,property,def[property.iri])}),this.findOperation=_.find.bind(null,this.operations,"method"),this.findProperty=_.find.bind(null,this.properties,"iri")},hydra.Operation=function(api,def){var self=this;this.api=api,this.iri=def["@id"],this.label=def.label,this.init=function(){return Promise.resolve().then(function(){return self.method=def.method,self.statusCodes=def.statusCodes,self.expects=self.api.findClass(def.expects),self.returns=self.api.findClass(def.returns),self})}},hydra.OperationDocument=function(document,abstract,def){this.document=document,this.iri=abstract.iri,this["abstract"]=abstract,this.link=def?utils.iri(def):null,this.label=this["abstract"].label,this.method=this["abstract"].method,this.statusCodes=this["abstract"].statusCodes,this.expects=this["abstract"].expects,this.returns=this["abstract"].returns,this.invoke=hydra.defaults.invokeOperation.bind(this)},hydra.Property=function(api,def){var self=this;this.api=api,this.iri=utils.iri(def.property),this.title=def.title,this.description=def.description,this.label=def.label,this.readonly=def.readonly,this.writeonly=def.writeonly,this.operations=utils.toArray(def.property.supportedOperation).map(function(operationDef){return self.api.findOperation(utils.iri(operationDef))}),this.findOperation=_.find.bind(null,this.operations,"method")},hydra.PropertyDocument=function(document,abstract,def){this.document=document,this.iri=abstract.iri,this["abstract"]=abstract,this.link=def?utils.iri(def):null,this.label=this["abstract"].label,this.operations=abstract.operations.map(function(operation){return new hydra.OperationDocument(document,operation,def)}),this.findOperation=_.find.bind(null,this.operations,"method")},hydra.api=function(json,base){return _.isString(json)&&(json=JSON.parse(json)),jsonldp.expand(json,{base:base}).then(function(compact){return new hydra.Api(compact).init()})},hydra.document=function(api,json,base){return _.isString(json)&&(json=JSON.parse(json)),jsonldp.expand(json,{base:base}).then(function(compact){return new hydra.Document(api,compact,base).init()})},module.exports=hydra},{"./utils":5}],3:[function(require){var hydra=require("./core"),utils=require("./utils"),jsonld=utils.require("jsonld"),jsonldp=utils.require("jsonld").promises();hydra.httpClient={},"undefined"!=typeof XMLHttpRequest?(hydra.httpClient.utf8Encode=function(string){string=string.replace(/\r\n/g,"\n");for(var utftext="",n=0;n<string.length;n++){var c=string.charCodeAt(n);128>c?utftext+=String.fromCharCode(c):c>127&&2048>c?(utftext+=String.fromCharCode(c>>6|192),utftext+=String.fromCharCode(63&c|128)):(utftext+=String.fromCharCode(c>>12|224),utftext+=String.fromCharCode(c>>6&63|128),utftext+=String.fromCharCode(63&c|128))}return utftext},hydra.httpClient.base64Encode=function(input){var chr1,chr2,chr3,enc1,enc2,enc3,enc4,keyStr="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",output="",i=0;for(input=hydra.httpClient.utf8Encode(input);i<input.length;)chr1=input.charCodeAt(i++),chr2=input.charCodeAt(i++),chr3=input.charCodeAt(i++),enc1=chr1>>2,enc2=(3&chr1)<<4|chr2>>4,enc3=(15&chr2)<<2|chr3>>6,enc4=63&chr3,isNaN(chr2)?enc3=enc4=64:isNaN(chr3)&&(enc4=64),output=output+keyStr.charAt(enc1)+keyStr.charAt(enc2)+keyStr.charAt(enc3)+keyStr.charAt(enc4);return output},hydra.httpClient.requestAsync=function(method,url,headers,content,callback,options){options=options||{};var xhr=new XMLHttpRequest;xhr.onreadystatechange=function(){if(xhr.readyState===xhr.DONE){for(var headerLines=xhr.getAllResponseHeaders().split("\r\n"),resHeaders={},i=0;i<headerLines.length;i++){var headerLine=headerLines[i].split(": ",2);resHeaders[headerLine[0].toLowerCase()]=headerLine[1]}callback(xhr.status,resHeaders,xhr.responseText)}},xhr.open(method,url,!0);for(var header in headers)xhr.setRequestHeader(header,headers[header]);options.user&&options.password&&xhr.setRequestHeader("Authorization","Basic "+hydra.httpClient.base64Encode(options.user+":"+options.password)),xhr.send(content)}):hydra.httpClient.requestAsync=function(method,url,headers,content,callback,options){var request=require("request");options=options||{};var req={method:method,url:url,headers:headers,body:content};options.user&&options.password&&(req.auth={user:options.user,password:options.password}),request(req,function(error,res,body){error?callback(null,null,null,error):callback(res.statusCode,res.headers,body)})},hydra.httpClient.request=function(method,url,headers,content,options){return new Promise(function(resolve,reject){hydra.httpClient.requestAsync(method,url,headers,content,function(status,resHeaders,resBody,error){var response={status:status,headers:resHeaders,body:resBody,request:{url:url,method:method,headers:headers,body:content}};error?reject(error):status>=400?reject(new Error("status code "+status+": "+resBody)):resolve(response)},options)})},hydra.httpClient.apiLink=function(headers,base){if(!("link"in headers))return Promise.resolve(void 0);var rels=jsonld.parseLinkHeader(headers.link);return hydra.ns.apiDocumentation in rels?utils.expandIri(rels[hydra.ns.apiDocumentation].target,base):Promise.resolve(void 0)},hydra.httpClient.contentLocation=function(headers,base){return"content-location"in headers?utils.expandIri(headers["content-location"],base):Promise.resolve(void 0)},hydra.httpClient.rawInvoke=function(headers,content,options){var self=this,url=self.link||self.document.iri;return hydra.httpClient.request(self.method,url,headers,content,options)},hydra.httpClient.rawJsonLdInvoke=function(content,options){var self=this,headers={Accept:"application/ld+json"};return("PATCH"===self.method||"POST"===self.method||"PUT"===self.method)&&(headers["Content-Type"]="application/ld+json"),hydra.httpClient.rawInvoke.bind(self)(headers,JSON.stringify(content),options).then(function(response){return response.body&&""!==response.body.trim()?jsonldp.expand(JSON.parse(response.body),{base:response.request.url}).then(function(expandedBody){return response.body=expandedBody,response}):(response.body=null,response)})},hydra.httpClient.jsonLdInvoke=function(content,options){var self=this;return hydra.httpClient.rawJsonLdInvoke.bind(self)(content,options).then(function(response){return response.body})}},{"./core":2,"./utils":5,request:void 0}],4:[function(require){var hydra=require("./core"),utils=require("./utils"),_=utils.require("lodash"),jsonldp=utils.require("jsonld").promises();hydra.model={},hydra.model.createHttpJsonLdInvoke=function(operation){return function(input,options){options=options||{};var context={};return"@context"in this&&(context=this["@context"]),context=options.context||context,input&&input.toJSON&&(input=input.toJSON()),hydra.httpClient.rawJsonLdInvoke.call(operation,input,options).then(function(response){return response.body?hydra.documentFromResponse(response).then(function(document){return jsonldp.compact(response.body,context).then(function(output){return hydra.model.create(document.classes,output)})}):Promise.resolve(null)})}},hydra.model.toJSON=function(){var copyProperties=function(object,root){if(!object)return null;var copy=_.keys(object).reduce(function(json,key){var value=object[key];return _.isFunction(value)?json:_.isObject(value)&&"@omit"in value&&value["@omit"]?json:(json[key]=_.isObject(value)?copyProperties(value,root):value,json)},{});return _.isArray(object)&&(copy=_.values(object)),copy};return copyProperties(this)},hydra.model.hide=function(property){return property["@omit"]=!0,property},hydra.model.create=function(classes,properties,options){var processOperations=function(root,operations){return operations.forEach(function(operation){var key="@"+operation.method.toLowerCase();key in root||(root[key]=options.createInvoke(operation).bind(model))}),Promise.resolve()},processProperties=function(root,properties){return Promise.all(properties.map(function(property){return utils.compactIri(property.iri,model["@context"]).then(function(key){return key in root||(root[key]={}),processOperations(root[key],property.operations)})}))},processClass=function(apiClass){return model["@type"].push(apiClass.iri),processOperations(model,apiClass.operations).then(function(){return processProperties(model,apiClass.properties)})};classes=utils.toArray(classes),options=options||{},options.createInvoke=options.createInvoke||hydra.defaults.model.createInvoke;var model=_.clone(properties);return _.defaults(model,{"@context":{},toJSON:hydra.model.toJSON}),model["@type"]=[],model.api=classes[0].api||classes[0]["abstract"].api,model.api["@omit"]=!0,classes[0].document&&(model.document=classes[0].document,model.document["@omit"]=!0),Promise.all(classes.map(function(apiClass){return processClass(apiClass)})).then(function(){return model})},hydra.model.load=function(url,properties,options){return hydra.loadDocument(url).then(function(document){return hydra.model.create(document.classes,properties,options)})}},{"./core":2,"./utils":5}],5:[function(require,module){var utils={};utils.require=function(module){var globalModule=module;return"lodash"===globalModule&&(globalModule="_"),"undefined"!=typeof window&&globalModule in window?window[globalModule]:require(module)};var _=utils.require("lodash"),jsonldp=utils.require("jsonld").promises();utils.collection=function(iri,members){return{"@id":iri,"@type":"http://www.w3.org/ns/hydra/core#Collection","http://www.w3.org/ns/hydra/core#member":_.values(members).map(function(member){return{"@id":member["@id"],"@type":member["@type"]}})}},utils.compactIri=function(iri,context){var dummy={};return dummy[iri]="",jsonldp.compact(dummy,context).then(function(compactDummy){return _.keys(compactDummy).pop()})},utils.expandIri=function(iri,base){if(!base)return Promise.resolve(iri);var dummy={"@context":{"@base":base,"@vocab":"http://schema.org/"},name:{"@id":iri}};return jsonldp.expand(dummy).then(function(expanded){return expanded[0]["http://schema.org/name"][0]["@id"]})},utils.iri=function(obj){return obj=utils.unwrap(obj),obj?_.isString(obj)?obj:"@id"in obj?obj["@id"]:void 0:void 0},utils.isCollection=function(collection){return _.isObject(collection)?!1:collection.member||ns.member in collection?!0:!1},utils.toArray=function(obj){return obj?(utils.isCollection(obj)&&(obj=obj.member),_.isArray(obj)?obj:[obj]):[]},utils.unwrap=function(obj){if(!obj)return void 0;if(_.isString(obj))return obj;if("@graph"in obj&&(obj=obj["@graph"]),_.isArray(obj)){if(0===obj.length)return void 0;obj=obj[0]}return obj},module.exports=utils},{}],6:[function(require){var hydra=require("./core"),utils=require("./utils"),jsonldp=utils.require("jsonld").promises();hydra.simpleValidateClass=function(object){var self=this;return object?jsonldp.expand(object).then(function(expanded){if(expanded.length>1)return Promise.reject(new Error("object contains multiple subjects"));if(expanded=expanded.shift(),!("@type"in expanded))return Promise.reject(new Error("@type missing"));if(utils.toArray(expanded["@type"]).indexOf(self.iri)<0)return Promise.reject(new Error("expected class <"+self.iri+">"));var error=self.properties.map(function(property){return property.readonly&&property.iri in object?new Error("readonly property <"+property.iri+'> filled with value "'+object[property.iri]+'"'):!1}).filter(function(error){return!!error}).shift();return error?Promise.reject(error):Promise.resolve()}):Promise.resolve()}},{"./core":2,"./utils":5}]},{},[1])(1)});
  //# sourceMappingURL=hydra-core.min.js.map