stratumn_sdk 0.2.0 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +65 -54
- data/lib/stratumn_sdk.rb +2 -35
- data/lib/stratumn_sdk/agent.rb +48 -0
- data/lib/stratumn_sdk/segment.rb +55 -0
- data/lib/stratumn_sdk/version.rb +1 -1
- metadata +5 -5
- data/lib/stratumn_sdk/application.rb +0 -62
- data/lib/stratumn_sdk/link.rb +0 -60
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 91ec9124a7287a16c372733fc37e220f4d52f31b
|
4
|
+
data.tar.gz: 5e04851a809d1444bd2a2e29efd601f0a5e16169
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: afd69b95dd5c685aafa3e456323fa364e31fcc7ab48027238c9b6f3b85597ebebf8b5666da8d72a8492f64d9185516afeae723228e66907a639f38920ca2673b
|
7
|
+
data.tar.gz: f6826334b8421d829278f3f343b5df59142a49544f0698bf6e0be455deb8123082ebe80be5097459dde1524e8ac419f3ead962a42d8e176c11ee21bf8b3770bc
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,15 +1,15 @@
|
|
1
|
-
# Stratumn SDK for Ruby
|
1
|
+
# Stratumn SDK for Ruby [ALPHA - incompatible with production]
|
2
2
|
|
3
3
|
[](https://travis-ci.org/stratumn/stratumn-sdk-ruby.svg?branch=master)
|
4
4
|
[](https://badge.fury.io/rb/stratumn_sdk)
|
5
5
|
|
6
|
-
Interact with your Stratumn agent from your ruby
|
6
|
+
Interact with your Stratumn agent from your ruby agent
|
7
7
|
|
8
8
|
code :: https://github.com/stratumn/stratumn-sdk-ruby
|
9
9
|
|
10
10
|
## Installation
|
11
11
|
|
12
|
-
Add this line to your
|
12
|
+
Add this line to your agent's Gemfile:
|
13
13
|
|
14
14
|
```ruby
|
15
15
|
gem 'stratumn-sdk'
|
@@ -26,106 +26,105 @@ Or install it yourself as:
|
|
26
26
|
## Quickstart
|
27
27
|
|
28
28
|
```ruby
|
29
|
-
|
29
|
+
agent = StratumnSdk::Agent.load('quickstart')
|
30
30
|
|
31
|
-
|
31
|
+
segment = agent.create_map('My message map')
|
32
32
|
|
33
|
-
|
33
|
+
segment = segment.add_essage('Hello, World')
|
34
34
|
|
35
|
-
puts
|
36
|
-
puts
|
35
|
+
puts segment.meta
|
36
|
+
puts segment.state
|
37
37
|
```
|
38
38
|
|
39
39
|
## Reference
|
40
40
|
|
41
|
-
#### StratumnSdk::
|
41
|
+
#### StratumnSdk::Agent.load(url)
|
42
42
|
|
43
|
-
Returns an instance of StratumnSdk::
|
43
|
+
Returns an instance of StratumnSdk::Agent.
|
44
44
|
|
45
45
|
```ruby
|
46
|
-
|
47
|
-
puts
|
46
|
+
agent = StratumnSdk::Agent.load('http://localhost:3000')
|
47
|
+
puts agent.agent_info
|
48
48
|
```
|
49
49
|
|
50
|
-
#### StratumnSdk::
|
50
|
+
#### StratumnSdk::Agent#create_map(*args)
|
51
51
|
|
52
|
-
Creates a new map in the
|
52
|
+
Creates a new map in the agent.
|
53
53
|
|
54
54
|
```ruby
|
55
|
-
|
56
|
-
|
55
|
+
agent = StratumnSdk::Agent.load('http://localhost:3000')
|
56
|
+
segment = agent.create_map('My message map')
|
57
57
|
```
|
58
58
|
|
59
|
-
#### StratumnSdk::
|
59
|
+
#### StratumnSdk::Agent.get_segment(hash)
|
60
60
|
|
61
|
-
Returns an existing
|
61
|
+
Returns an existing segment.
|
62
62
|
|
63
63
|
```ruby
|
64
|
-
|
65
|
-
|
66
|
-
puts
|
64
|
+
agent = StratumnSdk::Agent.load('http://localhost:3000')
|
65
|
+
segment = agent.get_segment('aee5427')
|
66
|
+
puts segment.link_hash
|
67
67
|
```
|
68
68
|
|
69
|
-
#### StratumnSdk::
|
69
|
+
#### StratumnSdk::Agent.find_segments(options = {})
|
70
70
|
|
71
|
-
Returns
|
71
|
+
Returns existing segments.
|
72
72
|
|
73
|
-
|
74
|
-
application = StratumnSdk::Application.load('quickstart')
|
75
|
-
links = application.get_map('aee5427', ['tag1', 'tag2'])
|
76
|
-
```
|
77
|
-
|
78
|
-
#### StratumnSdk::get_branches(hash, tags = [])
|
73
|
+
Available options are:
|
79
74
|
|
80
|
-
|
75
|
+
- `offset`: offset of first returned segments
|
76
|
+
- `limit`: limit number of returned segments
|
77
|
+
- `mapId`: return segments with specified map ID
|
78
|
+
- `prevLinkHash`: return segments with specified previous link hash
|
79
|
+
- `tags`: return segments that contains all the tags (array)
|
81
80
|
|
82
81
|
```ruby
|
83
|
-
|
84
|
-
|
82
|
+
agent = StratumnSdk::Agent.load('http://localhost:3000')
|
83
|
+
segments = agent.find_segments(tags: ['tag1', 'tag2'])
|
85
84
|
```
|
86
85
|
|
87
|
-
#### StratumnSdk::
|
86
|
+
#### StratumnSdk::Segment.from
|
88
87
|
|
89
|
-
Returns
|
88
|
+
Returns segment from a given raw object.
|
90
89
|
|
91
90
|
```ruby
|
92
|
-
|
93
|
-
|
94
|
-
|
91
|
+
segment = StratumnSdk::Agent.from(raw_segment)
|
92
|
+
puts segment.agent
|
93
|
+
puts segment.link_hash
|
95
94
|
```
|
96
95
|
|
97
|
-
#### StratumnSdk::
|
96
|
+
#### StratumnSdk::Segment#previous
|
98
97
|
|
99
|
-
|
98
|
+
Returns the previous segment of a segment (its parent).
|
100
99
|
|
101
100
|
```ruby
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
links.map { |link| link.load }
|
101
|
+
agent = StratumnSdk::Agent.load('http://localhost:3000')
|
102
|
+
segment = agent.get_segment('aee5427')
|
103
|
+
previous = segment.previous
|
106
104
|
```
|
107
105
|
|
108
|
-
#### StratumnSdk::
|
106
|
+
#### StratumnSdk::Segment#load
|
109
107
|
|
110
|
-
|
108
|
+
Loads a full segment. Can be useful when you only have the meta data of links.
|
111
109
|
|
112
110
|
```ruby
|
113
|
-
|
114
|
-
|
115
|
-
|
111
|
+
agent = StratumnSdk::Agent.load('http://localhost:3000')
|
112
|
+
segments = agent.find_segments
|
113
|
+
|
114
|
+
segments.map { |segment| segment.load }
|
116
115
|
```
|
117
116
|
|
118
|
-
#### StratumnSdk::
|
117
|
+
#### StratumnSdk::Segment#transition_function(*args)
|
119
118
|
|
120
|
-
Executes a transition function and returns the new
|
119
|
+
Executes a transition function and returns the new segment.
|
121
120
|
|
122
121
|
```ruby
|
123
|
-
|
124
|
-
|
125
|
-
|
122
|
+
agent = StratumnSdk::Agent.load('http://localhost:3000')
|
123
|
+
segment = agent.get_segment('aee5427')
|
124
|
+
new_segment = segment.addMessage('Hello, World!')
|
126
125
|
|
127
126
|
# underscore version is also available
|
128
|
-
|
127
|
+
new_segment = segment.add_message('Hello, World!')
|
129
128
|
```
|
130
129
|
|
131
130
|
## Development
|
@@ -134,6 +133,18 @@ After checking out the repo, run `bin/setup` to install dependencies. Then, run
|
|
134
133
|
|
135
134
|
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
136
135
|
|
136
|
+
## Tests
|
137
|
+
|
138
|
+
Tests are run against a mock agent whose results are recorded by vcr.
|
139
|
+
Should you need to regenerate the cassettes or add new tests, the mock agent can be launched on port 3333.
|
140
|
+
|
141
|
+
```
|
142
|
+
$ cd spec/agent
|
143
|
+
$ npm install
|
144
|
+
$ node index.js
|
145
|
+
```
|
146
|
+
|
147
|
+
|
137
148
|
## License:
|
138
149
|
|
139
150
|
(The MIT License)
|
data/lib/stratumn_sdk.rb
CHANGED
@@ -1,45 +1,12 @@
|
|
1
1
|
require 'stratumn_sdk/version'
|
2
2
|
require 'stratumn_sdk/request'
|
3
3
|
require 'stratumn_sdk/helper'
|
4
|
-
require 'stratumn_sdk/
|
5
|
-
require 'stratumn_sdk/
|
4
|
+
require 'stratumn_sdk/agent'
|
5
|
+
require 'stratumn_sdk/segment'
|
6
6
|
|
7
7
|
require 'yaml'
|
8
8
|
|
9
9
|
##
|
10
10
|
# Allows interacting with your Stratumn agent from your ruby app
|
11
11
|
module StratumnSdk
|
12
|
-
# Configuration defaults
|
13
|
-
@config = {
|
14
|
-
base_url: 'https://stratumn.rocks',
|
15
|
-
application_url: 'https://%s.stratumn.rocks'
|
16
|
-
}
|
17
|
-
|
18
|
-
@valid_config_keys = @config.keys
|
19
|
-
|
20
|
-
# Configure through hash
|
21
|
-
def self.configure(opts = {})
|
22
|
-
opts.each do |k, v|
|
23
|
-
@config[k.to_sym] = v if @valid_config_keys.include? k.to_sym
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
# Configure through yaml file
|
28
|
-
def self.configure_with(path_to_yaml_file)
|
29
|
-
begin
|
30
|
-
config = YAML.load(IO.read(path_to_yaml_file))
|
31
|
-
rescue Errno::ENOENT
|
32
|
-
puts 'YAML configuration file couldn\'t be found. Using defaults.'
|
33
|
-
return
|
34
|
-
rescue Psych::SyntaxError
|
35
|
-
puts 'YAML configuration file contains invalid syntax. Using defaults.'
|
36
|
-
return
|
37
|
-
end
|
38
|
-
|
39
|
-
configure(config)
|
40
|
-
end
|
41
|
-
|
42
|
-
def self.config
|
43
|
-
@config
|
44
|
-
end
|
45
12
|
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
module StratumnSdk
|
2
|
+
##
|
3
|
+
# Represents a Stratumn application
|
4
|
+
class Agent
|
5
|
+
include Request
|
6
|
+
extend Request
|
7
|
+
|
8
|
+
attr_accessor :url, :agent_info, :store_info
|
9
|
+
|
10
|
+
def self.load(url)
|
11
|
+
attributes = get(url)
|
12
|
+
|
13
|
+
new(url,
|
14
|
+
attributes['agentInfo'],
|
15
|
+
attributes['storeInfo'])
|
16
|
+
end
|
17
|
+
|
18
|
+
def initialize(url, agent_info, store_info)
|
19
|
+
self.url = url
|
20
|
+
self.agent_info = agent_info
|
21
|
+
self.store_info = store_info
|
22
|
+
end
|
23
|
+
|
24
|
+
def create_map(*args)
|
25
|
+
result = post(url + '/segments', json: args)
|
26
|
+
|
27
|
+
Segment.new(self, result)
|
28
|
+
end
|
29
|
+
|
30
|
+
def get_map_ids(options = {})
|
31
|
+
get(url + '/maps?' + URI.encode_www_form(options))
|
32
|
+
end
|
33
|
+
|
34
|
+
def find_segments(options = {})
|
35
|
+
result = get(url + '/segments?' + URI.encode_www_form(options))
|
36
|
+
|
37
|
+
result.map do |link|
|
38
|
+
Segment.new(self, link)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def get_segment(link_hash)
|
43
|
+
result = get(url + '/segments/' + link_hash)
|
44
|
+
|
45
|
+
Segment.new(self, result)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
module StratumnSdk
|
2
|
+
##
|
3
|
+
# Represents a link in a Stratumn application
|
4
|
+
class Segment
|
5
|
+
include Request
|
6
|
+
include Helper
|
7
|
+
|
8
|
+
attr_accessor :agent, :meta, :state, :link, :link_hash
|
9
|
+
|
10
|
+
def initialize(agent, obj)
|
11
|
+
self.agent = agent
|
12
|
+
|
13
|
+
self.link = obj['link']
|
14
|
+
self.meta = link['meta']
|
15
|
+
self.state = link['state']
|
16
|
+
self.link_hash = obj['meta']['linkHash']
|
17
|
+
|
18
|
+
agent.agent_info['actions'].each do |(method, _)|
|
19
|
+
add_transition_method(method)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def previous
|
24
|
+
agent.get_segment(meta['prevLinkHash']) if meta['prevLinkHash']
|
25
|
+
end
|
26
|
+
|
27
|
+
def find_segments(options = {})
|
28
|
+
agent.find_segments(options)
|
29
|
+
end
|
30
|
+
|
31
|
+
def load
|
32
|
+
agent.get_segment(link_hash)
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.from(segment)
|
36
|
+
agent = Agent.load(segment['meta']['agentUrl'])
|
37
|
+
|
38
|
+
new(agent, segment)
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
def add_transition_method(method)
|
44
|
+
define_singleton_method(method) do |*args|
|
45
|
+
url = "#{agent.url}/segments/#{link_hash}/#{method}"
|
46
|
+
|
47
|
+
result = post(url, json: args)
|
48
|
+
|
49
|
+
self.class.new(agent, result)
|
50
|
+
end
|
51
|
+
|
52
|
+
singleton_class.send(:alias_method, underscore(method), method)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
data/lib/stratumn_sdk/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: stratumn_sdk
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Stratumn Team
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-11-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -127,10 +127,10 @@ files:
|
|
127
127
|
- bin/console
|
128
128
|
- bin/setup
|
129
129
|
- lib/stratumn_sdk.rb
|
130
|
-
- lib/stratumn_sdk/
|
130
|
+
- lib/stratumn_sdk/agent.rb
|
131
131
|
- lib/stratumn_sdk/helper.rb
|
132
|
-
- lib/stratumn_sdk/link.rb
|
133
132
|
- lib/stratumn_sdk/request.rb
|
133
|
+
- lib/stratumn_sdk/segment.rb
|
134
134
|
- lib/stratumn_sdk/version.rb
|
135
135
|
- stratumn_sdk.gemspec
|
136
136
|
homepage: https://github.com/stratumn/stratumn-sdk-ruby
|
@@ -153,7 +153,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
153
153
|
version: '0'
|
154
154
|
requirements: []
|
155
155
|
rubyforge_project:
|
156
|
-
rubygems_version: 2.
|
156
|
+
rubygems_version: 2.5.2
|
157
157
|
signing_key:
|
158
158
|
specification_version: 4
|
159
159
|
summary: Interact with your Stratumn agent from your ruby app
|
@@ -1,62 +0,0 @@
|
|
1
|
-
module StratumnSdk
|
2
|
-
##
|
3
|
-
# Represents a Stratumn application
|
4
|
-
class Application
|
5
|
-
include Request
|
6
|
-
extend Request
|
7
|
-
|
8
|
-
attr_accessor :url, :id, :name, :agent_info
|
9
|
-
|
10
|
-
def self.load(application_name, application_location = nil)
|
11
|
-
url = application_location ||
|
12
|
-
StratumnSdk.config[:application_url].gsub('%s', application_name)
|
13
|
-
attributes = get(url)
|
14
|
-
|
15
|
-
new(url,
|
16
|
-
attributes['id'],
|
17
|
-
attributes['name'],
|
18
|
-
attributes['agentInfo'])
|
19
|
-
end
|
20
|
-
|
21
|
-
def initialize(url, id, name, agent_info)
|
22
|
-
self.url = url
|
23
|
-
self.id = id
|
24
|
-
self.name = name
|
25
|
-
self.agent_info = agent_info
|
26
|
-
end
|
27
|
-
|
28
|
-
def create_map(*args)
|
29
|
-
result = post(url + '/maps', json: args)
|
30
|
-
|
31
|
-
raise result['meta']['errorMessage'] if result['meta']['errorMessage']
|
32
|
-
|
33
|
-
Link.new(self, result)
|
34
|
-
end
|
35
|
-
|
36
|
-
def get_link(link_hash)
|
37
|
-
result = get(url + '/links/' + link_hash)
|
38
|
-
|
39
|
-
Link.new(self, result)
|
40
|
-
end
|
41
|
-
|
42
|
-
def get_map(map_id, tags = [])
|
43
|
-
query = tags.empty? ? '' : "?tags=#{tags.join('&tags=')}"
|
44
|
-
|
45
|
-
result = get(url + '/maps/' + map_id + query)
|
46
|
-
|
47
|
-
result.map do |link|
|
48
|
-
Link.new(self, link)
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
52
|
-
def get_branches(link_hash, tags = [])
|
53
|
-
query = tags.empty? ? '' : "?tags=#{tags.join('&tags=')}"
|
54
|
-
|
55
|
-
result = get(url + '/branches/' + link_hash + query)
|
56
|
-
|
57
|
-
result.map do |link|
|
58
|
-
Link.new(self, link)
|
59
|
-
end
|
60
|
-
end
|
61
|
-
end
|
62
|
-
end
|
data/lib/stratumn_sdk/link.rb
DELETED
@@ -1,60 +0,0 @@
|
|
1
|
-
module StratumnSdk
|
2
|
-
##
|
3
|
-
# Represents a link in a Stratumn application
|
4
|
-
class Link
|
5
|
-
include Request
|
6
|
-
include Helper
|
7
|
-
|
8
|
-
attr_accessor :application, :meta, :state, :link, :link_hash
|
9
|
-
|
10
|
-
def initialize(application, obj)
|
11
|
-
self.application = application
|
12
|
-
|
13
|
-
self.link = obj['link']
|
14
|
-
self.meta = link['meta']
|
15
|
-
self.state = link['state']
|
16
|
-
self.link_hash = obj['meta']['linkHash']
|
17
|
-
|
18
|
-
application.agent_info['functions'].each do |(method, _)|
|
19
|
-
add_transition_method(method)
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
def previous
|
24
|
-
application.get_link(meta['prevLinkHash']) if meta['prevLinkHash']
|
25
|
-
end
|
26
|
-
|
27
|
-
def get_branches(tags)
|
28
|
-
application.get_branches(link_hash, tags)
|
29
|
-
end
|
30
|
-
|
31
|
-
def load
|
32
|
-
application.get_link(link_hash)
|
33
|
-
end
|
34
|
-
|
35
|
-
def self.load(segment)
|
36
|
-
meta = segment['meta']
|
37
|
-
|
38
|
-
application = Application.load(
|
39
|
-
meta['application'],
|
40
|
-
meta['applicationLocation']
|
41
|
-
)
|
42
|
-
|
43
|
-
application.get_link(meta['linkHash'])
|
44
|
-
end
|
45
|
-
|
46
|
-
private
|
47
|
-
|
48
|
-
def add_transition_method(method)
|
49
|
-
define_singleton_method(method) do |*args|
|
50
|
-
url = "#{application.url}/links/#{link_hash}/#{method}"
|
51
|
-
|
52
|
-
result = post(url, json: args)
|
53
|
-
|
54
|
-
self.class.new(application, result)
|
55
|
-
end
|
56
|
-
|
57
|
-
singleton_class.send(:alias_method, underscore(method), method)
|
58
|
-
end
|
59
|
-
end
|
60
|
-
end
|