Class: Yieldmanager::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/yieldmanager/client.rb

Overview

This is the frontend for using Yieldmanager programmatically. It can be directly used by the user by creating a new instance and calling service_name to access YM services. For example:

ym = Yieldmanager::Client(
  :user => "bob",
  :pass => "secret"
)

ym.session do |token|
  currencies = @ym.dictionary.getCurrencies(token)
end

It also offers simple access to the ReportWare reporting engine via the #pull_report method.

Constant Summary collapse

BASE_URL =
"https://api.yieldmanager.com/api-"
BASE_URL_TEST =
"https://api-test.yieldmanager.com/api-"
WSDL_DIR =
File.join(File.dirname(__FILE__), '..', '..', 'wsdls')

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = nil) ⇒ Client

Creates interface object.

Options:

  • :user (required) - Yieldmanager user

  • :pass (required) - Yieldmanager pass

  • :env (optional) - Yieldmanager environment “prod” or “test” (defaults to prod)



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/yieldmanager/client.rb', line 95

def initialize(options = nil)
  unless options &&
    (options[:user] || options['user']) &&
    (options[:pass] || options['pass'])
    raise ArgumentError, ":user and :pass are required"
  end
  @user = options[:user] ||= options['user']
  @pass = options[:pass] ||= options['pass']
  @api_version = Yieldmanager::Client.api_version
  if options[:env]
    @env = options[:env]
  elsif options['env']
    @env = options['env']
  else
    @env = "prod"
  end
  @wsdl_dir = "#{WSDL_DIR}/#{@api_version}/#{@env}"
  wrap_services
end

Instance Attribute Details

#api_versionObject (readonly)

Yieldmanager api version (i.e., “1.33”)



82
83
84
# File 'lib/yieldmanager/client.rb', line 82

def api_version
  @api_version
end

#envObject (readonly)

Yieldmanager environment (“prod” or “test”, defaults to “prod”)



84
85
86
# File 'lib/yieldmanager/client.rb', line 84

def env
  @env
end

#passObject (readonly)

Yieldmanager password



80
81
82
# File 'lib/yieldmanager/client.rb', line 80

def pass
  @pass
end

#userObject (readonly)

Yieldmanager user



78
79
80
# File 'lib/yieldmanager/client.rb', line 78

def user
  @user
end

Class Method Details

.api_versionObject



195
196
197
198
199
200
201
202
# File 'lib/yieldmanager/client.rb', line 195

def self.api_version
  version_file = "API_VERSION"
  path = File.join(File.dirname(__FILE__), '..', '..', version_file)
  unless File.exists?(path)
    fail "Put the API version in a file called #{version_file}"
  end
  File.open(path){ |f| f.readline.chomp }
end

Instance Method Details

#available_servicesObject



115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/yieldmanager/client.rb', line 115

def available_services
  available_services = []
  available_services_file = "AVAILABLE_SERVICES"
  path = File.join(File.dirname(__FILE__), '..', '..', available_services_file)
  unless File.exists?(path)
    fail "Put available services in a file called #{available_services_file}"
  end
  IO.readlines(path).each do |line|
    available_services << camel_to_under(line.chomp.sub(/Service$/,''))
  end
  available_services
end

#camel_to_under(s) ⇒ Object



128
129
130
# File 'lib/yieldmanager/client.rb', line 128

def camel_to_under s
  s.gsub(/(.)([A-Z])/,'\1_\2').downcase
end

#disable_check_restrictionObject

Disable WSDL restriction checking until re-enabled Some services complain incorrectly about nillable fields requiring values Disabling this checking allows them to pass properly



185
186
187
# File 'lib/yieldmanager/client.rb', line 185

def disable_check_restriction
  ENV["YIELDMANAGER_CHECK_RESTRICTION"] = "SKIP"
end

#enable_check_restrictionObject

Enable WSDL restriction checking (it’s on by default) This is only needed when you’ve explicitly disabled the behavior previously



191
192
193
# File 'lib/yieldmanager/client.rb', line 191

def enable_check_restriction
  ENV["YIELDMANAGER_CHECK_RESTRICTION"] = nil
end

#end_session(token) ⇒ Object

Closes Yieldmanager session

Use #session if possible: it guarantees no hanging sessions



156
157
158
# File 'lib/yieldmanager/client.rb', line 156

def end_session token
  contact.logout(token)
end

#paginate(block_size) ⇒ Object

Allows looping over datasets too large to pull back in one call

Block must return total rows in dataset to know when to stop!



163
164
165
166
167
168
169
170
171
# File 'lib/yieldmanager/client.rb', line 163

def paginate block_size
  page = 1
  total = block_size + 1

  begin
    total = yield page # Need total back from block to know when to stop!
    page += 1
  end until (block_size * (page-1)) > total
end

#pull_report(token, xml, max_wait_seconds = 300) ⇒ Object

Pulls report from RightMedia, returned as Yieldmanager::Report

Must be called within the context of a session



176
177
178
179
180
# File 'lib/yieldmanager/client.rb', line 176

def pull_report token, xml, max_wait_seconds=300
  report = Yieldmanager::Report.new
  report.pull(token, self.report, xml, max_wait_seconds)
  report
end

#sessionObject

Manages Yieldmanager session

Returns block with token string to be used in API/report calls

Guarantees no hanging sessions except during system crashes



137
138
139
140
141
142
143
144
# File 'lib/yieldmanager/client.rb', line 137

def session
  token = start_session
  begin
    yield token
  ensure
    end_session token
  end
end

#start_sessionObject

Opens Yieldmanager session

Use #session if possible: it guarantees no hanging sessions



149
150
151
# File 'lib/yieldmanager/client.rb', line 149

def start_session
  contact.(@user,@pass,{'errors_level' => 'throw_errors','multiple_sessions' => '1'})
end