Class: Boxxspring::Request

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

Instance Method Summary collapse

Constructor Details

#initialize(default_parameters = {}) ⇒ Request

Returns a new instance of Request.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/boxxspring/request.rb', line 8

def initialize( default_parameters = {} )
  
  # parse the API uri
  uri = URI.parse( Boxxspring.configuration.api_uri )

  # construct http request
  @http = Net::HTTP.new( uri.host, uri.port )
  
  # use ssl when https is the uri scheme                       
  @http.use_ssl = ( uri.scheme == 'https' )

  # retain the default parameters 
  @default_parameters = default_parameters.stringify_keys
  
end

Instance Method Details

#destroy(path, parameters = {}) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/boxxspring/request.rb', line 24

def destroy( path, parameters = {} )

  begin  
           
    request = Net::HTTP::Delete.new( 
      compose_request_path( path, parameters ), 
      { 'Content-Type' =>'application/json' }
    )

    response = Response.new( @http.request( request ) )
    
  rescue Timeout::Error
    response = nil
  end

  response

end

#get(path, parameters = {}) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/boxxspring/request.rb', line 43

def get( path, parameters = {} )
  
  response = nil
  
  begin  
    response = Response.new( 
      @http.get( compose_request_path( path, parameters ) ) 
    )
  rescue Timeout::Error
    response = nil
  end
    
  response
                  
end

#post(path, parameters = {}, body = {}) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/boxxspring/request.rb', line 59

def post( path, parameters = {}, body = {} )
  
  response = nil
  
  begin  
           
    request = Net::HTTP::Post.new( 
      compose_request_path( path, parameters ), 
      { 'Content-Type' =>'application/json' }
    )
    request.body = body.to_json

    response = Response.new( @http.request( request ) )
    
  rescue Timeout::Error
    response = nil
  end
    
  response
                  
end