Class: ApiClient
- Inherits:
-
Object
- Object
- ApiClient
- Defined in:
- motion-prime/api_client.rb
Instance Attribute Summary collapse
-
#access_token ⇒ Object
Returns the value of attribute access_token.
Instance Method Summary collapse
- #authenticate(username = nil, password = nil, data = nil, &block) ⇒ Object
- #cached_request!(method, path, data, files = nil, options = {}, &block) ⇒ Object
- #delete(path, params = {}, options = {}, &block) ⇒ Object
- #get(path, params = {}, options = {}, &block) ⇒ Object
-
#initialize(options = {}) ⇒ ApiClient
constructor
A new instance of ApiClient.
- #page_url(path) ⇒ Object
- #post(path, params = {}, options = {}, &block) ⇒ Object
- #put(path, params = {}, options = {}, &block) ⇒ Object
- #queue(item) ⇒ Object
-
#read_cache(key) ⇒ Object
TODO: temporary solution, add real caching system here.
- #request(method, path, params = {}, options = {}, &block) ⇒ Object
- #request!(method, path, data, files = nil, options = {}, &block) ⇒ Object
- #request_params(data) ⇒ Object
- #resource_url(path) ⇒ Object
-
#write_cache(key, data) ⇒ Object
TODO: temporary solution, add real caching system here.
Constructor Details
#initialize(options = {}) ⇒ ApiClient
Returns a new instance of ApiClient.
4 5 6 7 8 9 10 |
# File 'motion-prime/api_client.rb', line 4 def initialize( = {}) self.access_token = [:access_token] AFMotion::Client.build_shared(config.base) do header "Accept", "application/json" response_serializer AFJSONResponseSerializer.serializerWithReadingOptions(NSJSONReadingMutableContainers) end end |
Instance Attribute Details
#access_token ⇒ Object
Returns the value of attribute access_token.
2 3 4 |
# File 'motion-prime/api_client.rb', line 2 def access_token @access_token end |
Instance Method Details
#authenticate(username = nil, password = nil, data = nil, &block) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'motion-prime/api_client.rb', line 27 def authenticate(username = nil, password = nil, data = nil, &block) data ||= { grant_type: "password", username: username, password: password, client_id: config.client_id, client_secret: config.client_secret } use_callback = block_given? AFMotion::Client.shared.post(config.auth_path, request_params(data)) do |response| auth_data = response.object self.access_token = auth_data[:access_token] if auth_data block.call(auth_data, response.operation.response.try(:statusCode)) if use_callback end true end |
#cached_request!(method, path, data, files = nil, options = {}, &block) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'motion-prime/api_client.rb', line 95 def cached_request!(method, path, data, files = nil, = {}, &block) use_callback = block_given? params = data.map { |key, value| "#{key}=#{value}" }.join('&') cache_key = [method, path, params].join(' ') response = read_cache(cache_key) if response && use_callback block.call(prepare_response_object(response), 200) else request!(method, path, data, files, ) do |response, status| write_cache(cache_key, response) block.call(response, status) if use_callback end end end |
#delete(path, params = {}, options = {}, &block) ⇒ Object
122 123 124 |
# File 'motion-prime/api_client.rb', line 122 def delete(path, params = {}, = {}, &block) request(:delete, path, params, , &block) end |
#get(path, params = {}, options = {}, &block) ⇒ Object
110 111 112 |
# File 'motion-prime/api_client.rb', line 110 def get(path, params = {}, = {}, &block) request(:get, path, params, , &block) end |
#page_url(path) ⇒ Object
45 46 47 |
# File 'motion-prime/api_client.rb', line 45 def page_url(path) "#{config.base}#{path}" end |
#post(path, params = {}, options = {}, &block) ⇒ Object
118 119 120 |
# File 'motion-prime/api_client.rb', line 118 def post(path, params = {}, = {}, &block) request(:post, path, params, , &block) end |
#put(path, params = {}, options = {}, &block) ⇒ Object
114 115 116 |
# File 'motion-prime/api_client.rb', line 114 def put(path, params = {}, = {}, &block) request(:put, path, params, , &block) end |
#queue(item) ⇒ Object
126 127 128 129 130 |
# File 'motion-prime/api_client.rb', line 126 def queue(item) queue_list = user_defaults['api_client_queue'].clone || [] queue_list.push(item) user_defaults['api_client_queue'] = queue_list end |
#read_cache(key) ⇒ Object
TODO: temporary solution, add real caching system here
133 134 135 136 137 |
# File 'motion-prime/api_client.rb', line 133 def read_cache(key) puts "read cache #{key}" @cache ||= {} @cache[key] end |
#request(method, path, params = {}, options = {}, &block) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'motion-prime/api_client.rb', line 55 def request(method, path, params = {}, = {}, &block) files = params.delete(:_files) data = request_params(params.merge(access_token: access_token)) if !.has_key?(:allow_queue) && config.default_methods_queue.include?(method.to_sym) [:allow_queue] = true end if !.has_key?(:allow_cache) && config.default_methods_cache.include?(method.to_sym) [:allow_cache] = true end if allow_cache?(method, path, ) cached_request!(method, path, data, files, , &block) else request!(method, path, data, files, , &block) end end |
#request!(method, path, data, files = nil, options = {}, &block) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'motion-prime/api_client.rb', line 74 def request!(method, path, data, files = nil, = {}, &block) use_callback = block_given? path = "#{config.api_namespace}#{path}" unless path.starts_with?('http') client_method = files.present? ? :"multipart_#{method}" : method AFMotion::Client.shared.send client_method, path, data do |response, form_data, progress| if form_data && files.present? append_files_to_data(files, form_data) elsif progress # handle progress elsif !response.success? && allow_queue?(method, path, ) queue(method: method, path: path, params: params) elsif response.operation.response.nil? block.call if use_callback else prepared_response = prepare_response_object(response.object) block.call(prepared_response, response.operation.response.statusCode) if use_callback process_queue if config.allow_queue? end end end |
#request_params(data) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'motion-prime/api_client.rb', line 12 def request_params(data) params = data.clone if config.http_auth? params.merge!(credentials: config.http_auth.to_hash) end if config.sign_request? signature = RmDigest::MD5.hexdigest( config.signature_secret + params.keys.map(&:to_s).sort.join ) params.merge!(sign: signature) end params end |
#resource_url(path) ⇒ Object
49 50 51 52 53 |
# File 'motion-prime/api_client.rb', line 49 def resource_url(path) # return if path.blank? base = config.resource_base? ? config.resource_base : config.base "#{base}#{path}" end |
#write_cache(key, data) ⇒ Object
TODO: temporary solution, add real caching system here
140 141 142 143 |
# File 'motion-prime/api_client.rb', line 140 def write_cache(key, data) @cache ||= {} @cache[key] = data end |