Class: Noti::Request

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, method = :get) ⇒ Request

Returns a new instance of Request.



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

def initialize(path, method = :get)
  @path = path
  @method = method
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



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

def client
  @client
end

#dataObject

Returns the value of attribute data.



11
12
13
# File 'lib/noti/request.rb', line 11

def data
  @data
end

#methodObject (readonly)

Returns the value of attribute method.



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

def method
  @method
end

#pathObject (readonly)

Returns the value of attribute path.



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

def path
  @path
end

Class Method Details

.request(path, data = {}) ⇒ Object



4
5
6
7
8
# File 'lib/noti/request.rb', line 4

def self.request(path, data = {})
  req = self.new(path, :post)
  req.data = data
  req.make && req.success? ? req.output : false
end

Instance Method Details

#makeObject



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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/noti/request.rb', line 26

def make
  uri = URI.parse(["https://notiapp.com", "api/v1", @path].join('/'))
  http_request = http_class.new(uri.request_uri)
  http_request.initialize_http_header({"User-Agent" => "NotiRubyClient/#{Noti::VERSION}"})
  http_request.content_type = 'application/json'

  http = Net::HTTP.new(uri.host, uri.port)

  if uri.scheme == 'https'
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end

  http_result = http.request(http_request, @data.to_json)
  if http_result.body == 'true'
    @output = true
  elsif http_result.body == 'false'
    @output = false
  else
    @output = JSON.parse(http_result.body)
  end
  @success = case http_result
  when Net::HTTPSuccess
    true
  when Net::HTTPServiceUnavailable
    raise Noti::Errors::ServiceUnavailable
  when Net::HTTPForbidden, Net::HTTPUnauthorized
    raise Noti::Errors::AccessDenied, "Access Denied for '#{Noti.app}'"
  when Net::HTTPNotFound
    json = JSON.parse(http_result.body)
    raise Noti::Errors::NotFound, json['error']
  when Net::HTTPClientError
    json = JSON.parse(http_result.body)
    raise Noti::Errors::ValidationError, json.to_s
  else
    raise Noti::Errors::CommunicationError, http_result.body
  end
  self
end

#outputObject



22
23
24
# File 'lib/noti/request.rb', line 22

def output
  @output || nil
end

#success?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/noti/request.rb', line 18

def success?
  @success || false
end