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.



6
7
8
9
10
11
# File 'lib/king_soa/service.rb', line 6

def initialize(opts)
  self.name = opts[:name].to_sym
  self.url = opts[:url] if opts[:url]
  self.queue = opts[:queue] if opts[:queue]
  self.auth = opts[:auth] if opts[:auth]
end

Instance Attribute Details

#authObject

endpoint url



4
5
6
# File 'lib/king_soa/service.rb', line 4

def auth
  @auth
end

#debugObject

endpoint url



4
5
6
# File 'lib/king_soa/service.rb', line 4

def debug
  @debug
end

#nameObject

endpoint url



4
5
6
# File 'lib/king_soa/service.rb', line 4

def name
  @name
end

#queueObject

endpoint url



4
5
6
# File 'lib/king_soa/service.rb', line 4

def queue
  @queue
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



29
30
31
32
# File 'lib/king_soa/service.rb', line 29

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



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/king_soa/service.rb', line 15

def call_remote(*args)
  request = Typhoeus::Easy.new
  set_request_opts(request, args)
  resp_code = request.perform
  case resp_code
  when 200
    return self.decode(request.response_body)["result"]
  else
    return self.decode(request.response_body)["error"]
  end
end

#decode(string) ⇒ Object



109
110
111
112
113
114
115
# File 'lib/king_soa/service.rb', line 109

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

#encode(string) ⇒ Object



105
106
107
# File 'lib/king_soa/service.rb', line 105

def encode(string)
  string.to_json
end

#local_classObject

The local class, if found



52
53
54
55
56
57
58
# File 'lib/king_soa/service.rb', line 52

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

#local_class_nameObject

Return the classname infered from the camelized service name. A service named: save_attachment => class SaveAttachment



62
63
64
# File 'lib/king_soa/service.rb', line 62

def local_class_name
  self.name.to_s.camelize
end

#params(payload) ⇒ Object

The params for each soa request consist of following values:

name => the name of the method to call
args => the arguments for the soa class method
auth => an authentication key. something like a api key or pass. To make

it really secure you MUST use https or do not expose your soa endpoints

Parameter

payload<Hash|Array|String>

will be json encoded

Returns

<HashString=>String>

params added to the POST body



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

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 recieves. Those are later json

encoded for remote or queued methods



41
42
43
44
45
46
47
48
49
# File 'lib/king_soa/service.rb', line 41

def perform(*args)
  if queue
    add_to_queue(*args)
    return nil
  else
    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[]>

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



70
71
72
73
74
75
76
77
78
# File 'lib/king_soa/service.rb', line 70

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

#urlObject

Url receiving the request TODO. if not present try to grab from endpoint



82
83
84
# File 'lib/king_soa/service.rb', line 82

def url
  @url
end

#url=(url) ⇒ Object



85
86
87
# File 'lib/king_soa/service.rb', line 85

def url=(url)
  @url = "#{url}/soa"
end