stratumn_sdk 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 96a0f25a4a3767eeb3f3c54518300534355fda7a
4
- data.tar.gz: d6e7507e0b4c4773988e0f593d72e3abc220b5c8
3
+ metadata.gz: 91ec9124a7287a16c372733fc37e220f4d52f31b
4
+ data.tar.gz: 5e04851a809d1444bd2a2e29efd601f0a5e16169
5
5
  SHA512:
6
- metadata.gz: ec904a1adfe462f1eaf95b3b544239ba4fc842c477f63f6e73f7f3389640fdb5712ff9178a4bcbb903a45019c34daf7f6f08fc2647611eb76f01bff09ae3136e
7
- data.tar.gz: b3ff43ad083c509e8026967149466663fae9b08ed6dd4fbd149d965cf79005feaeeed99aba920dfeb7bd28658f4fc844ae0533a5effd9e3ab75f8b4532cd7ba4
6
+ metadata.gz: afd69b95dd5c685aafa3e456323fa364e31fcc7ab48027238c9b6f3b85597ebebf8b5666da8d72a8492f64d9185516afeae723228e66907a639f38920ca2673b
7
+ data.tar.gz: f6826334b8421d829278f3f343b5df59142a49544f0698bf6e0be455deb8123082ebe80be5097459dde1524e8ac419f3ead962a42d8e176c11ee21bf8b3770bc
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- stratumn_sdk (0.2.0)
4
+ stratumn_sdk (1.0.0)
5
5
  http (~> 2.0)
6
6
 
7
7
  GEM
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
  [![build status](https://travis-ci.org/stratumn/stratumn-sdk-ruby.svg?branch=master)](https://travis-ci.org/stratumn/stratumn-sdk-ruby.svg?branch=master)
4
4
  [![Gem Version](https://badge.fury.io/rb/stratumn_sdk.svg)](https://badge.fury.io/rb/stratumn_sdk)
5
5
 
6
- Interact with your Stratumn agent from your ruby application
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 application's Gemfile:
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
- application = StratumnSdk::Application.load('quickstart')
29
+ agent = StratumnSdk::Agent.load('quickstart')
30
30
 
31
- link = application.create_map('My message map')
31
+ segment = agent.create_map('My message map')
32
32
 
33
- link = link.addMessage('Hello, World')
33
+ segment = segment.add_essage('Hello, World')
34
34
 
35
- puts link.meta
36
- puts link.state
35
+ puts segment.meta
36
+ puts segment.state
37
37
  ```
38
38
 
39
39
  ## Reference
40
40
 
41
- #### StratumnSdk::Application.load(name)
41
+ #### StratumnSdk::Agent.load(url)
42
42
 
43
- Returns an instance of StratumnSdk::Application.
43
+ Returns an instance of StratumnSdk::Agent.
44
44
 
45
45
  ```ruby
46
- application = StratumnSdk::Application.load('quickstart')
47
- puts application.id
46
+ agent = StratumnSdk::Agent.load('http://localhost:3000')
47
+ puts agent.agent_info
48
48
  ```
49
49
 
50
- #### StratumnSdk::Application#create_map(*args)
50
+ #### StratumnSdk::Agent#create_map(*args)
51
51
 
52
- Creates a new map in the application.
52
+ Creates a new map in the agent.
53
53
 
54
54
  ```ruby
55
- application = StratumnSdk::Application.load('quickstart')
56
- link = application.create_map('My message map')
55
+ agent = StratumnSdk::Agent.load('http://localhost:3000')
56
+ segment = agent.create_map('My message map')
57
57
  ```
58
58
 
59
- #### StratumnSdk::get_link(hash)
59
+ #### StratumnSdk::Agent.get_segment(hash)
60
60
 
61
- Returns an existing link.
61
+ Returns an existing segment.
62
62
 
63
63
  ```ruby
64
- application = StratumnSdk::Application.load('quickstart')
65
- link = application.get_link('aee5427')
66
- puts link.link_hash
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::get_map(map_id, tags = [])
69
+ #### StratumnSdk::Agent.find_segments(options = {})
70
70
 
71
- Returns the links in a map, optionally filtered by tags.
71
+ Returns existing segments.
72
72
 
73
- ```ruby
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
- Returns he links whose previous hashes are the given hash, optionally filters by tags.
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
- application = StratumnSdk::Application.load('quickstart')
84
- links = application.get_branches('aee5427', ['tag1', 'tag2'])
82
+ agent = StratumnSdk::Agent.load('http://localhost:3000')
83
+ segments = agent.find_segments(tags: ['tag1', 'tag2'])
85
84
  ```
86
85
 
87
- #### StratumnSdk::Link#previous
86
+ #### StratumnSdk::Segment.from
88
87
 
89
- Returns the previous link of a link (its parent).
88
+ Returns segment from a given raw object.
90
89
 
91
90
  ```ruby
92
- application = StratumnSdk::Application.load('quickstart')
93
- link = application.get_link('aee5427')
94
- previous = link.previous
91
+ segment = StratumnSdk::Agent.from(raw_segment)
92
+ puts segment.agent
93
+ puts segment.link_hash
95
94
  ```
96
95
 
97
- #### StratumnSdk::Link#load
96
+ #### StratumnSdk::Segment#previous
98
97
 
99
- Loads a full link. Can be useful when you only have the meta data of links.
98
+ Returns the previous segment of a segment (its parent).
100
99
 
101
100
  ```ruby
102
- application = StratumnSdk::Application.load('quickstart')
103
- links = application.get_branches('aee5427')
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::Link#get_branches(tags)
106
+ #### StratumnSdk::Segment#load
109
107
 
110
- Returns the links whose previous hashes are the hash of the link, optionally filters by tags.
108
+ Loads a full segment. Can be useful when you only have the meta data of links.
111
109
 
112
110
  ```ruby
113
- application = StratumnSdk::Application.load('quickstart')
114
- link = application.get_link('aee5427')
115
- link.get_branches(['tag1'])
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::Link#transition_function(*args)
117
+ #### StratumnSdk::Segment#transition_function(*args)
119
118
 
120
- Executes a transition function and returns the new link.
119
+ Executes a transition function and returns the new segment.
121
120
 
122
121
  ```ruby
123
- application = StratumnSdk::Application.load('quickstart')
124
- link = application.get_link('aee5427')
125
- new_link = link.addMessage('Hello, World!')
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
- new_link = link.add_message('Hello, World!')
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)
@@ -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/application'
5
- require 'stratumn_sdk/link'
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
@@ -1,3 +1,3 @@
1
1
  module StratumnSdk
2
- VERSION = '0.2.0'.freeze
2
+ VERSION = '1.0.0'.freeze
3
3
  end
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.2.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-08-16 00:00:00.000000000 Z
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/application.rb
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.4.6
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
@@ -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