Class: ProximityBeacon::Client::Request

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/proximity_beacon/client/request.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method, uri, credentials = nil, params = nil) {|_self| ... } ⇒ Request

Returns a new instance of Request.

Yields:

  • (_self)

Yield Parameters:



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/proximity_beacon/client/request.rb', line 33

def initialize(method, uri, credentials = nil, params = nil)
  self.method = method
  if params
    url_params = params.map {|k,v| "#{k}=#{v}"}.join("&")
    self.uri = URI(uri + "?#{url_params}")
  else
    self.uri = uri
  end
  self.credentials = credentials
  yield self if block_given?
end

Instance Attribute Details

#credentialsObject

Returns the value of attribute credentials.



10
11
12
# File 'lib/proximity_beacon/client/request.rb', line 10

def credentials
  @credentials
end

#methodObject

Returns the value of attribute method.



10
11
12
# File 'lib/proximity_beacon/client/request.rb', line 10

def method
  @method
end

#uriObject

Returns the value of attribute uri.



10
11
12
# File 'lib/proximity_beacon/client/request.rb', line 10

def uri
  @uri
end

Class Method Details

.delete(uri, credentials = nil, params = nil) ⇒ Object



28
29
30
31
# File 'lib/proximity_beacon/client/request.rb', line 28

def self.delete(uri, credentials = nil, params = nil)
  r = self.new(:delete, uri, credentials, params)
  r.perform {|req| yield req if block_given? }
end

.get(uri, credentials = nil, params = nil) ⇒ Object



13
14
15
16
# File 'lib/proximity_beacon/client/request.rb', line 13

def self.get(uri, credentials = nil, params = nil)
  r = self.new(:get, uri, credentials, params)
  r.perform {|req| yield req if block_given? }
end

.post(uri, credentials = nil, params = nil) ⇒ Object



18
19
20
21
# File 'lib/proximity_beacon/client/request.rb', line 18

def self.post(uri, credentials = nil, params = nil)
  r = self.new(:post, uri, credentials, params)
  r.perform {|req| yield req if block_given? }
end

.put(uri, credentials = nil, params = nil) ⇒ Object



23
24
25
26
# File 'lib/proximity_beacon/client/request.rb', line 23

def self.put(uri, credentials = nil, params = nil)
  r = self.new(:put, uri, credentials, params)
  r.perform {|req| yield req if block_given? }
end

Instance Method Details

#performObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/proximity_beacon/client/request.rb', line 45

def perform
  http_opts = {use_ssl: true}
  response = Net::HTTP.start(uri.host, uri.port, http_opts) do |http|
    add_field "Authorization", "Bearer #{credentials.access_token}" if credentials
    add_field "Accept", "application/json"
    yield self if block_given?
    http.request request
  end
  if (200..299).include?(response.code.to_i)
    return response
  else
    raise RequestError.new(response.code.to_i), "Error #{response.code} (#{response.msg}) - #{uri}\n#{response.body}"
  end
end