Class: Bluevia::BaseClient

Inherits:
Object
  • Object
show all
Includes:
BlueviaLogger
Defined in:
lib/bluevia/base_client.rb

Overview

Abstract class that wraps access to REST services

Direct Known Subclasses

Advertising, Directory, Oauth, Sms

Constant Summary collapse

BASEURI =

Base URI to invoke Bluevia API. Production environment

"https://api.bluevia.com"
BASEPATH =

Base Path to invoke Bluevia API. Production environment

"/services/REST"
DEFAULT_PARAMS =

Default parameters required to invoke API

{:version => "v1", :alt => "json"}
BASEPATH_COMMERCIAL =

Endpoint for commercial (either testing or commercial apps)

""
BASEPATH_SANDBOX =

Endpoint for sandbox (either testing or commercial apps)

"_Sandbox"
PROXY =

HTTP Proxy is SDK is being used behind a firewall

nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from BlueviaLogger

#create_logger, #log_level=, #logger, #logger=

Constructor Details

#initialize(params = nil) ⇒ BaseClient

Creates basic HTTP client



71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/bluevia/base_client.rb', line 71

def initialize(params = nil)
  @@uri = URI.parse(BASEURI)

  if params.nil?
    @rest = BaseClient.create_rest_client
  elsif params.instance_of?(Hash)
    params.has_key?(:rest) and @rest = params[:rest]
    params.has_key?(:logger) and logger = params[:logger]
  else
    @rest = params
  end

  logger.debug "Initialized baseClient for service: #{self.class.name}"
end

Instance Attribute Details

#base_uriObject

Returns the value of attribute base_uri.



26
27
28
# File 'lib/bluevia/base_client.rb', line 26

def base_uri
  @base_uri
end

Class Method Details

.create_rest_client(uri = nil) ⇒ Object

“localhost:8888” #, “nube.hi.inet:8080”



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/bluevia/base_client.rb', line 48

def BaseClient.create_rest_client(uri = nil)
  @@base_uri = uri.nil? ? BASEURI : uri
  @@uri = URI.parse(@@base_uri)

  # Set proxy if required
  unless PROXY.nil?
    proxy = PROXY.split(":")
    rest = Net::HTTP::Proxy(proxy[0], proxy[1]).new(@@uri.host, @@uri.port)
  else
    rest = Net::HTTP.new(@@uri.host, @@uri.port)
  end
  # Set HTTP connection with SSL if required
  if @@uri.instance_of?(URI::HTTPS)
    rest.use_ssl = true
  end
  rest.read_timeout=(5)
  rest
end

Instance Method Details

#[]=(key, value) ⇒ Object

Define an instance variable using the default Hash syntax



103
104
105
# File 'lib/bluevia/base_client.rb', line 103

def []=(key, value)
  self.instance_variable_set(:"@#{key}", value) unless key.nil?
end

#authorized_clientObject



202
203
204
205
206
# File 'lib/bluevia/base_client.rb', line 202

def authorized_client
  @consumer ||= OAuth::Consumer.new(@consumer_key, @consumer_secret, :site => @@base_uri)
  @access_token ||= OAuth::AccessToken.new(@consumer, @token, @token_secret)
  @access_token
end

#DELETE(_path = nil, headers = nil) ⇒ Object

Send an Http::Delete to the server



192
193
194
195
196
197
198
199
200
# File 'lib/bluevia/base_client.rb', line 192

def DELETE(_path = nil, headers = nil)
  path = set_path(_path)
  path = include_params(path, DEFAULT_PARAMS)

  logger.debug "DELETE Request path: " << path

  resp = authorized_client.delete(path, get_headers(headers, false))
  return handle_response(create_response(resp))
end

#GET(_path = nil, params = nil, headers = nil, field = nil) ⇒ Object

Send an Http::Get to the server



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/bluevia/base_client.rb', line 150

def GET(_path = nil, params = nil, headers = nil, field = nil)
  path = set_path(_path)
  params = Hash.new if params.nil?
  params.merge!(DEFAULT_PARAMS) { |key,oldval,newval| oldval }

  path = include_params(path, params)

  logger.debug "GET Request path: " << path

  begin
    resp = authorized_client.get(path, get_headers(headers))
  rescue => e
    logger.error e
  end
  return handle_response(create_response(resp), "body", field)
end

#get_basepathObject

Each Bluevia API has a specific basepath. This method gets the specific service basepath and the path associated to the request (either test or commercial)



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/bluevia/base_client.rb', line 131

def get_basepath
  begin
    basepath = self.class.const_get("BASEPATH_API")
  rescue NameError => e
    logger.error "Unable to fetch basepath #{e}"
    basepath = "/#{self.class.to_s.upcase}_"
  end

  if @commercial
    path = BASEPATH_COMMERCIAL
  else
    path = BASEPATH_SANDBOX
  end
  "#{basepath}#{path}"
end

#get_headers(_headers = nil, is_post = false) ⇒ Object

Creates the basic header while creating an HTTP Request



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/bluevia/base_client.rb', line 211

def get_headers(_headers = nil, is_post = false)
  
  headers = {
    "Accept" => "application/json"
    }

  if is_post
    headers["Content-Type"] = "application/json"
  end

  unless _headers.nil?
    headers.merge!(_headers) { |key,oldval,newval| newval }
  end
  logger.debug "HEADERS: "
  logger.debug headers
  return headers
end

#include_params(_path, _params) ⇒ Object

Include params in the URI being created to perform a REST call



119
120
121
122
123
124
125
126
# File 'lib/bluevia/base_client.rb', line 119

def include_params(_path, _params)
  "#{_path}?".
    concat(
    _params.collect{
      |k,v|
      "#{k}=#{CGI::escape(v.to_s)}"
    }.reverse.join('&')) unless _params.nil?
end

#POST(_path = nil, body = nil, headers = nil, files = nil, return_header = nil) ⇒ Object

Send an Http::Post to the server



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/bluevia/base_client.rb', line 170

def POST(_path = nil, body = nil, headers = nil, files = nil, return_header = nil)

  if files.nil?
    path = set_path(_path)
    path = include_params(path, DEFAULT_PARAMS)
    logger.debug "POST Request path: " << path
    logger.debug "POST body: " << body

    begin
      resp = authorized_client.post(path, body, get_headers(headers, true))
    rescue => e
      logger.error e
    end
    return handle_response(create_response(resp), return_header.nil? ? nil : "headers", return_header)
  else
    return POST_MULTIPART(_path, body, headers, files)
  end
end

#set_http_client(rest) ⇒ Object

set HTTP client



89
90
91
# File 'lib/bluevia/base_client.rb', line 89

def set_http_client(rest)
  @rest = rest
end

#set_path(_path = nil) ⇒ Object

Creates a valid path by concat the path received with the path defined in BASEPATH



111
112
113
114
# File 'lib/bluevia/base_client.rb', line 111

def set_path(_path = nil)
  path = BASEPATH.clone
  path << _path unless _path.nil?
end

#set_timeout(timeout = 5) ⇒ Object

set the valid timeout for the HTTP requests



96
97
98
# File 'lib/bluevia/base_client.rb', line 96

def set_timeout(timeout = 5)
  !timeout.nil? and @rest.read_timeout(timeout)
end