Transforming JSON from API into an activity stream

那年仲夏 提交于 2020-01-14 14:33:28

问题


I am doing a project for fun and one of the requirements is to the JSON data from an API of my choice and transform it into an activity stream using angularjs.

I've googled and read what an activity stream is and I believe I have a basic understanding of it. It's essentially transforming an action into a specific JSON format using the keywords "actor", "verb", "object", and "target".

When I look at examples, I understand why that specific JSON string is considered an activity stream.

Example:

  {
"published": "2011-02-10T15:04:55Z",
"actor": {
  "url": "http://example.org/martin",
  "objectType" : "person",
  "id": "tag:example.org,2011:martin",
  "image": {
    "url": "http://example.org/martin/image",
    "width": 250,
    "height": 250
  },
  "displayName": "Martin Smith"
},
"verb": "post",
"object" : {
  "url": "http://example.org/blog/2011/02/entry",
  "id": "tag:example.org,2011:abc123/xyz"
},
"target" : {
  "url": "http://example.org/blog/",
  "objectType": "blog",
  "id": "tag:example.org,2011:abc123",
  "displayName": "Martin's Blog"
}

}

But I'm confused in terms of how to transform my JSON Data into an activity stream.

My JSON doesn't have an "actor", nor does it have a "verb". If anyone can please explain or provide a solution that would be greatly appreciated. Thank you!

Weather API

{
  "coord": {
    "lon": -0.13,
    "lat": 51.51
  },
  "weather": [
    {
      "id": 300,
      "main": "Drizzle",
      "description": "light intensity drizzle",
      "icon": "09d"
    }
  ],
  "base": "stations",
  "main": {
    "temp": 280.32,
    "pressure": 1012,
    "humidity": 81,
    "temp_min": 279.15,
    "temp_max": 281.15
  },
  "visibility": 10000,
  "wind": {
    "speed": 4.1,
    "deg": 80
  },
  "clouds": {
    "all": 90
  },
  "dt": 1485789600,
  "sys": {
    "type": 1,
    "id": 5091,
    "message": 0.0103,
    "country": "GB",
    "sunrise": 1485762037,
    "sunset": 1485794875
  },
  "id": 2643743,
  "name": "London",
  "cod": 200
}

回答1:


ActivityStream is just a specification for activities. Weather is just a model. A model can either be a subject/actor or an object that an actor acts upon to. In this case, weather can only be an object.

An example of activity that deals with weather is:

Martin checks the weather for London at 3:04 PM UTC on February 10, 2015.

The corresponding activity JSON is:

{
  "published": "2015-02-10T15:04:55Z",
  "actor": {
    "url": "http://example.org/martin",
    "objectType" : "person",
    "id": "tag:example.org,2011:martin",
    "image": {
      "url": "http://example.org/martin/image",
      "width": 250,
      "height": 250
    },
    "displayName": "Martin Smith"
  },
  "verb": "search",
  "object" : {
    "url": "http://api.openweathermap.org/data/2.5/weather?q=London",
    "name": "London's weather",
    "published": "2015-02-10T15:04:55Z"
  }
}

Note that this is only an example. You may have different activities, depending on how you want to use the weather data.




回答2:


The activity stream concept is pretty simple. Here's a good example: "Wendy adds London to her list Places to visit"

In this case the actor is "wendy", the object is "london" the verb is "add" and the target is "places to visit".

You can use this syntax to support different use cases. I've seen people us it the activity stream spec for everything ranging from Leed certification of buildings, error reporting to social apps.

Example 2:

Application Uber broke down with error code 2 and has been assigned to John

  • Actor: application:uber
  • Verb: break
  • Object: Error:2
  • Target: user:John

Example 3:

RoadToVR published an article about Echo Arena

  • Actor: RoadToVR
  • Verb: published
  • Object: Article:123

Advanced fields

The spec also outlines the TO field. You can use the TO field to support @mentions, hashtags and notifications.

Version 2

There is also a new version of the activity stream spec available: https://www.w3.org/TR/activitystreams-core/ So far I don't see any evidence of applications adopting this new spec.

Activity stream tutorial

This tutorial is a nice starting point if you want to build a Twitter style feed: https://getstream.io/get_started/



来源:https://stackoverflow.com/questions/44900776/transforming-json-from-api-into-an-activity-stream

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!