Class: NameDotComApi::Connection

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

Constant Summary collapse

HTTP_FORMAT_HEADER_NAMES =
{
  :get    => 'Accept',
  :post   => 'Content-Type'
}
JSON_MIME_TYPE =
'text/json; charset=utf-8'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(test_mode = false) ⇒ Connection

Returns a new instance of Connection.



29
30
31
32
33
# File 'lib/name_dot_com_api/connection.rb', line 29

def initialize(test_mode = false)
  @headers ||= {}
  @cookies ||= {}
  @test_mode = test_mode
end

Instance Attribute Details

#api_tokenObject

Returns the value of attribute api_token.



35
36
37
# File 'lib/name_dot_com_api/connection.rb', line 35

def api_token
  @api_token
end

#cookiesObject

Returns the value of attribute cookies.



36
37
38
# File 'lib/name_dot_com_api/connection.rb', line 36

def cookies
  @cookies
end

#session_tokenObject

Returns the value of attribute session_token.



35
36
37
# File 'lib/name_dot_com_api/connection.rb', line 35

def session_token
  @session_token
end

#test_modeObject

Returns the value of attribute test_mode.



36
37
38
# File 'lib/name_dot_com_api/connection.rb', line 36

def test_mode
  @test_mode
end

#timeoutObject

Returns the value of attribute timeout.



36
37
38
# File 'lib/name_dot_com_api/connection.rb', line 36

def timeout
  @timeout
end

#urlObject

Returns the value of attribute url.



36
37
38
# File 'lib/name_dot_com_api/connection.rb', line 36

def url
  @url
end

#usernameObject

Returns the value of attribute username.



35
36
37
# File 'lib/name_dot_com_api/connection.rb', line 35

def username
  @username
end

Instance Method Details

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



55
# File 'lib/name_dot_com_api/connection.rb', line 55

def get(path, params = {}); request(:get, path, params); end

#httpObject

Creates new Net::HTTP instance for communication with remote service and resources.



46
47
48
49
50
51
52
53
# File 'lib/name_dot_com_api/connection.rb', line 46

def http
  http              = Net::HTTP.new(url.host, url.port)
  http.use_ssl      = url.is_a?(URI::HTTPS)
  http.verify_mode  = OpenSSL::SSL::VERIFY_NONE if http.use_ssl?
  http.read_timeout = timeout || 60 # Net::HTTP default 60 seconds
  http.set_debug_output $stderr if test_mode
  http
end

#logged_in?Boolean

Returns:

  • (Boolean)


38
# File 'lib/name_dot_com_api/connection.rb', line 38

def logged_in?; !!session_token; end

#loggerObject



58
59
60
# File 'lib/name_dot_com_api/connection.rb', line 58

def logger
  defined?(ActiveRecord) ? ActiveRecord::Base.logger : nil
end

#post(path, params = {}) ⇒ Object



56
# File 'lib/name_dot_com_api/connection.rb', line 56

def post(path, params = {}); request(:post, path, params); end