Class: QuickEmailVerification::HttpClient::HttpClient

Inherits:
Object
  • Object
show all
Defined in:
lib/quickemailverification/http_client.rb

Overview

Main HttpClient which is used by Api classes

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(auth = {}, options = {}) ⇒ HttpClient

Returns a new instance of HttpClient.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/quickemailverification/http_client.rb', line 16

def initialize(auth = {}, options = {})

  if auth.is_a?(String)
    auth = { :http_header => auth }
  end

  @options = {
    :base => "https://api.quickemailverification.com",
    :api_version => "v1",
    :user_agent => "quickemailverification-ruby/1.0.4 (https://github.com/quickemailverification/quickemailverification-ruby)"
  }

  @options.update(options)

  @headers = {
    "user-agent" => @options[:user_agent]
  }

  if @options.has_key?(:headers)
    @headers.update(Hash[@options[:headers].map { |k, v| [k.downcase, v] }])
    @options.delete(:headers)
  end

  @client = Faraday.new(@options[:base]) do |conn|
    conn.use(QuickEmailVerification::HttpClient::AuthHandler, auth)
    conn.use(QuickEmailVerification::HttpClient::ErrorHandler)

    conn.adapter(Faraday.default_adapter)
  end
end

Instance Attribute Details

#headersObject

Returns the value of attribute headers.



14
15
16
# File 'lib/quickemailverification/http_client.rb', line 14

def headers
  @headers
end

#optionsObject

Returns the value of attribute options.



14
15
16
# File 'lib/quickemailverification/http_client.rb', line 14

def options
  @options
end

Instance Method Details

#create_request(method, path, options) ⇒ Object

Creating a request with the given arguments

If api_version is set, appends it immediately after host



96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/quickemailverification/http_client.rb', line 96

def create_request(method, path, options)
  version = options.has_key?(:api_version) ? "/#{options[:api_version]}" : ""

  path = "#{version}#{path}"

  instance_eval <<-RUBY, __FILE__, __LINE__ + 1
    @client.#{method}(path) do |req|
      req.body = options[:body]
      req.headers.update(options[:headers])
      req.params.update(options[:query]) if options[:query]
    end
  RUBY
end

#delete(path, body = {}, options = {}) ⇒ Object



59
60
61
# File 'lib/quickemailverification/http_client.rb', line 59

def delete(path, body = {}, options = {})
  request(path, body, "delete", options)
end

#get(path, params = {}, options = {}) ⇒ Object



47
48
49
# File 'lib/quickemailverification/http_client.rb', line 47

def get(path, params = {}, options = {})
  request(path, nil, "get", options.merge({ :query => params }))
end

#get_body(env) ⇒ Object

Get response body in correct format



111
112
113
# File 'lib/quickemailverification/http_client.rb', line 111

def get_body(env)
  QuickEmailVerification::HttpClient::ResponseHandler.get_body(env)
end

#patch(path, body = {}, options = {}) ⇒ Object



55
56
57
# File 'lib/quickemailverification/http_client.rb', line 55

def patch(path, body = {}, options = {})
  request(path, body, "patch", options)
end

#post(path, body = {}, options = {}) ⇒ Object



51
52
53
# File 'lib/quickemailverification/http_client.rb', line 51

def post(path, body = {}, options = {})
  request(path, body, "post", options)
end

#put(path, body = {}, options = {}) ⇒ Object



63
64
65
# File 'lib/quickemailverification/http_client.rb', line 63

def put(path, body = {}, options = {})
  request(path, body, "put", options)
end

#request(path, body, method, options) ⇒ Object

Intermediate function which does three main things

  • Transforms the body of request into correct format

  • Creates the requests with give parameters

  • Returns response body after parsing it into correct format



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/quickemailverification/http_client.rb', line 72

def request(path, body, method, options)
  options = @options.merge(options)

  options[:headers] = options[:headers] || {}
  options[:headers] = @headers.merge(Hash[options[:headers].map { |k, v| [k.downcase, v] }])

  options[:body] = body

  if method != "get"
    options[:body] = options[:body] || {}
    options = set_body(options)
  end

  response = create_request(method, path, options)

  env = response.env
  body = get_body(env)

  QuickEmailVerification::HttpClient::Response.new(body, env.status, env.response_headers)
end

#set_body(options) ⇒ Object

Set request body in correct format



116
117
118
# File 'lib/quickemailverification/http_client.rb', line 116

def set_body(options)
  QuickEmailVerification::HttpClient::RequestHandler.set_body(options)
end