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



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

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



117
118
119
# File 'lib/reward_station/client.rb', line 117

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



81
82
83
84
85
86
# File 'lib/reward_station/client.rb', line 81

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


121
122
123
# File 'lib/reward_station/client.rb', line 121

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

#return_user_by_user_name(user_name) ⇒ Object



66
67
68
# File 'lib/reward_station/client.rb', line 66

def return_user_by_user_name user_name
   request_with_token(:return_user_by_user_name , :body => { 'UserName' => user_name} )[:user_profile]
end

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

Raises:

  • (ArgumentError)


88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/reward_station/client.rb', line 88

def update_user user_id, attrs = {}
  organization_id = attrs[:organization_id] || @organization_id

  raise(ArgumentError, "organization_id should be numeric") if organization_id.to_i.zero?

  [:email, :first_name, :last_name].each do |f|
    raise(ArgumentError, "#{f} required and should not be empty") if attrs[f].to_s.blank?
  end

  user_name = attrs[:user_name] || attrs[:email]
  balance = attrs[:balance] || 0

  request_with_token(:update_user , :body => {
      'updateUser' => {
          'UserID' => user_id,
          'ClientID' => @client_id,
          'UserName' => user_name,
          'FirstName' => attrs[:first_name],
          'LastName' => attrs[:last_name],
          'CountryCode' => 'USA',
          'Email' => attrs[:email],
          'IsActive' => true,
          'PointBalance' => balance,
          'OrganizationID' => organization_id
      }
  })[:update_user]
end