openactive 0.1.0.rc1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +139 -1
- data/lib/openactive/base_model.rb +9 -12
- data/lib/openactive/version.rb +1 -1
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c88ea62add4f15cd402a29d350d7186717da3c8c0b7feb3eb080f08156c0a792
|
4
|
+
data.tar.gz: 0b72d6feb18932a711e2a390038aac95c6e0e028bd0b68efe61162b94d5a7939
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b60135e0c0aa90f25f80a90219a3690fc6899886f638f0940e14a3d44d1c9f1a2204a5f654b119c9164878ed016054398690587f3232978ef49ae45db9748998
|
7
|
+
data.tar.gz: 2151a398e7907a1a7f8395c547f7d2acad88c888753c121bef946306afb0052eee52c32e8e2ff909a1c33cc61d95f35c507504fd6c0798d67870c60014901fd5
|
data/README.md
CHANGED
@@ -22,8 +22,146 @@ Or install it yourself as:
|
|
22
22
|
|
23
23
|
## Usage
|
24
24
|
|
25
|
-
|
25
|
+
This package provides Ruby models for the OpenActive specifications.
|
26
26
|
|
27
|
+
It also provide a set of models for the [schema.org](https://schema.org) specifications.
|
28
|
+
|
29
|
+
Finally it provides a set of classes to handle OpenActive's [RPDE](https://developer.openactive.io/publishing-data/data-feeds/how-a-data-feed-works) data feeds.
|
30
|
+
|
31
|
+
### Models
|
32
|
+
OpenActive includes OpenActive.io objects as objects for use in Ruby. All classes can be serialized into JSON-LD, to provide easy conformance with the [Modelling Specification](https://www.openactive.io/modelling-opportunity-data/) and [Open Booking API Specification](https://www.openactive.io/open-booking-api/).
|
33
|
+
|
34
|
+
|
35
|
+
Please note that type enforcement is in place for both class construction and attribute assignment, providing an invalid type will result in a OpenActive::Exception::InvalidArgumentException being thrown.
|
36
|
+
|
37
|
+
### OpenActive
|
38
|
+
Classes for all OpenActive classes are available in the `OpenActive::Models` and `OpenActive::Enums` namespaces, and can be easily serialized to JSON-LD, as follows. Enumerations are available as `enum`s for properties that require their use.
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
event = OpenActive::Models::Event.new(
|
42
|
+
name: "Virtual BODYPUMP",
|
43
|
+
event_status: OpenActive::Enums::Schema::EventStatusType::EventScheduled
|
44
|
+
)
|
45
|
+
json_ld = event.to_json
|
46
|
+
```
|
47
|
+
|
48
|
+
Value of `jsonLd`:
|
49
|
+
```JSON
|
50
|
+
{
|
51
|
+
"@context": "https://openactive.io/",
|
52
|
+
"@type": "Event",
|
53
|
+
"name": "Virtual BODYPUMP",
|
54
|
+
"eventStatus": "https://schema.org/EventScheduled"
|
55
|
+
}
|
56
|
+
```
|
57
|
+
|
58
|
+
### Schema
|
59
|
+
The OpenActive data model builds on top of Schema.org, which means that you are free to use additional schema.org properties within OpenActive published data.
|
60
|
+
|
61
|
+
To reflect this, OpenActive uses inheritance to build ontop of a copy of Schema.org, these are available within the OpenActive::Models::Schema and OpenActive::Enums::Schema namespaces. And so makes it easy to use additional properties from schema.org on any given type.
|
62
|
+
|
63
|
+
## RPDE Feed Publishing
|
64
|
+
|
65
|
+
To publish an OpenActive data feed (see this [video explainer](https://developer.openactive.io/publishing-data/data-feeds/how-a-data-feed-works)), The OpenActive gem provides a drop-in solution to render the feed pages. This also includes validation for the underlying feed query.
|
66
|
+
|
67
|
+
### Modified Timestamp and ID Ordering Strategy
|
68
|
+
|
69
|
+
`OpenActive::Rpde::RpdeBody.create_from_modified_id(feedBaseUrl, afterTimestamp, afterId, items)`
|
70
|
+
|
71
|
+
Creates a new RPDE Page based on the RPDE Items provided using the [Modified Timestamp and ID Ordering Strategy](https://www.w3.org/2017/08/realtime-paged-data-exchange/#modified-timestamp-and-id), given the `afterTimestamp` and `afterId` parameters of the current query. Also validates that the items are in the correct order, throwing a `SerializationException` if this is not the case.
|
72
|
+
|
73
|
+
```ruby
|
74
|
+
items = [
|
75
|
+
OpenActive::Rpde::RpdeItem.new(
|
76
|
+
Id: "1",
|
77
|
+
Modified: 3,
|
78
|
+
State: OpenActive::Rpde::RpdeState::Updated,
|
79
|
+
Kind: OpenActive::Rpde::RpdeKind::SessionSeries,
|
80
|
+
Data: @event
|
81
|
+
),
|
82
|
+
OpenActive::Rpde::RpdeItem.new(
|
83
|
+
Id: "2",
|
84
|
+
Modified: 4,
|
85
|
+
State: OpenActive::Rpde::RpdeState::Deleted,
|
86
|
+
Kind: OpenActive::Rpde::RpdeKind::SessionSeries,
|
87
|
+
Data: nil
|
88
|
+
)
|
89
|
+
]
|
90
|
+
|
91
|
+
json_ld = OpenActive::Rpde::RpdeBody.new("https://www.example.com/feed", 1, "1", items).to_json
|
92
|
+
```
|
93
|
+
|
94
|
+
|
95
|
+
### Incrementing Unique Change Number Ordering Strategy
|
96
|
+
|
97
|
+
`OpenActive::Rpde::RpdeBody.create_from_next_change_number(feedBaseUrl, afterChangeNumber, items)`
|
98
|
+
|
99
|
+
Creates a new RPDE Page based on the RPDE Items provided using the [Incrementing Unique Change Number Ordering Strategy](https://www.w3.org/2017/08/realtime-paged-data-exchange/#incrementing-unique-change-number), given the `afterChangeNumber` parameter of the current query. Also validates that the items are in the correct order, throwing a `SerializationException` if this is not the case.
|
100
|
+
|
101
|
+
```ruby
|
102
|
+
items = [
|
103
|
+
OpenActive::Rpde::RpdeItem.new(
|
104
|
+
Id: "1",
|
105
|
+
Modified: 3,
|
106
|
+
State: OpenActive::Rpde::RpdeState::Updated,
|
107
|
+
Kind: OpenActive::Rpde::RpdeKind::SessionSeries,
|
108
|
+
Data: @event
|
109
|
+
),
|
110
|
+
OpenActive::Rpde::RpdeItem.new(
|
111
|
+
Id: "2",
|
112
|
+
Modified: 4,
|
113
|
+
State: OpenActive::Rpde::RpdeState::Deleted,
|
114
|
+
Kind: OpenActive::Rpde::RpdeKind::SessionSeries,
|
115
|
+
Data: nil
|
116
|
+
)
|
117
|
+
]
|
118
|
+
|
119
|
+
json_ld = OpenActive::Rpde::RpdeBody.create_from_next_change_number("https://www.example.com/feed", 2, items).to_json
|
120
|
+
```
|
121
|
+
|
122
|
+
## Serialization
|
123
|
+
|
124
|
+
### `obj.serialize`
|
125
|
+
Returns the serialized object in hash form complying to json-ld structure (unlike .to_h this serializes everything all the way down.)
|
126
|
+
```
|
127
|
+
event.serialize
|
128
|
+
```
|
129
|
+
```
|
130
|
+
{"@type"=>"Event", "name"=>"Virtual BODYPUMP", "eventStatus"=>"https://schema.org/EventScheduled", "@context"=>["https://openactive.io/", "https://openactive.io/ns-beta"]}
|
131
|
+
```
|
132
|
+
### `obj.to_json`
|
133
|
+
Serializes down to a json-ld string.
|
134
|
+
```ruby
|
135
|
+
event.to_json
|
136
|
+
```
|
137
|
+
returns
|
138
|
+
```ruby
|
139
|
+
"{\"@type\":\"Event\",\"name\":\"Virtual BODYPUMP\",\"eventStatus\":\"https://schema.org/EventScheduled\",\"@context\":[\"https://openactive.io/\",\"https://openactive.io/ns-beta\"]}"
|
140
|
+
```
|
141
|
+
|
142
|
+
## Deserialization
|
143
|
+
```ruby
|
144
|
+
|
145
|
+
data = JSON.parse('{"@context": ["https:\/\/openactive.io\/","https:\/\/openactive.io\/ns-beta"],"type": "Action","name": "Book","target": {"type": "EntryPoint","encodingType": "application\/vnd.openactive.v1.0+json","httpMethod": "POST","type": "EntryPoint","url": "https:\/\/example.com\/orders"}}')
|
146
|
+
|
147
|
+
deserialized = OpenActive::BaseModel.deserialize(data)
|
148
|
+
|
149
|
+
pp deserialized
|
150
|
+
```
|
151
|
+
|
152
|
+
will result in
|
153
|
+
```ruby
|
154
|
+
#<OpenActive::Models::Action:0x00007ff6fd0966d0
|
155
|
+
@name="Book",
|
156
|
+
@target=
|
157
|
+
#<OpenActive::Models::EntryPoint:0x00007ff6fd095fa0
|
158
|
+
@encoding_type="application/vnd.openactive.v1.0+json",
|
159
|
+
@http_method="POST",
|
160
|
+
@url=#<URI::HTTPS https://example.com/orders>>>
|
161
|
+
=> #<OpenActive::Models::Action:0x00007ff6fd0966d0
|
162
|
+
@name="Book",
|
163
|
+
@target=#<OpenActive::Models::EntryPoint:0x00007ff6fd095fa0 @encoding_type="application/vnd.openactive.v1.0+json", @http_method="POST", @url=#<URI::HTTPS https://example.com/orders>>>
|
164
|
+
```
|
27
165
|
## Development
|
28
166
|
|
29
167
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
@@ -35,7 +35,7 @@ module OpenActive
|
|
35
35
|
|
36
36
|
val = value
|
37
37
|
|
38
|
-
val =
|
38
|
+
val = deserialize(value) if value.is_a?(Array) || value.is_a?(Hash)
|
39
39
|
|
40
40
|
return if ["context", "type"].include?(attr_name)
|
41
41
|
|
@@ -49,10 +49,7 @@ module OpenActive
|
|
49
49
|
# @param data [string,Array] If a string is provided, we attempt JSON-decoding first
|
50
50
|
# @return [object]
|
51
51
|
#
|
52
|
-
def self.
|
53
|
-
# If a string is provided, we attempt JSON-decoding first
|
54
|
-
data = JSON.parse(data) if data.is_a?(String)
|
55
|
-
|
52
|
+
def self.deserialize_class(data)
|
56
53
|
inst = new
|
57
54
|
|
58
55
|
# If data provided is not an array, return an empty class
|
@@ -65,15 +62,11 @@ module OpenActive
|
|
65
62
|
inst
|
66
63
|
end
|
67
64
|
|
68
|
-
def deserialize(*data)
|
69
|
-
self.class.deserialize(data)
|
70
|
-
end
|
71
|
-
|
72
65
|
# Returns a value from a given JSON-LD deserialized array.
|
73
66
|
#
|
74
67
|
# @param value [mixed] If an array is provided, we recursively deserialize it
|
75
68
|
# @return [mixed]
|
76
|
-
def
|
69
|
+
def self.deserialize(value)
|
77
70
|
if value.is_a?(Hash)
|
78
71
|
# If an associative array with a type, return its deserialization form,
|
79
72
|
# so that it gets converted from array to object
|
@@ -87,7 +80,7 @@ module OpenActive
|
|
87
80
|
|
88
81
|
klass = ::OpenActive::Models.const_get(type)
|
89
82
|
|
90
|
-
inst = klass.
|
83
|
+
inst = klass.deserialize_class(value)
|
91
84
|
|
92
85
|
return inst
|
93
86
|
end
|
@@ -96,12 +89,16 @@ module OpenActive
|
|
96
89
|
# If providing a non-associative array
|
97
90
|
# Loop through it and serialize each item if needed
|
98
91
|
value = value.map do |item|
|
99
|
-
|
92
|
+
deserialize(item)
|
100
93
|
end
|
101
94
|
end
|
102
95
|
value
|
103
96
|
end
|
104
97
|
|
98
|
+
def deserialize(*data)
|
99
|
+
self.class.deserialize(*data)
|
100
|
+
end
|
101
|
+
|
105
102
|
# Returns the JSON-LD representation of the given instance.
|
106
103
|
#
|
107
104
|
# @param obj [::OpenActive::BaseModel] The given instance to convert to JSON-LD
|
data/lib/openactive/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: openactive
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- OpenActive Community
|
@@ -1022,11 +1022,11 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
1022
1022
|
version: '0'
|
1023
1023
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
1024
1024
|
requirements:
|
1025
|
-
- - "
|
1025
|
+
- - ">="
|
1026
1026
|
- !ruby/object:Gem::Version
|
1027
|
-
version:
|
1027
|
+
version: '0'
|
1028
1028
|
requirements: []
|
1029
|
-
rubygems_version: 3.0.
|
1029
|
+
rubygems_version: 3.0.3
|
1030
1030
|
signing_key:
|
1031
1031
|
specification_version: 4
|
1032
1032
|
summary: OpenActive.io objects turned into strongly typed classes for use in Ruby.
|