Class: Kashflow::Client

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(login, password) ⇒ Client

Returns a new instance of Client.



13
14
15
16
17
18
# File 'lib/kashflow/client.rb', line 13

def initialize(, password)
  raise "missing login/password" unless  and password

  @login, @password = , password
  @service = Savon::Client.new "https://securedwebapp.com/api/service.asmx?wsdl"
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/kashflow/client.rb', line 24

def method_missing(m, *args, &block) 
  api_method = lookup_api_method(m)

  if api_method
    #puts "found api_method #{api_method.name} for #{m}: #{api_method.request_attrs.inspect}"
    #puts "you're calling with #{args.inspect}"
    
    api_call(m, api_method.name, args)
  else
    raise "method_missing: No method for #{m}"
  end
end

Instance Attribute Details

#serviceObject (readonly)

Returns the value of attribute service.



3
4
5
# File 'lib/kashflow/client.rb', line 3

def service
  @service
end

Class Method Details

.api_methodsObject



9
10
11
# File 'lib/kashflow/client.rb', line 9

def self.api_methods
  @@api_methods ||= YAML.load_file(yaml_path)
end

.yaml_pathObject



5
6
7
# File 'lib/kashflow/client.rb', line 5

def self.yaml_path
  File.join(Kashflow.root, 'config', 'kf_api_methods.yml')
end

Instance Method Details

#api_call(name, method, args) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/kashflow/client.rb', line 37

def api_call(name, method, args)
  soap_return = soap_call(name, method, args)
  response = soap_return["#{name}_response".to_sym]
  #puts "got response: " + response.inspect

  raise "API call failed: [#{response[:status_detail]}]\n\n #{response.inspect}" unless response[:status] == 'OK'

  r = response["#{name}_result".to_sym]

  if r.is_a?(Enumerable)
    if r.values.all?{|v| v.is_a?(Array) }# || r.keys.size == 1
      object_type, attrs = r.first
    else
      #puts "arrayifying #{r.inspect}"
      object_type = lookup_api_method(name).response_attrs.first[:type]
      attrs = r.first.last.is_a?(Hash) ? [r.first.last] : [r]
    end
  
    #puts "it's an enumerable... #{object_type} | #{attrs.inspect}"
  
    ostructs = attrs.map do |record_attrs|
      #puts "making new ostruct with #{record_attrs.inspect}"
      OpenStruct.new(record_attrs.merge(:object_type => object_type.to_s))
    end
    #r.first.last
  else
    #puts "it's a #{r.class}"
    r
  end
end

#lookup_api_method(name) ⇒ Object



20
21
22
# File 'lib/kashflow/client.rb', line 20

def lookup_api_method(name)
  self.class.api_methods.detect{ |api_method| api_method.name.underscore == name.to_s }
end

#soap_call(name, method, params = {}) ⇒ Object

called with CamelCase version of method name



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/kashflow/client.rb', line 69

def soap_call(name, method, params = {})
  begin
    result = @service.send(name) do |soap|
      soap.action = "KashFlow/#{method}"
    
      params = params.pop if params.is_a?(Array)
      params_xml = params.map do |field, value|
        xml_tag = field.to_s.camelize
        "<#{xml_tag}>#{value}</#{xml_tag}>"
      end.join("\n") unless params.blank?
    
      soap.xml = %[<?xml version="1.0" encoding="utf-8"?>
      <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
        <soap:Body>
          <#{method} xmlns="KashFlow">
            <UserName>#{@login}</UserName>
            <Password>#{@password}</Password>
            #{params_xml}
          </#{method}>
        </soap:Body>
      </soap:Envelope>]
    end.to_hash
  rescue Savon::SOAPFault => e
    puts "soap fault:" + e.inspect
    return false
  end
end