Module: Rapidash::HTTPClient

Defined in:
lib/rapidash/http_client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#connectionObject



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rapidash/http_client.rb', line 20

def connection
  raise ConfigurationError.new "Site is required" unless site

  @connection ||= Faraday.new(site) do |builder|
    if  || password
      builder.use Faraday::Request::BasicAuthentication, , password
    end

    connection_builder.call(builder)
  end
end

#loginObject

Returns the value of attribute login.



7
8
9
# File 'lib/rapidash/http_client.rb', line 7

def 
  @login
end

#passwordObject

Returns the value of attribute password.



7
8
9
# File 'lib/rapidash/http_client.rb', line 7

def password
  @password
end

#request_default_optionsObject

Returns the value of attribute request_default_options.



7
8
9
# File 'lib/rapidash/http_client.rb', line 7

def request_default_options
  @request_default_options
end

Instance Method Details

#initialize(options = {}) ⇒ Object

Provide login and password fields for basic HTTP authentication Provide request_default_options field for default options to be provided on each http request To set a default User-agent which identifies your application, provide { request_default_options: { header: { user_agent: ‘My great new App V.0.1 Contact: [email protected]’} } }



14
15
16
17
18
# File 'lib/rapidash/http_client.rb', line 14

def initialize(options = {})
  [:login, :password, :request_default_options].each do |key|
    self.send("#{key.to_s}=".to_sym, options[key])
  end
end

#request(verb, url, options = {}) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/rapidash/http_client.rb', line 32

def request(verb, url, options = {})
  options.merge!(self.request_default_options) if self.request_default_options
  url = connection.build_url(normalize_url(url), options[:params]).to_s
  response = connection.run_request(verb, url, options[:body], options[:header])

  response.body
end