Class: Homeflow::API::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/homeflow/api/request.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request_specification) ⇒ Request

Returns a new instance of Request.



8
9
10
# File 'lib/homeflow/api/request.rb', line 8

def initialize(request_specification)
  @request_specification = request_specification
end

Instance Attribute Details

#request_specificationObject

Returns the value of attribute request_specification.



6
7
8
# File 'lib/homeflow/api/request.rb', line 6

def request_specification
  @request_specification
end

#resource_classObject

Returns the value of attribute resource_class.



6
7
8
# File 'lib/homeflow/api/request.rb', line 6

def resource_class
  @resource_class
end

Class Method Details

.run_for(request_specification) ⇒ Object



58
59
60
61
# File 'lib/homeflow/api/request.rb', line 58

def run_for(request_specification)
  r = Request.new(request_specification)
  Response.new_from_json(r.perform)
end

Instance Method Details

#constant_paramsObject



53
54
55
# File 'lib/homeflow/api/request.rb', line 53

def constant_params
  {:api_key=> Homeflow::API.config.api_key}
end

#performObject



12
13
14
15
16
17
18
19
# File 'lib/homeflow/api/request.rb', line 12

def perform
  begin
    response = perform_request
  rescue Errno::ECONNREFUSED => e
    raise Homeflow::API::Exceptions::APIConnectionError, "Connection error. Homeflow might be down?"
  end
  response
end

#perform_requestObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/homeflow/api/request.rb', line 21

def perform_request
  if request_specification.is_a? Query
    url = "#{Homeflow::API.config.source}/#{request_specification.resource_class.resource_uri}"
  else
    url = "#{Homeflow::API.config.source}/#{request_specification.resource_uri}"
  end
  query_params = @request_specification.to_params.merge(constant_params)
  post_params = (@request_specification.respond_to?(:post_params) ? @request_specification.post_params : {})
  if Homeflow::API.config.show_debug && Homeflow::API.configuration.logger
    log_line = [] 
    log_line << "Destination - #{url}"
    log_line << "Request params:\n#{query_params.to_json}\n"
    log_line << "Post params:\n#{post_params.to_json}\n"
    log_line << "request_specification:\n#{request_specification.to_json}\n"
    log_line << "@request_specification:\n#{@request_specification.to_json}\n"
    Homeflow::API.configuration.logger.info(log_line.join("\n"))
  end

  if request_specification.is_a? Query
    return (HTTParty.get(url, :query => query_params)).body
  elsif request_specification.is_a? ResourceIdentifier
    return (HTTParty.get(url, :query => query_params)).body
  elsif request_specification.is_a? Delete
    return (HTTParty.delete(url, :query => query_params)).body
  elsif request_specification.is_a? Put
    return (HTTParty.put(url, :query => query_params, :body => post_params)).body
  elsif request_specification.is_a? Post
    return (HTTParty.pos(url, :query => query_params, :body => post_params)).body
  end
end