Sha256: c62a96ceb25c60acf5438bebd18ea72197b00ce8723b3658f1a84c0ddb841e1f

Contents?: true

Size: 1.88 KB

Versions: 1

Compression:

Stored size: 1.88 KB

Contents

# Copyright 2017 Stratumn SAS. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

module AgentClient
  ##
  # Represents a link in a Stratumn application
  class Segment
    include Request
    include Helper

    attr_accessor :process, :meta, :state, :link, :link_hash, :references

    def initialize(process, obj)
      self.process = process

      self.link = obj['link']
      self.meta = link['meta']
      self.state = link['state']
      self.link_hash = obj['meta']['linkHash']
      self.references = meta['refs']

      process.info['actions'].each do |(method, _)|
        add_transition_method(method)
      end
    end

    def previous
      process.get_segment(meta['prevLinkHash']) if meta['prevLinkHash']
    end

    def find_segments(options = {})
      process.find_segments(options)
    end

    def load
      process.get_segment(link_hash)
    end

    def self.from(segment)
      agent = Agent.load(segment['meta']['agentUrl'])
      process = agent.get segment['link']['meta']['process']

      new(process, segment)
    end

    private

    def add_transition_method(method)
      define_singleton_method(method) do |*args|
        url = "#{process.process_url}/segments/#{link_hash}/#{method}"

        result = post(url, json: args)

        self.class.new(process, result)
      end

      singleton_class.send(:alias_method, underscore(method), method)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
indigoframework_agent_client-0.2.0 lib/agent_client/segment.rb