Class: FsCommunicator

Inherits:
Object show all
Includes:
FamilytreeV2, IdentityV2, RubyFsStack
Defined in:
lib/ruby-fs-stack/identity/communicator.rb,
lib/ruby-fs-stack/fs_communicator.rb,
lib/ruby-fs-stack/familytree/communicator.rb

Overview

Mix in the module so that the fs_familytree_v1 can be called

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from FamilytreeV2

#familytree_v2

Methods included from IdentityV2

#identity_v1, #identity_v2

Methods included from RubyFsStack

version

Constructor Details

#initialize(options = {}) ⇒ FsCommunicator

Params

options - a hash with the following options

  • :domain - Defaults to “www.dev.usys.org” (the Reference System)

  • :key - Your developer key. Defaults to ”

  • :user_agent - Your User-Agent string. This should be overridden by your app. It defaults to “FsCommunicator/0.1 (Ruby)”

  • :session - A session string if you already have one.

  • :handle_throttling - (true|false) Defaults to false. If true, when a 503 response is received from the API, it will sleep 15 seconds, and try again until successful. You will likely want this turned off when running this library from Rails or any other system that is single-threaded so as to not sleep the entire process until throttling is successful.

  • :logger - (optional) if a logger is assigned to the communicator, all get requests and responses will be logged. The request and response (“GET /path” and “200 OK”) will be logged at the info level. Headers and request/response bodies will be logged at the debug level.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ruby-fs-stack/fs_communicator.rb', line 24

def initialize(options = {})
  # merge default options with options hash
  o = {
    :domain => 'http://www.dev.usys.org',
    :key => '',
    :user_agent => 'FsCommunicator/0.1 (Ruby)', # should be overridden by options user_agent
    :session => nil,
    :handle_throttling => false,
    :logger => nil,
    :timeout => nil
  }.merge(options)
  @domain = o[:domain]
  @key = o[:key]
  @user_agent = o[:user_agent]
  @session = o[:session]
  @handle_throttling = o[:handle_throttling]
  @logger = o[:logger]
  @timeout = o[:timeout]
end

Instance Attribute Details

#domainObject

Returns the value of attribute domain.



6
7
8
# File 'lib/ruby-fs-stack/fs_communicator.rb', line 6

def domain
  @domain
end

#handle_throttlingObject

Returns the value of attribute handle_throttling.



6
7
8
# File 'lib/ruby-fs-stack/fs_communicator.rb', line 6

def handle_throttling
  @handle_throttling
end

#keyObject

Returns the value of attribute key.



6
7
8
# File 'lib/ruby-fs-stack/fs_communicator.rb', line 6

def key
  @key
end

#loggerObject

Returns the value of attribute logger.



6
7
8
# File 'lib/ruby-fs-stack/fs_communicator.rb', line 6

def logger
  @logger
end

#sessionObject

Returns the value of attribute session.



6
7
8
# File 'lib/ruby-fs-stack/fs_communicator.rb', line 6

def session
  @session
end

#timeoutObject

Returns the value of attribute timeout.



6
7
8
# File 'lib/ruby-fs-stack/fs_communicator.rb', line 6

def timeout
  @timeout
end

#user_agentObject

Returns the value of attribute user_agent.



6
7
8
# File 'lib/ruby-fs-stack/fs_communicator.rb', line 6

def user_agent
  @user_agent
end

Instance Method Details

#get(url, credentials = {}) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/ruby-fs-stack/fs_communicator.rb', line 71

def get(url,credentials = {})
  uri = URI.parse(self.domain+url)
  full_url = set_extra_params(uri,credentials)
  request = Net::HTTP::Get.new(full_url)
  request['User-Agent'] = self.user_agent
  if credentials[:username] && credentials[:password]
    request.basic_auth credentials[:username], credentials[:password]
  end
  
  http = Net::HTTP.new(uri.host, uri.port)
  set_ssl(http) if uri.scheme == 'https'
  http.read_timeout = @timeout unless @timeout.nil?
  
  log_request('GET',full_url,request) if logger
  res = http.start do |ht|
    ht.request(request)
  end
  log_response(res) if logger
  
  if res.code == '503' && @handle_throttling
    sleep 15
    res = get(url,credentials)
  elsif res.code != '200'
    raise_exception(res)
  end
  return res
end

#post(url, payload) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/ruby-fs-stack/fs_communicator.rb', line 44

def post(url,payload)
  uri = URI.parse(self.domain+url)
  full_url = set_extra_params(uri)
  request = Net::HTTP::Post.new(full_url)
  request.body = payload
  request['Content-Type'] = "application/json"
  request['User-Agent'] = self.user_agent
  
  http = Net::HTTP.new(uri.host, uri.port)
  set_ssl(http) if uri.scheme == 'https'
  http.read_timeout = @timeout unless @timeout.nil?
  
  log_request('POST',full_url,request) if logger
  res = http.start do |ht|
    ht.request(request)
  end
  log_response(res) if logger
  
  if res.code == '503' && @handle_throttling
    sleep 15
    res = post(url,payload)
  elsif res.code != '200'
    raise_exception(res)
  end
  return res
end