Class: Bluevia::Advertising

Inherits:
BaseClient show all
Defined in:
lib/bluevia/advertising.rb

Overview

This class fetches information related to Advertising API.

Constant Summary collapse

OPTIONAL_PARAMS =

optional parameters in the request

[:ad_presentation, :ad_presentation_size, :keywords, :protection_policy]
REQUIRED_PARAMS =

required parameters in the request

[:user_agent, :ad_request_id, :ad_space]
VALID_PARAMS =
OPTIONAL_PARAMS + REQUIRED_PARAMS
BASEPATH_API =

Base Path for Advertising API

"/Advertising"

Constants inherited from BaseClient

BaseClient::BASEPATH, BaseClient::BASEPATH_COMMERCIAL, BaseClient::BASEPATH_SANDBOX, BaseClient::BASEURI, BaseClient::DEFAULT_PARAMS, BaseClient::PROXY

Instance Attribute Summary

Attributes inherited from BaseClient

#base_uri

Instance Method Summary collapse

Methods inherited from BaseClient

#DELETE, #GET, #POST, #[]=, #authorized_client, create_rest_client, #get_basepath, #get_headers, #include_params, #set_http_client, #set_path, #set_timeout

Methods included from BlueviaLogger

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

Constructor Details

#initialize(params = nil) ⇒ Advertising

Returns a new instance of Advertising.



23
24
25
# File 'lib/bluevia/advertising.rb', line 23

def initialize(params = nil)
  super(params)
end

Instance Method Details

#request(params) ⇒ Object

This method fetchs an Ad from the server.

params

Hash object that must include at least the REQUIRED_PARAMS and

cero or more OPTIONAL_PARAMS i.e.

response = @service.request(

{:user_agent => "Mozilla 5.0",
 :ad_request_id => "a1x4zasg58",
 :ad_space => "1200",
 :keywords => "bar"
})


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
# File 'lib/bluevia/advertising.rb', line 43

def request(params)
  Utils.check_attribute(params, "params cannot be null")
  
  REQUIRED_PARAMS.each { |param|
    Utils.check_attribute(params[param], "#{param} cannot be null")
  }

  # delete any parameter that is neither optional nor mandatory
  params.delete_if{|key,value| VALID_PARAMS.index(key).nil?}

  # include in the request all the parameters
  message = params.collect{|k,v| "#{k}=#{CGI::escape(v.to_s)}"}.join('&')

  # this should be sent without content-Type because fails to validate signature
  path = set_path("#{get_basepath}/simple/requests")
  path = include_params(path, DEFAULT_PARAMS)
  response = authorized_client.post(
    path,
    message,
    {"Accept" => "application/json"})
  
  resp = handle_response(create_response(response)).body
  
  filter_response(resp)      
end