Class: Conductor::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/nf-conductor/http/connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Connection

Returns a new instance of Connection.



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/nf-conductor/http/connection.rb', line 7

def initialize(args = {})
  @connection ||= Faraday.new(url: Conductor.config.service_uri) do |c|
    c.request :json
    c.response :json, content_type: /\bjson$/, parser_options: { symbolize_names: true }
    c.adapter Faraday.default_adapter
  end

  args.each do |k,v|
    @connection.headers[k] = v
  end
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



5
6
7
# File 'lib/nf-conductor/http/connection.rb', line 5

def args
  @args
end

#connectionObject (readonly)

Returns the value of attribute connection.



5
6
7
# File 'lib/nf-conductor/http/connection.rb', line 5

def connection
  @connection
end

Instance Method Details

#delete(url, args = {}) ⇒ Object



52
53
54
55
56
57
58
59
60
61
# File 'lib/nf-conductor/http/connection.rb', line 52

def delete(url, args={})
  Rails.logger.info("Conductor::Connection : DELETE #{url} with args #{args}") if Conductor.config.verbose
  connection.delete do |req|
    req.url url
    req.headers['Content-Type'] = ( args[:headers] && args[:headers]['Content-Type'] || 'application/json' )
    req.body = args[:body] if args[:body]
  end
rescue Faraday::ParsingError
  Struct.new(:status, :body).new(500, 'Conductor::Connection : Faraday failed to properly parse response.')
end

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



19
20
21
22
23
24
25
26
27
28
# File 'lib/nf-conductor/http/connection.rb', line 19

def get(url, args={})
  Rails.logger.info("Conductor::Connection : GET #{url} with args #{args}") if Conductor.config.verbose
  connection.get do |req|
    req.url url
    req.headers['Content-Type'] = ( args[:headers] && args[:headers]['Content-Type'] || 'application/json' )
    req.body = args[:body] if args[:body]
  end
rescue Faraday::ParsingError
  Struct.new(:status, :body).new(500, 'Conductor::Connection : Faraday failed to properly parse response.')
end

#post(url, args = {}) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/nf-conductor/http/connection.rb', line 30

def post(url, args={})
  Rails.logger.info("Conductor::Connection : POST #{url} with args #{args}") if Conductor.config.verbose
  connection.post do |req|
    req.url url
    req.headers['Content-Type'] = ( args[:headers] && args[:headers]['Content-Type'] || 'application/json' )
    req.body = args[:body] if args[:body]
  end
rescue Faraday::ParsingError
  Struct.new(:status, :body).new(500, 'Conductor::Connection : Faraday failed to properly parse response.')
end

#put(url, args = {}) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/nf-conductor/http/connection.rb', line 41

def put(url, args={})
  Rails.logger.info("Conductor::Connection : PUT #{url} with args #{args}") if Conductor.config.verbose
  connection.put do |req|
    req.url url
    req.headers['Content-Type'] = ( args[:headers] && args[:headers]['Content-Type'] || 'application/json' )
    req.body = args[:body] if args[:body]
  end
rescue Faraday::ParsingError
  Struct.new(:status, :body).new(500, 'Conductor::Connection : Faraday failed to properly parse response.')
end