stratumn_agent_client 0.2.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +1 -1
- data/Gemfile.lock +1 -1
- data/README.md +66 -57
- data/agent_client.gemspec +1 -1
- data/bin/console +1 -1
- data/lib/agent_client/agent.rb +54 -0
- data/lib/agent_client/{link.rb → segment.rb} +14 -19
- data/lib/agent_client/version.rb +1 -1
- data/lib/agent_client.rb +2 -2
- metadata +4 -4
- data/lib/agent_client/application.rb +0 -66
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5dc1d9233eb9b69631fa998886084001f7115c6b
|
4
|
+
data.tar.gz: 8333c017990136820863e091cdd149ad266089e2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 35b965f742d54ef11e3d8bbc271b614a5bcbd6678be280a228e8531dec875d89c4e49945efff7380f46f0423fc29a794a7d7a2060f5686e953a31b4d355e3242
|
7
|
+
data.tar.gz: bc5aae0dacdd99d19cd31ef34bae849568fcd5f49d5fb1e5acf378e0ba860e168a4bc3bcfe0c8a8637c3360c7fcc7e00c53bd44cffc7e7150019b8292aad67cf
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,15 +1,15 @@
|
|
1
|
-
# Agent client for Ruby
|
1
|
+
# Agent client for Ruby [ALPHA - incompatible with production]
|
2
2
|
|
3
3
|
[![build status](https://travis-ci.org/stratumn/agent-client-ruby.svg?branch=master)](https://travis-ci.org/stratumn/agent-client-ruby.svg?branch=master)
|
4
|
-
[![Gem Version](https://badge.fury.io/rb/agent_client.svg)](https://badge.fury.io/rb/
|
4
|
+
[![Gem Version](https://badge.fury.io/rb/agent_client.svg)](https://badge.fury.io/rb/stratumn_agent_client)
|
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/agent-client-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 'agent-client'
|
@@ -26,110 +26,119 @@ Or install it yourself as:
|
|
26
26
|
## Quickstart
|
27
27
|
|
28
28
|
```ruby
|
29
|
-
|
29
|
+
agent = AgentClient::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
|
-
#### AgentClient::
|
41
|
+
#### AgentClient::Agent.load(url)
|
42
42
|
|
43
|
-
Returns an instance of AgentClient::
|
43
|
+
Returns an instance of AgentClient::Agent.
|
44
44
|
|
45
45
|
```ruby
|
46
|
-
|
47
|
-
puts
|
46
|
+
agent = AgentClient::Agent.load('http://localhost:3000')
|
47
|
+
puts agent.agent_info
|
48
48
|
```
|
49
49
|
|
50
|
-
#### AgentClient::
|
50
|
+
#### AgentClient::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 = AgentClient::Agent.load('http://localhost:3000')
|
56
|
+
segment = agent.create_map('My message map')
|
57
57
|
```
|
58
58
|
|
59
|
-
#### AgentClient::
|
59
|
+
#### AgentClient::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 = AgentClient::Agent.load('http://localhost:3000')
|
65
|
+
segment = agent.get_segment('aee5427')
|
66
|
+
puts segment.link_hash
|
67
67
|
```
|
68
68
|
|
69
|
-
#### AgentClient::
|
69
|
+
#### AgentClient::Agent.find_segments(options = {})
|
70
70
|
|
71
|
-
Returns
|
71
|
+
Returns existing segments.
|
72
72
|
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
Returns he links whose previous hashes are the given hash, optionally filters by tags.
|
73
|
+
Available options are:
|
74
|
+
- `offset`: offset of first returned segments
|
75
|
+
- `limit`: limit number of returned segments
|
76
|
+
- `mapId`: return segments with specified map ID
|
77
|
+
- `prevLinkHash`: return segments with specified previous link hash
|
78
|
+
- `tags`: return segments that contains all the tags (array)
|
81
79
|
|
82
80
|
```ruby
|
83
|
-
|
84
|
-
|
81
|
+
agent = AgentClient::Agent.load('http://localhost:3000')
|
82
|
+
segments = agent.find_segments(tags: ['tag1', 'tag2'])
|
85
83
|
```
|
86
84
|
|
87
|
-
#### AgentClient::
|
85
|
+
#### AgentClient::Segment.from
|
88
86
|
|
89
|
-
Returns
|
87
|
+
Returns segment from a given raw object.
|
90
88
|
|
91
89
|
```ruby
|
92
|
-
|
93
|
-
|
94
|
-
|
90
|
+
segment = AgentClient::Agent.from(raw_segment)
|
91
|
+
puts segment.agent
|
92
|
+
puts segment.link_hash
|
95
93
|
```
|
96
94
|
|
97
|
-
#### AgentClient::
|
95
|
+
#### AgentClient::Segment#previous
|
98
96
|
|
99
|
-
|
97
|
+
Returns the previous segment of a segment (its parent).
|
100
98
|
|
101
99
|
```ruby
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
links.map { |link| link.load }
|
100
|
+
agent = AgentClient::Agent.load('http://localhost:3000')
|
101
|
+
segment = agent.get_segment('aee5427')
|
102
|
+
previous = segment.previous
|
106
103
|
```
|
107
104
|
|
108
|
-
#### AgentClient::
|
105
|
+
#### AgentClient::Segment#load
|
109
106
|
|
110
|
-
|
107
|
+
Loads a full segment. Can be useful when you only have the meta data of links.
|
111
108
|
|
112
109
|
```ruby
|
113
|
-
|
114
|
-
|
115
|
-
|
110
|
+
agent = AgentClient::Agent.load('http://localhost:3000')
|
111
|
+
segments = agent.find_segments
|
112
|
+
|
113
|
+
segments.map { |segment| segment.load }
|
116
114
|
```
|
117
115
|
|
118
|
-
#### AgentClient::
|
116
|
+
#### AgentClient::Segment#transition_function(*args)
|
119
117
|
|
120
|
-
Executes a transition function and returns the new
|
118
|
+
Executes a transition function and returns the new segment.
|
121
119
|
|
122
120
|
```ruby
|
123
|
-
|
124
|
-
|
125
|
-
|
121
|
+
agent = AgentClient::Agent.load('http://localhost:3000')
|
122
|
+
segment = agent.get_segment('aee5427')
|
123
|
+
new_segment = segment.addMessage('Hello, World!')
|
126
124
|
|
127
125
|
# underscore version is also available
|
128
|
-
|
126
|
+
new_segment = segment.add_message('Hello, World!')
|
129
127
|
```
|
130
128
|
|
131
129
|
## Development
|
132
130
|
|
133
131
|
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.
|
134
132
|
|
135
|
-
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)
|
133
|
+
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).
|
134
|
+
|
135
|
+
## Tests
|
136
|
+
|
137
|
+
Tests are run against a mock agent whose results are recorded by vcr.
|
138
|
+
Should you need to regenerate the cassettes or add new tests, the mock agent can be launched on port 3333.
|
139
|
+
|
140
|
+
```
|
141
|
+
$ cd spec/agent
|
142
|
+
$ npm install
|
143
|
+
$ node index.js
|
144
|
+
```
|
data/agent_client.gemspec
CHANGED
@@ -16,7 +16,7 @@ Gem::Specification.new do |spec|
|
|
16
16
|
spec.email = ['stratumn@gmail.com']
|
17
17
|
|
18
18
|
spec.summary = 'Interact with your Stratumn agent from your ruby app'
|
19
|
-
spec.homepage = 'https://github.com/stratumn/
|
19
|
+
spec.homepage = 'https://github.com/stratumn/agent-client-ruby'
|
20
20
|
spec.license = 'MPL-2.0'
|
21
21
|
|
22
22
|
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
data/bin/console
CHANGED
@@ -0,0 +1,54 @@
|
|
1
|
+
# Copyright (C) 2017 Stratumn SAS
|
2
|
+
#
|
3
|
+
# This Source Code Form is subject to the terms of the Mozilla Public
|
4
|
+
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
5
|
+
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
6
|
+
|
7
|
+
module AgentClient
|
8
|
+
##
|
9
|
+
# Represents a Stratumn agent
|
10
|
+
class Agent
|
11
|
+
include Request
|
12
|
+
extend Request
|
13
|
+
|
14
|
+
attr_accessor :url, :agent_info, :store_info
|
15
|
+
|
16
|
+
def self.load(url)
|
17
|
+
attributes = get(url)
|
18
|
+
|
19
|
+
new(url,
|
20
|
+
attributes['agentInfo'],
|
21
|
+
attributes['storeInfo'])
|
22
|
+
end
|
23
|
+
|
24
|
+
def initialize(url, agent_info, store_info)
|
25
|
+
self.url = url
|
26
|
+
self.agent_info = agent_info
|
27
|
+
self.store_info = store_info
|
28
|
+
end
|
29
|
+
|
30
|
+
def create_map(*args)
|
31
|
+
result = post(url + '/segments', json: args)
|
32
|
+
|
33
|
+
Segment.new(self, result)
|
34
|
+
end
|
35
|
+
|
36
|
+
def get_map_ids(options = {})
|
37
|
+
get(url + '/maps?' + URI.encode_www_form(options))
|
38
|
+
end
|
39
|
+
|
40
|
+
def find_segments(options = {})
|
41
|
+
result = get(url + '/segments?' + URI.encode_www_form(options))
|
42
|
+
|
43
|
+
result.map do |link|
|
44
|
+
Segment.new(self, link)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def get_segment(link_hash)
|
49
|
+
result = get(url + '/segments/' + link_hash)
|
50
|
+
|
51
|
+
Segment.new(self, result)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -7,57 +7,52 @@
|
|
7
7
|
module AgentClient
|
8
8
|
##
|
9
9
|
# Represents a link in a Stratumn application
|
10
|
-
class
|
10
|
+
class Segment
|
11
11
|
include Request
|
12
12
|
include Helper
|
13
13
|
|
14
|
-
attr_accessor :
|
14
|
+
attr_accessor :agent, :meta, :state, :link, :link_hash
|
15
15
|
|
16
|
-
def initialize(
|
17
|
-
self.
|
16
|
+
def initialize(agent, obj)
|
17
|
+
self.agent = agent
|
18
18
|
|
19
19
|
self.link = obj['link']
|
20
20
|
self.meta = link['meta']
|
21
21
|
self.state = link['state']
|
22
22
|
self.link_hash = obj['meta']['linkHash']
|
23
23
|
|
24
|
-
|
24
|
+
agent.agent_info['actions'].each do |(method, _)|
|
25
25
|
add_transition_method(method)
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
29
29
|
def previous
|
30
|
-
|
30
|
+
agent.get_segment(meta['prevLinkHash']) if meta['prevLinkHash']
|
31
31
|
end
|
32
32
|
|
33
|
-
def
|
34
|
-
|
33
|
+
def find_segments(options = {})
|
34
|
+
agent.find_segments(options)
|
35
35
|
end
|
36
36
|
|
37
37
|
def load
|
38
|
-
|
38
|
+
agent.get_segment(link_hash)
|
39
39
|
end
|
40
40
|
|
41
|
-
def self.
|
42
|
-
|
41
|
+
def self.from(segment)
|
42
|
+
agent = Agent.load(segment['meta']['agentUrl'])
|
43
43
|
|
44
|
-
|
45
|
-
meta['application'],
|
46
|
-
meta['applicationLocation']
|
47
|
-
)
|
48
|
-
|
49
|
-
application.get_link(meta['linkHash'])
|
44
|
+
new(agent, segment)
|
50
45
|
end
|
51
46
|
|
52
47
|
private
|
53
48
|
|
54
49
|
def add_transition_method(method)
|
55
50
|
define_singleton_method(method) do |*args|
|
56
|
-
url = "#{
|
51
|
+
url = "#{agent.url}/segments/#{link_hash}/#{method}"
|
57
52
|
|
58
53
|
result = post(url, json: args)
|
59
54
|
|
60
|
-
self.class.new(
|
55
|
+
self.class.new(agent, result)
|
61
56
|
end
|
62
57
|
|
63
58
|
singleton_class.send(:alias_method, underscore(method), method)
|
data/lib/agent_client/version.rb
CHANGED
data/lib/agent_client.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: stratumn_agent_client
|
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
|
@@ -128,12 +128,12 @@ files:
|
|
128
128
|
- bin/console
|
129
129
|
- bin/setup
|
130
130
|
- lib/agent_client.rb
|
131
|
-
- lib/agent_client/
|
131
|
+
- lib/agent_client/agent.rb
|
132
132
|
- lib/agent_client/helper.rb
|
133
|
-
- lib/agent_client/link.rb
|
134
133
|
- lib/agent_client/request.rb
|
134
|
+
- lib/agent_client/segment.rb
|
135
135
|
- lib/agent_client/version.rb
|
136
|
-
homepage: https://github.com/stratumn/
|
136
|
+
homepage: https://github.com/stratumn/agent-client-ruby
|
137
137
|
licenses:
|
138
138
|
- MPL-2.0
|
139
139
|
metadata: {}
|
@@ -1,66 +0,0 @@
|
|
1
|
-
# Copyright (C) 2017 Stratumn SAS
|
2
|
-
#
|
3
|
-
# This Source Code Form is subject to the terms of the Mozilla Public
|
4
|
-
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
5
|
-
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
6
|
-
|
7
|
-
module AgentClient
|
8
|
-
##
|
9
|
-
# Represents a Stratumn application
|
10
|
-
class Application
|
11
|
-
include Request
|
12
|
-
extend Request
|
13
|
-
|
14
|
-
attr_accessor :url, :id, :name, :agent_info
|
15
|
-
|
16
|
-
def self.load(application_name, application_location = nil)
|
17
|
-
url = application_location ||
|
18
|
-
AgentClient.config[:application_url].gsub('%s', application_name)
|
19
|
-
attributes = get(url)
|
20
|
-
|
21
|
-
new(url,
|
22
|
-
attributes['id'],
|
23
|
-
attributes['name'],
|
24
|
-
attributes['agentInfo'])
|
25
|
-
end
|
26
|
-
|
27
|
-
def initialize(url, id, name, agent_info)
|
28
|
-
self.url = url
|
29
|
-
self.id = id
|
30
|
-
self.name = name
|
31
|
-
self.agent_info = agent_info
|
32
|
-
end
|
33
|
-
|
34
|
-
def create_map(*args)
|
35
|
-
result = post(url + '/maps', json: args)
|
36
|
-
|
37
|
-
Link.new(self, result)
|
38
|
-
end
|
39
|
-
|
40
|
-
def get_link(link_hash)
|
41
|
-
result = get(url + '/links/' + link_hash)
|
42
|
-
|
43
|
-
Link.new(self, result)
|
44
|
-
end
|
45
|
-
|
46
|
-
def get_map(map_id, tags = [])
|
47
|
-
query = tags.empty? ? '' : "?tags=#{tags.join('&tags=')}"
|
48
|
-
|
49
|
-
result = get(url + '/maps/' + map_id + query)
|
50
|
-
|
51
|
-
result.map do |link|
|
52
|
-
Link.new(self, link)
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
|
-
def get_branches(link_hash, tags = [])
|
57
|
-
query = tags.empty? ? '' : "?tags=#{tags.join('&tags=')}"
|
58
|
-
|
59
|
-
result = get(url + '/branches/' + link_hash + query)
|
60
|
-
|
61
|
-
result.map do |link|
|
62
|
-
Link.new(self, link)
|
63
|
-
end
|
64
|
-
end
|
65
|
-
end
|
66
|
-
end
|