Class: Flatpack::Client::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/flatpack/client/request.rb

Direct Known Subclasses

FlatpackRequest, JsonRequest

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api, method, path, *args) ⇒ Request

Returns a new instance of Request.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/flatpack/client/request.rb', line 10

def initialize(api, method, path, *args)
  @api = api
  @method = method.downcase
    
  # replace path paramters
  @path = path
  @path.scan(/[{][^}]*[}]/).each_with_index do |match, i|
    @path.gsub!(match, args[i])
  end
  
  # define default headers
  @headers = {
    'Content-Type' => 'application/json'
  }
  @headers.merge!(@api.auth_headers)
  
  # and query parameters
  @query_params = {}
end

Instance Attribute Details

#payloadObject

Returns the value of attribute payload.



8
9
10
# File 'lib/flatpack/client/request.rb', line 8

def payload
  @payload
end

Instance Method Details

#executeObject

Execute the request.



31
32
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
# File 'lib/flatpack/client/request.rb', line 31

def execute
  
  # ensure our api session is still active
  @api.refresh_session unless @api.session_active?
  
  # build our URI
  uri = URI.parse(@api.server_base.to_s)
  uri.path = @path
  unless(@query_params.empty?) 
    uri.query = @query_params.map{ |key, value| "#{key}=#{URI::encode(value.to_s)}" }.join('&')
  end
  
  # build our request
  req = Net::HTTP.const_get(@method.capitalize).new(uri.request_uri)
  payload = self.payload
  if(payload and payload.length > 0)
    puts "payload is #{payload}" if @api.verbose
    req.body = payload 
  end
  
  # add the headers
  @headers.each do |key, value|
    req.add_field(key, value)
  end
  
  # log it
  if(@api.verbose)
    puts "requesting #{@method} to #{uri.to_s} with headers #{@headers.inspect}"
  end
  
  # and send it off
  http = Net::HTTP.new(uri.host, uri.port)
  if(uri.scheme == 'https')
    # TODO allow the certificate options to be specified 
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    http.use_ssl = true
  end
  response = http.start { |h| h.request(req) }
    
  # log our response
  if(@api.verbose)
    puts "response #{response.code} with payload #{response.body}"
  end
  
  process_response(response)
end

#header(name, value) ⇒ Object

Add a custom header to the request.



79
80
81
82
# File 'lib/flatpack/client/request.rb', line 79

def header(name, value)
  @headers[name] = value
  self
end

#query_parameter(name, value) ⇒ Object

Add a custom query parameter to the request. The generated subclasses of Request will have convenience methods for setting defined query parameters.



87
88
89
90
# File 'lib/flatpack/client/request.rb', line 87

def query_parameter(name, value)
  @query_params[name] = value
  self
end