Class: Mb::Service

Inherits:
Object
  • Object
show all
Extended by:
Meta
Defined in:
lib/mindbody/service.rb

Constant Summary collapse

SRC_CREDS =
"SourceCredentials"

Constants included from Meta

Meta::NS, Meta::WSDL_POSTFIX, Meta::WSDL_PREFIX

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Meta

wsdl_url

Constructor Details

#initialize(args) ⇒ Service

Returns a new instance of Service.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/mindbody/service.rb', line 38

def initialize args
	if args.is_a? Mb::SourceCredentials
    	@src_creds = args
	else	
	#assume a hash
	@client = nil
	@src_creds = args[:source_credentials]
	@usr_creds = args[:user_credentials]
	end

	if local_document
		@client = Savon::Client.new do
			wsdl.document = File.expand_path( local_document , __FILE__)
		end
	else
		@client = Savon::Client.new endpoint			
	end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_id, *arguments, &block) ⇒ Object



139
140
141
142
143
144
145
146
147
# File 'lib/mindbody/service.rb', line 139

def method_missing(method_id, *arguments, &block)
 		if method_id.to_s =~ /^get_\w+/
		options = arguments[0] || {}
	    non_mb_options = arguments[1] || {}
		get_service method_id.to_sym, options.is_a?(Hash) ? options : {}, non_mb_options[:unwrap]			
	else
		super
	end	
end

Class Attribute Details

.doc_pathObject

Returns the value of attribute doc_path.



9
10
11
# File 'lib/mindbody/service.rb', line 9

def doc_path
  @doc_path
end

.endpoint(url = nil) ⇒ Object

Returns the endpoint (a WSDL document) that this service is using to access its methods, if a url is supplied the endpoint it updated and then returned



26
27
28
# File 'lib/mindbody/service.rb', line 26

def endpoint
  @endpoint
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



35
36
37
# File 'lib/mindbody/service.rb', line 35

def client
  @client
end

#src_credsObject

Returns the value of attribute src_creds.



35
36
37
# File 'lib/mindbody/service.rb', line 35

def src_creds
  @src_creds
end

#usr_credsObject

Returns the value of attribute usr_creds.



35
36
37
# File 'lib/mindbody/service.rb', line 35

def usr_creds
  @usr_creds
end

Class Method Details

.local_document(path) ⇒ Object



16
17
18
# File 'lib/mindbody/service.rb', line 16

def self.local_document(path)
	@doc_path = path			
end

.service(service_name) ⇒ Object

Sets up the service WSDL endpoint given a Mindbody service name



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

def self.service(service_name)
	@endpoint = self.wsdl_url(service_name)
end

Instance Method Details

#build_request(options = {}) ⇒ Object

Builds the inner XML of the Mindbody SOAP call



58
59
60
61
62
63
64
65
66
67
68
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
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/mindbody/service.rb', line 58

def build_request(options = {})

		src_creds_name = SRC_CREDS

		options = options.dup #Don't clobber the original hash
		#NOTE: Could extend this to read WSDL document or using Savon client to tell which nodes are allowed in the request
		#performing the same type of test against passed in variables on each and replacing with the appropriate value

		final_opts = options.dup

		options.keys.each do |key|
			orig_key = key
			new_key = orig_key.to_s.gsub("_", "").downcase
			if (new_key == src_creds_name.downcase)
				final_opts[src_creds_name] = final_opts[key] #Set "SourceCredentials" to hash referenced by similarly named
				final_opts.delete(orig_key)
			end
		end
		
		opts = {};
		final_opts.each  do |key, value|
			new_val = value
			if value.kind_of?(Array)
				tranformed = {}	
				if item[0].kind_of? Integer
					transformed[:int] = value
				elsif item[0].kind_of? String
					transformed[:string] = value
				else
					break #Don't know how to deal with it, return regular
				end
				 new_val = transformed
			end
			opts[key] = new_val
		end

		request_body = 
		{
			"PageSize" => 10, 
			"CurrentPageIndex" => 0
		}

		request_body["XMLDetail"] = "Bare" unless opts["Fields"]


		request_body[src_creds_name] = @src_creds.to_hash if @src_creds
		request_body["UserCredentials"] = @usr_creds.to_hash if @usr_creds	

		return request_body.merge!(opts)
end

#endpointObject



31
32
33
# File 'lib/mindbody/service.rb', line 31

def endpoint
	self.class.endpoint
end

#get_service(service_symbol, options, unwrap_bool) ⇒ Object

Build a Mindbody SOAP request for the given service



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/mindbody/service.rb', line 110

def get_service(service_symbol, options, unwrap_bool)
    raise "No SOAP client instantiated" unless @client

	request_options = build_request(options)

	raise "No SourceCredentials supplied" if !@src_creds || !request_options[SRC_CREDS] #Just checking for :source_credentials does not 
	#check all possiblities as "SourceCredentials", 
	response = @client.request Mb::Meta::NS, service_symbol do
		soap.body = 
		{	
			"Request" => request_options
		}
    end	
	if unwrap_bool
		#Unwrap response to hash array underneath)
	    plural = service_symbol.to_s.gsub("get_", "")
		singular =plural[0..-2] #Remove last
	   	r = response.to_hash
		basify = lambda {|enda| (service_symbol.to_s + "_" + enda).to_sym }  
		r = r[basify.call('response')][basify.call('result')]
		return [] if r[:result_count] == "0"
		r = r[plural.to_sym][singular.to_sym]

		return r	 
	end
	
	response
end

#local_documentObject



20
21
22
# File 'lib/mindbody/service.rb', line 20

def local_document
	self.class.doc_path
end

#respond_to?(method_id, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


149
150
151
152
153
154
155
# File 'lib/mindbody/service.rb', line 149

def respond_to?(method_id, include_private = false)
	if method_id.to_s =~ /^get_\w+/
		true
	else
		super
	end
end