Create highstock using AJAX/JSON - ajax

I build a highstock using AngularJs,
This is the code currently look like http://jsfiddle.net/j06ivy/r88yszk0/
My question is how can I returns JSON data (http link) instead of put these data below in javascript code?
$scope.chartConfig.series.push({
id: 1,
data: [
[1147651200000, 23.15],
[1147737600000, 23.01],
[1147824000000, 22.73],
[1147910400000, 22.83],
[1147996800000, 22.56],
[1148256000000, 22.88],
[1148342400000, 22.79],
[1148428800000, 23.50],
[1148515200000, 23.74],
[1148601600000, 23.72],
[1148947200000, 23.15],
[1149033600000, 22.65]
]
},   {
id: 2,
data: [
[1147651200000, 25.15],
[1147737600000, 25.01],
[1147824000000, 25.73],
[1147910400000, 25.83],
[1147996800000, 25.56],
[1148256000000, 25.88],
[1148342400000, 25.79],
[1148428800000, 25.50],
[1148515200000, 26.74],
[1148601600000, 26.72],
[1148947200000, 26.15],
[1149033600000, 26.65]
]
}
);
I try to build on my webserver
http://52.74.94.173/ivy-demo-project/highstock-json.html
I think if something wrong in here?
$scope.chartConfig.series.push({
data: jsonData
});

you can use the $http service, here are the docs: $http
place your data in a json file, then you can access you file locally like so
$http.get("../relativePathTolocalJSON/mydata.json");
your file needs to be in a legal JSON format

Your fiddle doesn't work because of several js errors - at least $http is not injected, missing parenthesis and your json file is not available from a domain other than yours.
Check this one and make sure that your json file is available and contains an array with two data series for the chart.
$http.get('http://j06ivy.tw/public/chart-data.json').success(function (jsonData) {
$scope.chartConfig.series.push({
data: jsonData
});
});

Related

How do I fetch from certain paths from an yaml file?

I have a create-react-app small project and I am trying to fetch from an yaml downloaded from SwaggerHubfile and I can't figure it out.
The structure of the project is as in the image:
I am trying to acces something that looks like this:
paths:
/users/jobs/list:
So far I tried this:
function getData () {
fetch("http://localhost/3000/data.json")
.then(data => data.json())
.then(resp => console.log(resp));
}
getData();
but it doesn't work. What can I do to fetch data from an yaml file? Or can I fetch straight from SwaggerHub by using just the url?

How to run the same test with multiple logins, URL's, and body elements in cypress.io

