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",
  :api_version => "1.30"
)

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

Instance Method Summary collapse

Constructor Details

#initialize(options = nil) ⇒ Client

Creates interface object.

Options:

  • :user (required) - Yieldmanager user

  • :pass (required) - Yieldmanager pass

  • :api_version (required) - Yieldmanager API version (i.e., “1.30”)

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



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/yieldmanager/client.rb', line 43

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

Instance Attribute Details

#api_versionObject (readonly)

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



29
30
31
# File 'lib/yieldmanager/client.rb', line 29

def api_version
  @api_version
end

#envObject (readonly)

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



31
32
33
# File 'lib/yieldmanager/client.rb', line 31

def env
  @env
end

#passObject (readonly)

Yieldmanager password



27
28
29
# File 'lib/yieldmanager/client.rb', line 27

def pass
  @pass
end

#userObject (readonly)

Yieldmanager user



25
26
27
# File 'lib/yieldmanager/client.rb', line 25

def user
  @user
end

Instance Method Details

#available_servicesObject



55
56
57
58
59
60
61
62
63
# File 'lib/yieldmanager/client.rb', line 55

def available_services
  Dir.entries(@wsdl_dir).map do |wsdl|
    if wsdl.match(/wsdl/) 
      wsdl.sub(/\.wsdl/,'')
    else
      nil
    end
  end.compact
end

#end_session(token) ⇒ Object

Closes Yieldmanager session

Use #session if possible: it guarantees no hanging sessions



89
90
91
# File 'lib/yieldmanager/client.rb', line 89

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!



96
97
98
99
100
101
102
103
104
# File 'lib/yieldmanager/client.rb', line 96

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) ⇒ Object

Pulls report from RightMedia, returned as Yieldmanager::Report

Must be called within the context of a session



109
110
111
112
113
# File 'lib/yieldmanager/client.rb', line 109

def pull_report token, xml
  report = Yieldmanager::Report.new
  report.pull(token, self.report, xml)
  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



70
71
72
73
74
75
76
77
# File 'lib/yieldmanager/client.rb', line 70

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



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

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