Module: QueryInterface::Client::Request::ClassMethods

Defined in:
lib/query-interface-client/request.rb

Instance Method Summary collapse

Instance Method Details

#collection_path(path = nil) ⇒ Object



34
35
36
37
38
# File 'lib/query-interface-client/request.rb', line 34

def collection_path(path=nil)
  fragments = self.collection_path_fragments.dup
  fragments << path unless path.blank?
  fragments.join("/")
end

#collection_path_fragments(*fragments) ⇒ Object



16
17
18
19
20
21
# File 'lib/query-interface-client/request.rb', line 16

def collection_path_fragments(*fragments)
  unless fragments.empty?
    @_qi_collection_path_fragments = [self.default_api.url] + fragments
  end
  @_qi_collection_path_fragments || [self.default_api.url, self.resource_name]
end

#default_apiObject



30
31
32
# File 'lib/query-interface-client/request.rb', line 30

def default_api
  QueryInterface::Client::API.instance_variable_get(:@default_api)
end

#member_path(path = nil, keys = {}) ⇒ Object

TODO: Discern by “am i an instance or a class” what collection and member routes do



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/query-interface-client/request.rb', line 41

def member_path(path=nil, keys={})
  fragments = self.resource_path_fragments.map do |fragment|
    if fragment.is_a?(Symbol)
      if keys.has_key?(fragment)
        keys[fragment]
      else
        nil
      end
    else
      fragment
    end
  end.compact
  fragments << path unless path.blank?
  fragments.join("/")
end

#raw_request(method, params, keys, path) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/query-interface-client/request.rb', line 57

def raw_request(method, params, keys, path)
  if keys
    params[:_path] = self.member_path(path, keys)
  else
    params[:_path] = self.collection_path(path)
  end
  params[:_method] = method
  if params.include?(:_headers)
    params[:_headers] = params[:_headers].merge(self.default_api.options[:headers])
  else
    params[:_headers] = self.default_api.options[:headers]
  end
  result = self.default_api.request(params)
  {parsed_data: result[:parsed_data], response: result[:response], errors: result[:errors]}
end

#resource_nameObject



12
13
14
# File 'lib/query-interface-client/request.rb', line 12

def resource_name
  self.name.underscore.pluralize
end

#resource_path_fragments(*fragments) ⇒ Object



23
24
25
26
27
28
# File 'lib/query-interface-client/request.rb', line 23

def resource_path_fragments(*fragments)
  unless fragments.empty?
    @_qi_resource_path_fragments = [self.default_api.url] + fragments
  end
  @_qi_resource_path_fragments || self.collection_path_fragments + [:id]
end