Class: RewardStation::Client

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

Direct Known Subclasses

StubClient

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/reward_station/client.rb', line 6

def initialize options = {}
  [:client_id, :client_password].each do |arg|
    raise ArgumentError, "Missing required option '#{arg}'" unless options.has_key? arg
  end

  @client_id = options[:client_id]
  @client_password = options[:client_password]
  @token = options[:token]
  @organization_id = options[:organization_id]

  @program_id = options[:program_id]
  @point_reason_code_id = options[:point_reason_code_id]

  if options[:new_token_callback]
    raise ArgumentError, "new_token_callback option should be proc or lambda" unless options[:new_token_callback].is_a?(Proc)
    @new_token_callback = options[:new_token_callback]
  end

end

Instance Attribute Details

#tokenObject

Returns the value of attribute token.



4
5
6
# File 'lib/reward_station/client.rb', line 4

def token
  @token
end

Class Method Details

.get_clientObject



36
37
38
39
40
# File 'lib/reward_station/client.rb', line 36

def get_client
  @@client ||= Savon::Client.new do |wsdl|
    wsdl.document = File.join(File.dirname(__FILE__), '..', 'wsdl', 'reward_services.xml')
  end
end

.loggerObject



42
43
44
# File 'lib/reward_station/client.rb', line 42

def logger
  @@logger ||= defined?(Rails) ? Rails.logger : Logger.new(STDOUT)
end

.stub(options = {}) ⇒ Object



32
33
34
# File 'lib/reward_station/client.rb', line 32

def stub options = {}
  RewardStation::StubClient.new options
end

Instance Method Details

#award_points(user_id, points, description, program_id = nil, point_reason_code_id = nil) ⇒ Object



66
67
68
69
70
71
72
73
74
# File 'lib/reward_station/client.rb', line 66

def award_points user_id, points, description, program_id = nil, point_reason_code_id = nil
  request_with_token(:award_points, :body => {
      'UserID' => user_id,
      'Points' => points,
      'ProgramID' => program_id || @program_id,
      'PointReasonCodeID' => point_reason_code_id || @point_reason_code_id,
      'Description' => description
  })[:confirmation_number]
end

#create_user(attrs = {}) ⇒ Object



133
134
135
# File 'lib/reward_station/client.rb', line 133

def create_user attrs = {}
  update_user -1, attrs
end

#loggerObject



47
48
49
# File 'lib/reward_station/client.rb', line 47

def logger
  Client.logger
end

#new_token_callback(&block) ⇒ Object



26
27
28
# File 'lib/reward_station/client.rb', line 26

def new_token_callback &block
  @new_token_callback = block
end

#return_point_summary(user_id) ⇒ Object



77
78
79
80
81
82
# File 'lib/reward_station/client.rb', line 77

def return_point_summary user_id
  request_with_token(:return_point_summary, :body => {
      'clientId' => @client_id,
      'userId' => user_id
  })[:point_summary_collection][:point_summary]
end


137
138
139
# File 'lib/reward_station/client.rb', line 137

def return_popular_products user_id
  request_with_token(:return_popular_products , :body => { 'userId' => user_id} )[:products][:product]
end

#return_tokenObject



51
52
53
54
55
56
57
58
59
60
# File 'lib/reward_station/client.rb', line 51

def return_token
  result = request :return_token, :body => {
      'AccountNumber' => @client_id,
      'AccountCode' => @client_password
  }

  logger.info "xceleration token #{result[:token]}"

  result[:token]
end

#return_user(user_id, attrs = {}) ⇒ Object



62
63
64
# File 'lib/reward_station/client.rb', line 62

def return_user user_id
  request_with_token(:return_user, :body => { 'UserID' => user_id} )[:user_profile]
end

#update_user(user_id, attrs = {}) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/reward_station/client.rb', line 95

def update_user user_id, attrs = {}

  organization_id = attrs[:organization_id] || @organization_id
  email = attrs[:email] || ""
  first_name = attrs[:first_name] || ""
  last_name = attrs[:last_name] || ""
  user_name = attrs[:user_name] || email
  balance = attrs[:balance] || 0

  request_with_token(:update_user , :body => {
      'updateUser' => {
          'UserID' => user_id,
          'ClientID' => @client_id,
          'UserName' => user_name,
          'FirstName' => first_name,
          'LastName' => last_name,
          'CountryCode' => 'USA',
          'Email' => email,
          'IsActive' => true,
          'PointBalance' => balance,
          'OrganizationID' => organization_id,
          'AddressOne' => "",
          'AddressTwo' => "",
          'City' => "",
          'StateCode' => "",
          'Province' => "",
          'PostalCode' => "",
          'Phone' => "",
          'OrganizationName' => "",
          'RepTypeID' => "",
          'ClientRegionID' => "",
          'ManagerID' => "",
          'ManagerName' => ""
      }
  })[:update_user]
end