Class: ApiClient

Inherits:
Object
  • Object
show all
Defined in:
motion-prime/api_client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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(options = {})
  self.access_token = options[:access_token]
  AFMotion::Client.build_shared(config.base) do
    header "Accept", "application/json"
    response_serializer AFJSONResponseSerializer.serializerWithReadingOptions(NSJSONReadingMutableContainers)
  end
end

Instance Attribute Details

#access_tokenObject

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

#add_to_queue(item) ⇒ Object



88
89
90
91
92
# File 'motion-prime/api_client.rb', line 88

def add_to_queue(item)
  queue = user_defaults['api_client_queue'].clone || []
  queue.push(item)
  user_defaults['api_client_queue'] = queue
end

#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.statusCode) if use_callback
  end
  true
end

#delete(path, params = {}, options = {}, &block) ⇒ Object



107
108
109
110
# File 'motion-prime/api_client.rb', line 107

def delete(path, params = {}, options = {}, &block)
  options[:allow_queue] = true unless options.has_key?(:allow_queue)
  request(:delete, path, params, options, &block)
end

#get(path, params = {}, options = {}, &block) ⇒ Object



94
95
96
# File 'motion-prime/api_client.rb', line 94

def get(path, params = {}, options = {}, &block)
  request(:get, path, params, options, &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



102
103
104
105
# File 'motion-prime/api_client.rb', line 102

def post(path, params = {}, options = {}, &block)
  options[:allow_queue] = true unless options.has_key?(:allow_queue)
  request(:post, path, params, options, &block)
end

#process_queueObject



80
81
82
83
84
85
86
# File 'motion-prime/api_client.rb', line 80

def process_queue
  queue = user_defaults['api_client_queue']
  user_defaults['api_client_queue'] = []
  Array.wrap(queue).each do |item|
    request(item[:method], item[:path], item[:params].clone.symbolize_keys)
  end
end

#put(path, params = {}, options = {}, &block) ⇒ Object



98
99
100
# File 'motion-prime/api_client.rb', line 98

def put(path, params = {}, options = {}, &block)
  request(:put, path, params, options, &block)
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
73
74
75
76
77
78
# File 'motion-prime/api_client.rb', line 55

def request(method, path, params = {}, options = {}, &block)
  params.merge!(access_token: access_token)
  data = request_params(params)
  files = data.delete(:_files)
  use_callback = block_given?

  client_method = files.present? ? :"multipart_#{method}" : method
  AFMotion::Client.shared.send client_method, "#{config.api_namespace}#{path}", data do |response, form_data, progress|
    if form_data && files.present?
      files.each do |file_data|
        form_data.appendPartWithFileData(file_data[:data], name: file_data[:name], fileName:"avatar.png", mimeType: "image/jpeg")
      end
    elsif progress
      # handle progress
    elsif !response.success? && options[:allow_queue] && config.allow_queue?
      add_to_queue(method: method, path: path, params: params)
    elsif response.operation.response.nil?
      block.call if use_callback
    else
      block.call(prepared_object(response.object), response.operation.response.statusCode) if use_callback
      process_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