Class: KingSoa::Service

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Service

Returns a new instance of Service.



16
17
18
19
20
21
# File 'lib/king_soa/service.rb', line 16

def initialize(opts)
  self.name = opts[:name].to_sym
  [:url, :queue, :auth, :debug ].each do |opt|
    self.send("#{opt}=", opts[opt]) if opts[opt]
  end     
end

Instance Attribute Details

#authObject

name<String/Symbol>

name of the service class to call

auth<String/Int>

password for the remote service. Used by rack middleware

to authentify the callee

url<String>

Url where the service is located. If the rack middleware is

used on the receiving side, make sure to append /soa to the url so the middleware can grab the incoming call. A custom endpoint path can be set in the middleware.

queue<Boolean>

turn on queueing for this service call. The incoming

request(className+parameter) will be put onto a resque queue

debug<Boolean>

turn on verbose outpur for typhoeus request



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

def auth
  @auth
end

#debugObject

name<String/Symbol>

name of the service class to call

auth<String/Int>

password for the remote service. Used by rack middleware

to authentify the callee

url<String>

Url where the service is located. If the rack middleware is

used on the receiving side, make sure to append /soa to the url so the middleware can grab the incoming call. A custom endpoint path can be set in the middleware.

queue<Boolean>

turn on queueing for this service call. The incoming

request(className+parameter) will be put onto a resque queue

debug<Boolean>

turn on verbose outpur for typhoeus request



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

def debug
  @debug
end

#nameObject

name<String/Symbol>

name of the service class to call

auth<String/Int>

password for the remote service. Used by rack middleware

to authentify the callee

url<String>

Url where the service is located. If the rack middleware is

used on the receiving side, make sure to append /soa to the url so the middleware can grab the incoming call. A custom endpoint path can be set in the middleware.

queue<Boolean>

turn on queueing for this service call. The incoming

request(className+parameter) will be put onto a resque queue

debug<Boolean>

turn on verbose outpur for typhoeus request



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

def name
  @name
end

#queueObject

name<String/Symbol>

name of the service class to call

auth<String/Int>

password for the remote service. Used by rack middleware

to authentify the callee

url<String>

Url where the service is located. If the rack middleware is

used on the receiving side, make sure to append /soa to the url so the middleware can grab the incoming call. A custom endpoint path can be set in the middleware.

queue<Boolean>

turn on queueing for this service call. The incoming

request(className+parameter) will be put onto a resque queue

debug<Boolean>

turn on verbose outpur for typhoeus request



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

def queue
  @queue
end

#urlObject

name<String/Symbol>

name of the service class to call

auth<String/Int>

password for the remote service. Used by rack middleware

to authentify the callee

url<String>

Url where the service is located. If the rack middleware is

used on the receiving side, make sure to append /soa to the url so the middleware can grab the incoming call. A custom endpoint path can be set in the middleware.

queue<Boolean>

turn on queueing for this service call. The incoming

request(className+parameter) will be put onto a resque queue

debug<Boolean>

turn on verbose outpur for typhoeus request



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

def url
  @url
end

Instance Method Details

#add_to_queue(*args) ⇒ Object

A queued method MUST have an associated resque worker running and the soa class MUST have the @queue attribute for redis set



49
50
51
52
# File 'lib/king_soa/service.rb', line 49

def add_to_queue(*args)
  # use low level resque method since class might not be local available for Resque.enqueue
  Resque::Job.create(queue, local_class_name, *args)
end

#call_remote(*args) ⇒ Object

Call a service living somewhere in the soa universe. This is done by making a POST request to the url



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/king_soa/service.rb', line 25

def call_remote(*args)
  request = Typhoeus::Easy.new
  set_request_opts(request, args)
  resp_code = request.perform
  case resp_code
  when 200
    if request.response_header.include?('Content-Type: application/json')
      #decode incoming json .. most likely from KingSoa's rack middleware
      return self.decode(request.response_body)["result"]
    else # return plain body
      return request.response_body
    end
  else
    if request.response_header.include?('Content-Type: application/json')
      #decode incoming json .. most likely from KingSoa's rack middleware
      return self.decode(request.response_body)["error"]
    else # return plain body
      return request.response_body
    end
  end
end

#decode(string) ⇒ Object



126
127
128
129
130
131
132
# File 'lib/king_soa/service.rb', line 126

def decode(string)
  begin
    JSON.parse(string)
  rescue JSON::ParserError => e
    raise e
  end
end

#encode(string) ⇒ Object



122
123
124
# File 'lib/king_soa/service.rb', line 122

def encode(string)
  string.to_json
end

#local_classObject

The local class, if found



76
77
78
79
80
81
82
# File 'lib/king_soa/service.rb', line 76

def local_class
  begin
    local_class_name.constantize
  rescue NameError => e        # no local implementation
    false
  end
end

#local_class_nameObject

Return the class name infered from the camelized service name.

Example

save_attachment => class SaveAttachment



87
88
89
# File 'lib/king_soa/service.rb', line 87

def local_class_name
  self.name.to_s.camelize
end

#params(payload) ⇒ Object

Params for a soa request consist of following values: name => name of the soa class to call args => arguments for the soa class method -> Class.perform(args) auth => an authentication key. something like a api key or pass. To make it really secure you MUST use https or hide your soa endpoints from public web acces

Parameter

payload<Hash|Array|String>

will be json encoded

Returns

<HashString=>String>

params added to the POST body



116
117
118
119
120
# File 'lib/king_soa/service.rb', line 116

def params(payload)
  { 'name'    => name.to_s,
    'args'    => encode(payload),
    'auth'    => auth }
end

#perform(*args) ⇒ Object

Call a method:

* remote over http
* local by calling perform method on a class
* put a job onto a queue

Parameter

args

whatever arguments the service methods receives. A local service/method

gets thems as splatted params. For a remote service they are converted to json

Returns

<nil> for queued services dont answer <mixed> Whatever the method/service



65
66
67
68
69
70
71
72
73
# File 'lib/king_soa/service.rb', line 65

def perform(*args)
  if queue
    add_to_queue(*args)
    return nil
  else # call the local class if present, else got remote
    result = local_class ? local_class.send(:perform, *args) : call_remote(*args)
    return result
  end
end

#set_request_opts(req, args) ⇒ Object

Set options for the typhoeus curl request

Parameter

req<Typhoeus::Easy>

request object

args<Array[]>

arguments for the soa method, added to post body json encoded



95
96
97
98
99
100
101
102
103
# File 'lib/king_soa/service.rb', line 95

def set_request_opts(req, args)
  req.url         = url
  req.method      = :post
  req.timeout     = 10000 # milliseconds
  req.params      = params(args)
  req.user_agent  = 'KingSoa'
  req.follow_location = true
  req.verbose     = 1 if debug
end