Class: Reach::REST::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/reach-ruby/rest/client.rb

Overview

A client for accessing the Reach API.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username = nil, password = nil, http_client = nil, logger = nil, user_agent_extensions = nil) ⇒ Client

Initializes the Reach Client



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/reach-ruby/rest/client.rb', line 24

def initialize(username=nil, password=nil, http_client=nil, logger=nil, user_agent_extensions=nil)
  @username = username || Reach.username
  @password = password || Reach.auth_token
  @auth_token = @password
  @auth = [@username, @password]
  @http_client = http_client || Reach.http_client || Reach::HTTP::Client.new
  @logger = logger || Reach.logger
  @user_agent_extensions = user_agent_extensions || []

  # Domains
  @api = nil
end

Instance Attribute Details

#auth_tokenObject

Returns the value of attribute auth_token.



20
21
22
# File 'lib/reach-ruby/rest/client.rb', line 20

def auth_token
  @auth_token
end

#http_clientObject

Returns the value of attribute http_client.



20
21
22
# File 'lib/reach-ruby/rest/client.rb', line 20

def http_client
  @http_client
end

#loggerObject

Returns the value of attribute logger.



20
21
22
# File 'lib/reach-ruby/rest/client.rb', line 20

def logger
  @logger
end

#passwordObject

Returns the value of attribute password.



20
21
22
# File 'lib/reach-ruby/rest/client.rb', line 20

def password
  @password
end

#user_agent_extensionsObject

Returns the value of attribute user_agent_extensions.



20
21
22
# File 'lib/reach-ruby/rest/client.rb', line 20

def user_agent_extensions
  @user_agent_extensions
end

#usernameObject

Returns the value of attribute username.



20
21
22
# File 'lib/reach-ruby/rest/client.rb', line 20

def username
  @username
end

Instance Method Details

#apiObject

Access the Api Reach Domain



106
107
108
# File 'lib/reach-ruby/rest/client.rb', line 106

def api
  @api ||= Api.new self
end

#authentixObject

Acess the Authentix api version



118
119
120
# File 'lib/reach-ruby/rest/client.rb', line 118

def authentix
  self.api.authentix
end

#build_uri(uri) ⇒ Object

Build the final request uri



99
100
101
102
# File 'lib/reach-ruby/rest/client.rb', line 99

def build_uri(uri)
  parsed_url = URI(uri)
  parsed_url.to_s
end

#messagingObject

Acess the Messaging api version



112
113
114
# File 'lib/reach-ruby/rest/client.rb', line 112

def messaging
  self.api.messaging
end

#request(host, port, method, uri, params = {}, data = {}, headers = {}, auth = nil, timeout = nil) ⇒ Object

Makes a request to the Reach API using the configured http client Authentication information is automatically added if none is provided



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/reach-ruby/rest/client.rb', line 40

def request(host, port, method, uri, params={}, data={}, headers={}, auth=nil, timeout=nil)
  auth ||= @auth

  ruby_config = RbConfig::CONFIG
  headers['User-Agent'] = "reach-ruby/#{Reach::VERSION} (#{ruby_config["host_os"]} #{ruby_config["host_cpu"]}) Ruby/#{RUBY_VERSION}"
  headers['Accept-Charset'] = 'utf-8'

  user_agent_extensions.each { |extension| headers['User-Agent'] += " #{extension}" }

  if method == 'POST' && !headers['Content-Type']
    headers['Content-Type'] = 'application/x-www-form-urlencoded'
  end

  unless headers['Accept']
    headers['Accept'] = 'application/json'
  end

  uri = build_uri(uri)

  if @logger
    @logger.debug("--BEGIN REACH API Request--")
    @logger.debug("Request Method: <#{method}>")

    headers.each do |key, value|
      unless key.downcase == 'authorization' || key.downcase == 'apikey' || key.downcase == 'apiuser'
        @logger.debug("#{key}:#{value}")
      end
    end

    url = URI(uri)
    @logger.debug("Host:#{url.host}")
    @logger.debug("Path:#{url.path}")
    @logger.debug("Query:#{url.query}")
    @logger.debug("Request Params:#{params}")
  end

  response = @http_client.request(
    host,
    port,
    method,
    uri,
    params,
    data,
    headers,
    auth,
    timeout
  )

  if @logger
    @logger.debug("Response Status Code:#{response.status_code}")
    @logger.debug("Response Headers:#{response.headers}")
    @logger.debug("--END REACH API REQUEST--")
  end

  response
end

#to_sObject

Provide a user friendly representation



124
125
126
# File 'lib/reach-ruby/rest/client.rb', line 124

def to_s
  "#<Reach::REST::Client #{@username}>"
end