Class: UserApp::Client
- Inherits:
-
Object
- Object
- UserApp::Client
- Defined in:
- lib/userapp/client.rb,
lib/userapp/userapp.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
Returns the value of attribute options.
Instance Method Summary collapse
- #call(version, service, method, arguments) ⇒ Object
- #get_options(*args) ⇒ Object
-
#initialize(*args) ⇒ Client
constructor
A new instance of Client.
Constructor Details
#initialize(*args) ⇒ Client
Returns a new instance of Client.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/userapp/client.rb', line 11 def initialize(*args) @options = self.() if args.length == 1 and args[0].class == Hash @options.set(args[0]) else if args.length >= 1 .app_id = args[0] end if args.length >= 2 .token = args[2] end end end |
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
9 10 11 |
# File 'lib/userapp/client.rb', line 9 def @options end |
Instance Method Details
#call(version, service, method, arguments) ⇒ Object
33 34 35 36 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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/userapp/client.rb', line 33 def call(version, service, method, arguments) # Build the URL of the API to call target_url = 'http' + (!self..secure || 's') + '://' + self..base_address + '/' target_url << 'v' + version.to_s + '/' + service.to_s + '.' + method if arguments.nil? arguments = {} end if self.().debug self.log("Sending HTTP POST request to '#{target_url}' with app_id '#{self..app_id}', token '#{self..token}' and arguments '#{arguments.to_json()}'.") end resource = RestClient::Resource.new(target_url, self..app_id, self..token) resource.post( (arguments.nil? ? {} : arguments).to_json(), :content_type => :json ){ |response, request, z_result, &block| if self.().debug self.log("Recieved HTTP response with code '#{response.code}' and body '#{response.body}'.") end if response.code != 200 and response.code != 401 raise Error.new("Recieved HTTP status #{response.code}, expected 200.") end result = HashUtility.convert_to_object(JSON.parse(response.body)) is_error_result = result.respond_to?('error_code') if self..throw_errors and is_error_result if result.error_code == 'INVALID_SERVICE' raise InvalidServiceError.new("Service '#{service}' does not exist.") elsif result.error_code == 'INVALID_METHOD' raise InvalidServiceError.new("Method '#{method}' on service '#{service}' does not exist.") else raise ServiceError.new(result.error_code, result.) end end if service == 'user' if not is_error_result and method == 'login' self..token = result.token elsif method == 'logout' self..token = nil end end return result } end |
#get_options(*args) ⇒ Object
26 27 28 29 30 31 |
# File 'lib/userapp/client.rb', line 26 def (*args) if self..nil? self. = ClientOptions.get_global().clone() end return self. end |