Class: EasyUpnp::ServiceMethod
- Inherits:
-
Object
- Object
- EasyUpnp::ServiceMethod
- Defined in:
- lib/easy_upnp/control_point/service_method.rb
Instance Attribute Summary collapse
-
#in_args ⇒ Object
readonly
Returns the value of attribute in_args.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#out_args ⇒ Object
readonly
Returns the value of attribute out_args.
Class Method Summary collapse
Instance Method Summary collapse
- #arg_reference(arg) ⇒ Object
- #call_method(client, args_hash, validator_provider) ⇒ Object
-
#initialize(name, in_args, out_args, arg_references) ⇒ ServiceMethod
constructor
A new instance of ServiceMethod.
- #parse_response(response) ⇒ Object
Constructor Details
#initialize(name, in_args, out_args, arg_references) ⇒ ServiceMethod
Returns a new instance of ServiceMethod.
5 6 7 8 9 10 |
# File 'lib/easy_upnp/control_point/service_method.rb', line 5 def initialize(name, in_args, out_args, arg_references) @name = name @in_args = in_args @out_args = out_args @arg_references = arg_references end |
Instance Attribute Details
#in_args ⇒ Object (readonly)
Returns the value of attribute in_args.
3 4 5 |
# File 'lib/easy_upnp/control_point/service_method.rb', line 3 def in_args @in_args end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/easy_upnp/control_point/service_method.rb', line 3 def name @name end |
#out_args ⇒ Object (readonly)
Returns the value of attribute out_args.
3 4 5 |
# File 'lib/easy_upnp/control_point/service_method.rb', line 3 def out_args @out_args end |
Class Method Details
.from_xml(xml) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/easy_upnp/control_point/service_method.rb', line 58 def self.from_xml(xml) name = xml.xpath('name').text.to_sym args = xml.xpath('argumentList') arg_references = {} extract_args = ->(v) do arg_name = v.xpath('name').text.to_sym ref = v.xpath('relatedStateVariable').text.to_sym arg_references[arg_name] = ref arg_name end in_args = args.xpath('argument[direction = "in"]').map(&extract_args) out_args = args.xpath('argument[direction = "out"]').map(&extract_args) ServiceMethod.new(name, in_args, out_args, arg_references) end |
Instance Method Details
#arg_reference(arg) ⇒ Object
54 55 56 |
# File 'lib/easy_upnp/control_point/service_method.rb', line 54 def arg_reference(arg) @arg_references[arg.to_sym] end |
#call_method(client, args_hash, validator_provider) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/easy_upnp/control_point/service_method.rb', line 12 def call_method(client, args_hash, validator_provider) raise ArgumentError, 'Service args must be a hash' unless args_hash.is_a?(Hash) present_args = args_hash.keys.map(&:to_sym) if (unsupported_args = (present_args - in_args)).any? raise ArgumentError, "Unsupported arguments: #{unsupported_args.join(', ')}." << " Supported args: #{in_args.join(', ')}" end args_hash.each do |arg, val| validator = validator_provider.validator(arg_reference(arg)) begin validator.validate(val) rescue ArgumentError => e raise ArgumentError, "Invalid value for argument #{arg}: #{e}" end end raw_response = client.call(name, args_hash) parse_response(raw_response) end |
#parse_response(response) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/easy_upnp/control_point/service_method.rb', line 35 def parse_response(response) # Response is usually wrapped in <#{ActionName}Response></>. For example: # <BrowseResponse>...</BrowseResponse>. Extract the body since consumers # won't care about wrapper stuff. if response.body.keys.count > 1 raise RuntimeError, "Unexpected keys in response body: #{response.body.keys}" end result = response.body.first[1] output = {} # Keys returned by savon are underscore style. Convert them to camelcase. out_args.map do |arg| output[arg] = result[underscore(arg.to_s).to_sym] end output end |