I have a simple test I want to create in cypress that would require a test where using a settings file I would create 1 test that executes for each entry in the settings file. The file would contain user/pwd/url/elementID and be used to login for each user at a custom URL, and validate that a specific elementID is displayed, logout, and do it again - iterating through the settings file until each is tested.
I want to do something like:
forEach(URL,uname,pwd,elementID) do
cy.request(URL)
cy.get('input:uname').btn.click
cy.get('input:pwd').btn.click
cy.get(data-cy=elementID).should(be present)
cy.get(btn.logout).btn.click
I highly doubt the above code is correct - but hopefully you get the idea. Main goal is to create a simple and quick script that will quickly iterate through an array to smoke test the functionality.
You can still iterate over your test data and create a test case out of each:
[
{
url,
uname,
pwd,
elementID,
}
].forEach(testData => {
it(`Test ${testData.uname} on ${testData.url}`, () => {
// your test code
});
});
Of course the array:
[
{
url,
uname,
pwd,
elementID,
}
]
does not need to be there in the same file, you can have it somewhere separate and import it into your spec file.
Caveat: You can only visit URLs from the same origin in one test! This code will only work if all URLs you want to test are from the same origin (i.e. same
Save your data in json format and put them in Cypress folder "fixtures"
[
{"user":"username1","pwd":"pwuser1","url":"url1","elementID":"#element_name1"},
{"user":"username2","pwd":"pwuser2","url":"url2","elementID":"#element_name2"}
]
(Don't forget the # in front of the element_name id)
Then this is your smoke_test.spec.js
//fetch the parameters from the file and save them as constant "login"
const login_data = require('../fixtures/login_data.json')
//Now you can fetch the parameters using "login_data"
describe('smoke test', () => {
it('loop through login list', () => {
//we call each entry "param" and loop through the lines of the json file
cy.get(login_data).each((param) => {
cy.visit(param.url)
cy.get('#id_of_username_field').type(param.user)
cy.get('#id_of_pw_field').type(param.pwd)
//The next line is only if you have a login button
cy.get('#id_of_login_button').click()
cy.get(param.elementID).should('be.visible')
cy.get('#id_of_logout_button)
})
})
})

Can I get FilePond to show previews of loaded local images?

I use FilePond to show previously uploaded images with the load functionality. The files are visible, however I don't get a preview (which I get when uploading a file).
Should it be possible to show previews for files through load?
files: [{
source: " . $profile->profileImage->id . ",
options: {
type: 'local',
}
}],
First you have to install and register File Poster and File Preview plugins and here is the example of how to register it in your code:
import * as FilePond from 'filepond';
import FilePondPluginImagePreview from 'filepond-plugin-image-preview';
import FilePondPluginFilePoster from 'filepond-plugin-file-poster';
FilePond.registerPlugin(
FilePondPluginImagePreview,
FilePondPluginFilePoster,
);
then You have to set the server.load property to your server endpoint and add a metadata property to your files object which is the link to your image on the server:
const pond = FilePond.create(document.querySelector('file'));
pond.server = {
url: '127.0.0.1:3000/',
process: 'upload-file',
revert: null,
// this is the property you should set in order to render your file using Poster plugin
load: 'get-file/',
restore: null,
fetch: null
};
pond.files = [
{
source: iconId,
options: {
type: 'local',
metadata: {
poster: '127.0.0.1:3000/images/test.jpeg'
}
}
}
];
the source property is the variable you want to send to your end point which in my case I wanted to send to /get-file/{imageDbId}.
In this case it does not matter what your endpoint in the load property returns but my guess is, we have to return a file object.

Grails 3 - Spring Rest Docs using Rest assured giving SnippetException when using JSON views

I am trying to integrate Spring REST docs with rest assured with Grails 3.1.4 application. I am using JSON Views.
Complete code is at https://github.com/rohitpal99/rest-docs
In NoteController when I use
List<Note> noteList = Note.findAll()
Map response = [totalCount: noteList.size(), type: "note"]
render response as grails.converters.JSON
Document generation works well.
But I want to use JSON views like
respond Note.findAll()
where I have _notes.gson and index.gson files in /views directory. I get a SnippetException. A usual /notes GET request response is correct.
rest.docs.ApiDocumentationSpec > test and document get request for /index FAILED
org.springframework.restdocs.snippet.SnippetException at ApiDocumentationSpec.groovy:54
with no message. Unable to track why it occurs.
Please suggest.
Full stacktrace
org.springframework.restdocs.snippet.SnippetException: The following parts of the payload were not documented:
{
"instanceList" : [ {
"title" : "Hello, World!",
"body" : "Integration Test from Hello"
}, {
"title" : "Hello, Grails",
"body" : "Integration Test from Grails"
} ]
}
at org.springframework.restdocs.payload.AbstractFieldsSnippet.validateFieldDocumentation(AbstractFieldsSnippet.java:134)
at org.springframework.restdocs.payload.AbstractFieldsSnippet.createModel(AbstractFieldsSnippet.java:74)
at org.springframework.restdocs.snippet.TemplatedSnippet.document(TemplatedSnippet.java:64)
at org.springframework.restdocs.generate.RestDocumentationGenerator.handle(RestDocumentationGenerator.java:192)
at org.springframework.restdocs.restassured.RestDocumentationFilter.filter(RestDocumentationFilter.java:63)
at com.jayway.restassured.internal.filter.FilterContextImpl.next(FilterContextImpl.groovy:73)
at org.springframework.restdocs.restassured.RestAssuredRestDocumentationConfigurer.filter(RestAssuredRestDocumentationConfigurer.java:65)
at com.jayway.restassured.internal.filter.FilterContextImpl.next(FilterContextImpl.groovy:73)
at com.jayway.restassured.internal.RequestSpecificationImpl.applyPathParamsAndSendRequest(RequestSpecificationImpl.groovy:1574)
at com.jayway.restassured.internal.RequestSpecificationImpl.get(RequestSpecificationImpl.groovy:159)
at rest.docs.ApiDocumentationSpec.$tt__$spock_feature_0_0(ApiDocumentationSpec.groovy:54)
at rest.docs.ApiDocumentationSpec.test and document get request for /index_closure2(ApiDocumentationSpec.groovy)
at groovy.lang.Closure.call(Closure.java:426)
at groovy.lang.Closure.call(Closure.java:442)
at grails.transaction.GrailsTransactionTemplate$1.doInTransaction(GrailsTransactionTemplate.groovy:70)
at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:133)
at grails.transaction.GrailsTransactionTemplate.executeAndRollback(GrailsTransactionTemplate.groovy:67)
at rest.docs.ApiDocumentationSpec.test and document get request for /index(ApiDocumentationSpec.groovy)
REST Docs will fail a test if you try to document something that isn't there or fail to document something that is there. You've documented two fields in your test:
responseFields(
fieldWithPath('totalCount').description('Total count'),
fieldWithPath('type').description("Type of result")
)))
REST Docs has failed the test as some parts of the response haven't been documented. Specifically an instanceList array that contains maps with two keys: title and body. You can document those and the other two fields with something like this:
responseFields(
fieldWithPath('totalCount').description('Total count'),
fieldWithPath('type').description("Type of result"),
fieldWithPath('instanceList[].title').description('Foo'),
fieldWithPath('instanceList[].body').description('Bar')
)))
If you don't care about potentially missing fields, you can use relaxedResponseFields instead of responseFields:
relaxedResponseFields(
fieldWithPath('totalCount').description('Total count'),
fieldWithPath('type').description("Type of result")
))
This won't fail the test if some fields are not mentioned.

How to extract individual object from an ajax callback

I am getting back the following from an AJAX call which exactly matches the syntax in my manual:
var data = {
coach100: {
PID: '23169',
POrt: '11'
},
coach200: {
PID: '23170',
POrt: '11'
}
};
Now I want to extract one piece of data. The manual syntax is:
data.coach100.PID
but the Firebug console says: "TypeError: data.coach100 is undefined"
How to format the data in the variable "data" so that it can be extracted using dot syntax?
This runs just fine in JSfiddle - http://jsfiddle.net/GWFe9/
My guess is that you're defining and calling data in the wrong place.
If you're calling your data outside of the success of the ajax but also defining it there you're going to have a bad time.
try adding var data; to the top of your doc so its a global variable - then when you get the success from ajax define your data like this -
data = {
coach100: {
PID: '23169',
POrt: '11'
},
coach200: {
PID: '23170',
POrt: '11'
}
};
then you can call it at any time after it's been loaded!

Resources