Class: Servicy::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/client.rb

Defined Under Namespace

Classes: ServiceNotFoundError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(transport = nil) ⇒ Client

Create a new Client instance



14
15
16
# File 'lib/client.rb', line 14

def initialize(transport=nil)
  @transport = transport || Servicy.client.transport
end

Instance Attribute Details

#transportObject (readonly)

Returns the value of attribute transport.



11
12
13
# File 'lib/client.rb', line 11

def transport
  @transport
end

Class Method Details

.create_api_for(klass) ⇒ Object

Creats an api from the provided class’ public methods and returns it. You can then use said API to build a service that exports it over the wire how you would like.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/client.rb', line 60

def self.create_api_for(klass)
  # Get all the public methods, and for each one get their arguments. Parse
  # around the method to find documentation if any is available.
  file_contents = {}
  methods = klass.public_instance_methods - Object.instance_methods - Module.instance_methods
  all_methods = Contraction.methods_for klass
  data = all_methods.inject({}) do |h, (type, methods)|
    h[type] = methods.map do |method|
      file, line = read_file_for_method(klass, method, type)
      # If `file` is nil, there is no source file. Either it's an IRB
      # session, or a standard library class.
      next file if file.nil?

      # Walking backwards through the text, we look for RDoc comments
      lines = file[0...line-1]
      doc = []
      lines.reverse.each do |line|
        line.strip!
        break unless line.start_with? '#'
        doc << line
      end
      doc.reverse!
      next if doc.empty?

      contract = Contraction::Parser.parse(doc, klass, method, type)

      { method: method.to_s, docs: doc.join("\n"), contract: contract }
    end.compact
    h
  end

  API.create klass, data
end

Instance Method Details

#connected?Boolean

Return weather or not you are connected to a remote servicy server

Returns:

  • (Boolean)


19
20
21
22
# File 'lib/client.rb', line 19

def connected?
  @transport.send(Servicy::Transport::Message.query(name: 'com.servicy.not.a.real.service')) rescue return false
  true
end

#find_all_services(args) ⇒ Object

Find all services matching a query



48
49
50
# File 'lib/client.rb', line 48

def find_all_services(args)
  @transport.send(Servicy::Transport::Message.query(args)).services.map { |s| Servicy::Service.new s }
end

#find_service(args) ⇒ Object

Find a single service matching a query. This will cause the server to use whatever configured load-balancing mechanism is configured for it.



40
41
42
43
44
45
# File 'lib/client.rb', line 40

def find_service(args)
  result = @transport.send(Servicy::Transport::Message.query_one(args))
  services = result && result.services
  service = services && services.first
  service && Servicy::Service.new(service)
end

#register_service(args) ⇒ Object

Register a service with the service discovery portal found at the other end of @transport



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/client.rb', line 26

def register_service(args)
  if !args.is_a?(Hash) && args.ancestors.include?(Servicy::API)
    args = args.search_query
  end

  if args.is_a?(Hash)
    @transport.send(Servicy::Transport::Message.registration(Servicy::Service.new(args)))
  else
    raise ArgumentError.new("Don't know how to register type, #{args.inspect}")
  end
end

#server_statsObject

Send a message requesting server statistics



53
54
55
# File 'lib/client.rb', line 53

def server_stats
  @transport.send(Servicy::Transport::Message.statistics)
end