I am integrating with Scalable Press API. I am using HTTparty to handle the api response.
Example request:
curl "https://api.scalablepress.com/v2/products/gildan-sweatshirt-crew"
Example response:
{
"comments": "Generous fit. Soft, sturdy, easy to move around in, all the while looking good.",
"description": "Air Jet Spun Yarn. Double-needle stitching. Set-in sleeves. 1x1 Athletic Rib with Lycra(R). Quarter-turned to eliminate center crease.",
"name": "Gildan Sweatshirt - Crew",
"type": "Garment",
"properties": {
"brand": "Gildan",
"material": "7.75 oz 50% cotton, 50% polyester.",
"style": "18000"
},
"colors": [{
"name": "Kiwi",
"hex": "88b95d",
"images": [{
"url": "http://i1.ooshirts.com/images/lab_shirts/Kiwi-5-R.jpg",
"label": "Right"
}, {
"url": "http://i1.ooshirts.com/images/lab_shirts/Kiwi-5-L.jpg",
"label": "Left"
}, {
"url": "http://i1.ooshirts.com/images/lab_shirts/Kiwi-5-F.jpg",
"label": "Front"
}, {
"url": "http://i1.ooshirts.com/images/lab_shirts/Kiwi-5-B.jpg",
"label": "Back"
}],
"sizes": [
"sml",
"med",
"lrg",
"xlg",
"xxl",
"xxxl",
"xxxxl",
"xxxxxl"
]
}, {
"name": "Irish Green",
"hex": "3da858",
"images": [{
"url": "http://i1.ooshirts.com/images/lab_shirts/Irish-Green-5-F.jpg",
"label": "Front"
}, {
"url": "http://i1.ooshirts.com/images/lab_shirts/Irish-Green-5-L.jpg",
"label": "Left"
}, {
"url": "http://i1.ooshirts.com/images/lab_shirts/Irish-Green-5-R.jpg",
"label": "Right"
}, {
"url": "http://i1.ooshirts.com/images/lab_shirts/Irish-Green-5-B.jpg",
"label": "Back"
}],
"sizes": [
"sml",
"med",
"lrg",
"xlg",
"xxl",
"xxxl",
"xxxxl",
"xxxxxl"
]
}],
"additionalImages": [{
"label": "Front",
"url": "http://i1.ooshirts.com/products/5/front.jpg"
}, {
"label": "Back",
"url": "http://i1.ooshirts.com/products/5/back.jpg"
}, {
"label": "Collar",
"url": "http://i1.ooshirts.com/products/5/collar.jpg"
}],
"image": {
"label": "Catalog",
"url": "http://www.ooshirts.com/products/5/catalog.jpg"
},
"available": true,
"url": "https://api.scalablepress.com/v2/products/gildan-sweatshirt-crew",
"availabilityUrl": "https://api.scalablepress.com/v2/products/gildan-sweatshirt-crew/availability",
"productId": "gildan-sweatshirt-crew"
}
This response isn't too bad, but some products have upwards of 50 colors and each color has around 4 images.
Question:
I am looking to make my code a little better and easier to access the images. Is there a better way to handle the array?
View: <%= image_tag #product['colors'].to_a[0].to_a[2].to_a[1].to_a[1].to_a[0].to_a[1] %>
controller:
def show_product
#product = scalable_press.show_product(params[:product])
end
private
def scalable_press
ScalablePress.new
end
class ScalablePress
include HTTParty
base_uri 'https://api.scalablepress.com'
def initialize
#options = { basic_auth: { password: 'APIKEY' }, verify: false}
end
def products_in_category(category)
response = self.class.get("https://api.scalablepress.com/v2/categories/#{category}", #options)
response["products"]
end
def categories
response = self.class.get("https://api.scalablepress.com/v2/categories", #options)
end
def show_product(product)
response = self.class.get("https://api.scalablepress.com/v2/products/#{product}", #options)
# response["products"]
end
end
Related
I have an application that is pushing data into IoT Hub which is being used as a data source for TSI. Below is an example message:
{
"EnqueuedTimeUtc": "2021-06-17T22:00:47.2170000Z",
"Properties": {},
"SystemProperties": {
"connectionDeviceId": "Device1",
"connectionAuthMethod": "{\"scope\":\"device\",\"type\":\"sas\",\"issuer\":\"iothub\",\"acceptingIpFilterRule\":null}",
"connectionDeviceGenerationId": "637425408342887985",
"contentType": "application/json",
"contentEncoding": "utf-8",
"enqueuedTime": "2021-06-17T22:00:47.2170000Z"
},
"Body": {
"topic": {
"namespace": "spBv1.0",
"edgeNodeDescriptor": "Routed Group/E2",
"groupId": "Routed Group",
"edgeNodeId": "E2",
"deviceId": "D2",
"type": "DBIRTH"
},
"payload": {
"timestamp": "2021-06-17T22:00:47.082Z",
"metrics": [{
"name": "Ramp1",
"timestamp": "2021-06-17T22:00:47.082Z",
"dataType": "Int32",
"metaData": {},
"properties": {
"Quality": {
"type": "Int32",
"value": 192
},
"My Property": {
"type": "String",
"value": "{\"\":\"\"}"
}
},
"value": 77
}],
"seq": 1
}
}
}
I found documentation showing that my array of 'metrics' is supported as shown here:
https://learn.microsoft.com/en-us/azure/time-series-insights/concepts-json-flattening-escaping-rules
With this message, I can see 'Ramp1' show up in TSI with a value and timestamp as expected. However, the 'properties' under each metric do not show up. In this example that is 'Quality' and 'My Property'. Is there a way to get this data into TSI with an association to 'Ramp1'?
I'm trying to add padding to my adaptive card view so that it's contents are inset from the edge of the card. I'd like to not adjust the padding of any of the internal card elements. I'm trying to use the following host config, which parses without error but seems to have no effect on the card.
Host config JSON:
{
"spacing": {
"small": 3,
"default": 8,
"medium": 20,
"large": 30,
"extraLarge": 40,
"padding": 100
},
"adaptiveCard": {
"allowCustomStyle": true,
"spacing": {
"padding": 100
}
}
}
Resulting card:
As you can see, there is certainly not 100px of padding being added to the card. I've used sample host configs and tweak other settings like colors so I know the config is being applied, but nothing I do seems to affect the card padding. Thanks in advance!
Card JSON:
{
"type": "AdaptiveCard",
"body": [
{
"type": "Container",
"items": [
{
"type": "TextBlock",
"size": "Medium",
"weight": "Bolder",
"text": "Publish Adaptive Card schema"
},
{
"type": "ColumnSet",
"columns": [
{
"type": "Column",
"items": [
{
"type": "Image",
"style": "Person",
"url": "https://pbs.twimg.com/profile_images/3647943215/d7f12830b3c17a5a9e4afcc370e3a37e_400x400.jpeg",
"size": "Small"
}
],
"width": "auto"
},
{
"type": "Column",
"items": [
{
"type": "TextBlock",
"weight": "Bolder",
"text": "Matt Hidinger",
"wrap": true
},
{
"type": "TextBlock",
"spacing": "None",
"text": "Created {{DATE(2017-02-14T06:08:39Z,SHORT)}}",
"isSubtle": true,
"wrap": true
}
],
"width": "stretch"
}
]
}
]
},
{
"type": "Container",
"items": [
{
"type": "TextBlock",
"text": "Now that we have defined the main rules and features of the format, we need to produce a schema and publish it to GitHub. The schema will be the starting point of our reference documentation.",
"wrap": true
},
{
"type": "FactSet",
"facts": [
{
"title": "Board:",
"value": "Adaptive Card"
},
{
"title": "List:",
"value": "Backlog"
},
{
"title": "Assigned to:",
"value": "Matt Hidinger"
},
{
"title": "Due date:",
"value": "Not set"
}
]
}
]
}
],
"actions": [
{
"type": "Action.ShowCard",
"title": "Set due date",
"card": {
"type": "AdaptiveCard",
"body": [
{
"type": "Input.Date",
"id": "dueDate"
},
{
"type": "Input.Text",
"id": "comment",
"placeholder": "Add a comment",
"isMultiline": true
}
],
"actions": [
{
"type": "Action.OpenUrl",
"title": "OK",
"url": "http://adaptivecards.io"
}
],
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json"
}
},
{
"type": "Action.OpenUrl",
"title": "View",
"url": "http://adaptivecards.io"
}
],
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"version": "1.0"
}
I'm using swagger for quite a bit now, we have started documenting our code using it, in one place there's an API response which returns multiple objects in the included block.
Example:
{
"data": {
"id": "1",
"type": "schoolPositions",
"attributes": {
"description": "teases the students",
"mustHaves": "principle"
},
"relationships": {
"schoolLocation": {
"data": {
"id": "72",
"type": "schoolLocations"
}
},
"schoolCompensation": {
"data": {
"id": "75",
"type": "schoolCompensations"
}
},
"jobSpecs": {
"data": [
{
"id": "82",
"type": "schoolAttachments"
}
]
}
}
},
"included": [
{
"id": "72",
"type": "schoolLocations",
"attributes": {
"city": "Berhampore",
"state": "West Bengal",
"postalCode": "742101",
"country": "India",
"globalRegionId": 30,
"regionId": 683
}
},
{
"id": "75",
"type": "schoolCompensations",
"attributes": {
"salary": "",
"bonus": "",
"equity": "",
"currencyId": null,
"equityType": "percent",
"salaryDescription": null
}
},
{
"id": "82",
"type": "schoolAttachments",
"attributes": {
"attachmentType": "JobSpecificationAttachmentType",
"fileFileName": "vs.jpg",
"fileContentType": "image/jpeg",
"fileFileSize": 2410039,
"fileUpdatedAt": "2018-12-12T07:06:38Z",
"downloadUrl": "001-vs.jpg?1544598398",
"klass": "SchoolAttachments"
}
}
]
I have wasted an entire day on the internet and documentation trying to document the included part, but I'm going wrong somewhere
response 200 do
key :description, 'School Data'
schema do
property :data do
key :type, :array
items do
key :'$ref', :School
end
end
property :included do
key :type, :array
items do
key :'$ref', :SchoolLocations
key :'$ref', :SchoolCompensations
key :'$ref', :SchoolAttachments
end
end
end
end
This shows only the SchoolAttachments in the included part.
I have tried using allOff but it doesn't work.
Issue
We are currently implementing a highcharts graph for large datasets that will require the boost module for performance reasons. However, on enabling the boost module we have encountered an intermittent issue that is proving challenging to consistently reproduce and isolate.
Because I am not able to replicate this I cannot create a jsfiddle as of yet. Does anyone have any idea how to isolate and fix this?
The graph should look like this:
Expected graph
But occasionally does this:
Actual graph
This has also happened on another occasion whilst manually testing with different data:
With different data
It has been observed on
Chrome 70.0.3538.67
IE 11.345.17134.0
Highcharts JS & Boost module v6.1.4
Input data
Sample of the type of data:
{
"label": "2018-04-19T15:17:02",
"value": "1"
}, {
"label": "2018-04-19T15:17:05",
"value": "9620035.36877074"
}, {
"label": "2018-04-19T15:17:59",
"value": "9583104.14689662"
}, {
"label": "2018-04-19T15:18:46",
"value": "9604094.84064805"
}, {
"label": "2018-04-19T15:20:17",
"value": "9571679.9536289"
}, {
"label": "2018-04-20T08:18:24",
"value": "7902991.39771514"
}
Or:
{
"label": "2018-04-19T15:17:59",
"value": "1.4132e-007"
}, {
"label": "2018-04-19T15:18:46",
"value": "1.41434e-007"
}, {
"label": "2018-04-19T15:20:17",
"value": "1.41355e-007"
}, {
"label": "2018-04-20T08:18:24",
"value": "1.40532e-007"
}, {
"label": "2018-04-20T08:20:35",
"value": "1.40928e-007"
}, {
"label": "2018-04-20T08:23:51",
"value": "1.4078e-007"
}, {
"label": "2018-04-20T08:24:39",
"value": "1.40901e-007"
}
Graph config
{
"chart": {
"type": "line",
"zoomType": "x",
"panning": true,
"panKey": "shift"
},
"boost": {
"usePreallocated": false
},
...
y-axis label customisation
title
exporting
etc
...
"series": [{
"showInLegend": false,
"name": "",
"data": []
}],
}
Update mechanism
When the HTTP response comes back the following is called on the graph instance:
graphToUpdate.series[0].setData(parsedData, true, true, false);
I'm having problems extracting values in a response hash from a call to google image search API.
I only want the value from the url key from each result.
I figured i'd call deep_symbolize_keys on the response and do something like hash.results.url , which doesn't work. I feel like i'm doing something stupid, so any pointers are welcome.
CONTROLLER CODE
_url = 'http://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=fuzzy%20monkey'
_response = Net::HTTP.get_response(URI.parse(_url))
#response_hash = JSON.parse _response.body
RESPONSE HASH BEFORE KEYED STORED IN #response_hash
"responseData": {
"results": [
{
"GsearchResultClass": "GimageSearch",
"width": "1152",
"height": "864",
"imageId": "ANd9GcQQigy-U6KTXke82n5hma5qvFM2UyVnkGtJme6pkZgl_1GYM--Yb90oqnOJ",
"tbWidth": "150",
"tbHeight": "113",
"unescapedUrl": "http://www.blirk.net/wallpapers/1152x864/fuzzy-monkey-1.jpg",
"url": "http://www.blirk.net/wallpapers/1152x864/fuzzy-monkey-1.jpg",
"visibleUrl": "www.blirk.net",
"title": "<b>Fuzzy Monkey</b> Normal 1152x864",
"titleNoFormatting": "Fuzzy Monkey Normal 1152x864",
"originalContextUrl": "http://www.blirk.net/fuzzy-monkey/1/1152x864/",
"content": "<b>Fuzzy Monkey</b> Normal 1152x864",
"contentNoFormatting": "Fuzzy Monkey Normal 1152x864",
"tbUrl": "http://t1.gstatic.com/images?q=tbn:ANd9GcQQigy-U6KTXke82n5hma5qvFM2UyVnkGtJme6pkZgl_1GYM--Yb90oqnOJ"
},
{
"GsearchResultClass": "GimageSearch",
"width": "600",
"height": "397",
"imageId": "ANd9GcRpZyXXWBk0TJuU6PCdvrgrU7QckCJQ5DP96iyLc6uLx1bQn4EvBZDFLCk",
"tbWidth": "135",
"tbHeight": "89",
"unescapedUrl": "http://www.acuteaday.com/blog/wp-content/uploads/2011/05/fuzzy-snub-nosed-monkey.jpg",
"url": "http://www.acuteaday.com/blog/wp-content/uploads/2011/05/fuzzy-snub-nosed-monkey.jpg",
"visibleUrl": "www.acuteaday.com",
"title": "<b>Monkeys</b> » A Cute A Day",
"titleNoFormatting": "Monkeys » A Cute A Day",
"originalContextUrl": "http://www.acuteaday.com/blog/category/monkeys/",
"content": "<b>Monkeys</b> » A Cute A Day",
"contentNoFormatting": "Monkeys » A Cute A Day",
"tbUrl": "http://t1.gstatic.com/images?q=tbn:ANd9GcRpZyXXWBk0TJuU6PCdvrgrU7QckCJQ5DP96iyLc6uLx1bQn4EvBZDFLCk"
},
{
"GsearchResultClass": "GimageSearch",
"width": "800",
"height": "600",
"imageId": "ANd9GcQGs5BxeWNBIqhM5vL6dVaPcqkopWN8HrqXNdBiuq54HFOXzHBbtgu2wpE",
"tbWidth": "143",
"tbHeight": "107",
"unescapedUrl": "http://wild-facts.com/wp-content/uploads/2010/09/woolly_monkey1.jpg",
"url": "http://wild-facts.com/wp-content/uploads/2010/09/woolly_monkey1.jpg",
"visibleUrl": "www.wild-facts.com",
"title": "Facts about the Woolly <b>Monkey</b> | Wild Facts",
"titleNoFormatting": "Facts about the Woolly Monkey | Wild Facts",
"originalContextUrl": "http://www.wild-facts.com/2010/wild-fact-723-it-is-not-a-fuzzy-monkey-woolly-monkey/",
"content": "Facts about the Woolly <b>Monkey</b> | Wild Facts",
"contentNoFormatting": "Facts about the Woolly Monkey | Wild Facts",
"tbUrl": "http://t1.gstatic.com/images?q=tbn:ANd9GcQGs5BxeWNBIqhM5vL6dVaPcqkopWN8HrqXNdBiuq54HFOXzHBbtgu2wpE"
},
{
"GsearchResultClass": "GimageSearch",
"width": "1200",
"height": "1600",
"imageId": "ANd9GcQvIhN2ozrnr6ujdQSbknSV2hexAuA-lP5X22UsDCYzmTsolC97nfjXAVAB",
"tbWidth": "113",
"tbHeight": "150",
"unescapedUrl": "http://4.bp.blogspot.com/_Q6jkicsZAr0/TN1A8bLuGVI/AAAAAAAADoA/NyC1Xl8bNOc/s1600/Fuzzy+Monkey+017.JPG",
"url": "http://4.bp.blogspot.com/_Q6jkicsZAr0/TN1A8bLuGVI/AAAAAAAADoA/NyC1Xl8bNOc/s1600/Fuzzy%2BMonkey%2B017.JPG",
"visibleUrl": "www.inkingpink.com",
"title": "inkingpink: <b>Fuzzy Monkey</b>",
"titleNoFormatting": "inkingpink: Fuzzy Monkey",
"originalContextUrl": "http://www.inkingpink.com/2010/11/fuzzy-monkey.html",
"content": "inkingpink: <b>Fuzzy Monkey</b>",
"contentNoFormatting": "inkingpink: Fuzzy Monkey",
"tbUrl": "http://t0.gstatic.com/images?q=tbn:ANd9GcQvIhN2ozrnr6ujdQSbknSV2hexAuA-lP5X22UsDCYzmTsolC97nfjXAVAB"
}
],
"cursor": {
"resultCount": "2,990,000",
"pages": [
{
"start": "0",
"label": 1
},
{
"start": "4",
"label": 2
},
{
"start": "8",
"label": 3
},
{
"start": "12",
"label": 4
},
{
"start": "16",
"label": 5
},
{
"start": "20",
"label": 6
},
{
"start": "24",
"label": 7
},
{
"start": "28",
"label": 8
}
],
"estimatedResultCount": "2990000",
"currentPageIndex": 0,
"moreResultsUrl": "http://www.google.com/images?oe=utf8&ie=utf8&source=uds&start=0&hl=en&q=fuzzy+monkey",
"searchResultTime": "0.21"
}
},
"responseDetails": null,
"responseStatus": 200
}
are you looking for something like this?
urls = _response["responseData"]["results"].map { |result| result["url"] }