Class: IActionable::Connection

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(settings) ⇒ Connection

Returns a new instance of Connection.



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/iactionable/connection.rb', line 45

def initialize(settings)
  @settings = settings

  @connection = Faraday.new api_url(@settings.version) do |builder|
    builder.use FaradayMiddleware::ParseJson, content_type: 'application/json'
    builder.use Faraday::Response::RaiseError
    builder.use Faraday::Adapter::NetHttp
  end

  @request = nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symbol, *args) ⇒ Object (private)



96
97
98
99
100
# File 'lib/iactionable/connection.rb', line 96

def method_missing(symbol, *args)
  @request.send(symbol, *args) and self
rescue NoMethodError => e
  raise e
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



40
41
42
# File 'lib/iactionable/connection.rb', line 40

def connection
  @connection
end

#requestObject (readonly)

Returns the value of attribute request.



42
43
44
# File 'lib/iactionable/connection.rb', line 42

def request
  @request
end

#responseObject

Returns the value of attribute response.



43
44
45
# File 'lib/iactionable/connection.rb', line 43

def response
  @response
end

#settingsObject (readonly)

Returns the value of attribute settings.



41
42
43
# File 'lib/iactionable/connection.rb', line 41

def settings
  @settings
end

Instance Method Details

#get(path = nil, query_params = {}) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/iactionable/connection.rb', line 61

def get(path=nil, query_params={})
  @request ||= Request.new(@settings)
  @request.to(path).with_params(query_params)
  @response = @connection.get do |req|
    req.headers.merge! @request.headers
    req.url @request.path, @request.params
  end
  @response.body
rescue Faraday::Error::ClientError => e
  handle_client_error e
rescue Faraday::Error::TimeoutError, Timeout::Error => e
  raise e
ensure
  @request = nil
end

#post(path = nil, query_params = {}, body_params = {}) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/iactionable/connection.rb', line 77

def post(path=nil, query_params={}, body_params={})
  @request ||= Request.new(@settings)
  @request.to(path).with_params(query_params)
  @response = @connection.post do |req|
    req.headers.merge! @request.headers
    req.url @request.path, @request.params
    req.body = @request.body unless @request.body.empty?
  end
  @response.body
rescue Faraday::Error::ClientError => e
  handle_client_error e
rescue Faraday::Error::TimeoutError, Timeout::Error => e
  raise e
ensure
  @request = nil
end