Module: Zanox::API

Defined in:
lib/zanox.rb

Defined Under Namespace

Modules: Session Classes: AuthError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#driverObject

Returns the value of attribute driver.



15
16
17
# File 'lib/zanox.rb', line 15

def driver
  @driver
end

#public_keyObject

Returns the value of attribute public_key.



12
13
14
# File 'lib/zanox.rb', line 12

def public_key
  @public_key
end

#secret_keyObject

Returns the value of attribute secret_key.



13
14
15
# File 'lib/zanox.rb', line 13

def secret_key
  @secret_key
end

#wsdlObject

Returns the value of attribute wsdl.



14
15
16
# File 'lib/zanox.rb', line 14

def wsdl
  @wsdl
end

Class Method Details

.authenticate(connect_id, secret_key = nil) ⇒ Object



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

def self.authenticate(connect_id, secret_key=nil)
  raise 'Zanox::API.authenticate(connect_id, secret_key) is no longer supported. Please use Zanox::API.authorize("your public key", "your secret key") instead.'
end

.authorize(public_key, secret_key) ⇒ Object



56
57
58
59
60
# File 'lib/zanox.rb', line 56

def self.authorize(public_key, secret_key)
  @public_key = public_key
  @secret_key = secret_key
  true
end

.create_signature(secret_key, string2sign) ⇒ Object



52
53
54
# File 'lib/zanox.rb', line 52

def self.create_signature(secret_key, string2sign)
  Base64.encode64(HMAC::SHA1.new(secret_key).update(string2sign).digest)[0..-2]
end

.generate_nonceObject



44
45
46
# File 'lib/zanox.rb', line 44

def self.generate_nonce
  Digest::MD5.hexdigest((Time.new.usec + rand()).to_s)
end

.get_timestampObject



48
49
50
# File 'lib/zanox.rb', line 48

def self.get_timestamp
  Time.new.gmtime.strftime("%Y-%m-%dT%H:%M:%S.000Z").to_s
end

.request(method, options) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/zanox.rb', line 17

def self.request(method, options)
  begin
    puts method + " " + options.inspect if $DEBUG
  
    options.merge!(:connectId=>Zanox::API::Session.connect_id)
  
    #raise AuthError, "Missing connect id. Try calling Zanox::API.authenticate('your connect id', 'your secret key') before your requests", caller[caller.length - 1] if !!options[:connect_id]
  
    unless Zanox::API::Session.secret_key.nil?
      timestamp = Zanox::API.get_timestamp
      nonce = Zanox::API.generate_nonce
      signature = Zanox::API.create_signature(Zanox::API::Session.secret_key, "publisherservice"+method.downcase + timestamp + nonce)
      options.merge!(:timestamp=>timestamp, :nonce=>nonce, :signature=>signature)
    end
  
    @wsdl = 'http://api.zanox.com/wsdl/2009-07-01/' unless !!@wsdl
    @driver = SOAP::WSDLDriverFactory.new(@wsdl).create_rpc_driver unless !!@driver
    @driver.wiredump_dev = STDOUT if $DEBUG
    @driver.options['protocol.http.ssl_config.verify_mode'] = OpenSSL::SSL::VERIFY_NONE if $DEBUG
    @driver.method(method.to_sym).call(options)
  rescue Exception => e
    puts
    puts "ERROR"
    puts e.message
  end
end