Class: Sirportly::Request

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Request.



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

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

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



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

def client
  @client
end

#dataObject

Returns the value of attribute data.



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

def data
  @data
end

#methodObject (readonly)

Returns the value of attribute method.



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

def method
  @method
end

#pathObject (readonly)

Returns the value of attribute path.



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

def path
  @path
end

Class Method Details

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



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

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

Instance Method Details

#makeObject



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
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/sirportly/request.rb', line 27

def make
  uri = URI.parse([domain, "api/v1", @path].join('/'))
  http_request = http_req(uri, @data.stringify_keys)
  http_request.add_field("User-Agent", "SirportlyRubyClient/#{Sirportly::VERSION}")
  http_request.add_field("X-Auth-Token", @client.token)
  http_request.add_field("X-Auth-Secret", @client.secret)
  http_request.add_field("X-Sirportly-Rules", "disabled") if Sirportly.execute_rules == false

  if application
    http_request.add_field("X-Auth-Application", application)
  end

  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)

  if http_result.body == 'true'
    @output = true
  elsif http_result.body == 'false'
    @output = false
  elsif http_result.content_type == 'application/json'
    @output = JSON.parse(http_result.body)
  else
    @output = http_result.body
  end

  @success = case http_result
  when Net::HTTPSuccess
    true
  when Net::HTTPServiceUnavailable
    raise Sirportly::Errors::ServiceUnavailable
  when Net::HTTPForbidden, Net::HTTPUnauthorized
    puts http_result.body.inspect
    raise Sirportly::Errors::AccessDenied, "Access Denied for '#{@client.token}'"
  when Net::HTTPNotFound
    json = JSON.parse(http_result.body)
    raise Sirportly::Errors::NotFound, json['error']
  when Net::HTTPClientError
    json = JSON.parse(http_result.body)
    raise Sirportly::Errors::ValidationError, json['errors'].to_s
  else
    raise Sirportly::Errors::CommunicationError, http_result.body
  end
  self
end

#outputObject



23
24
25
# File 'lib/sirportly/request.rb', line 23

def output
  @output || nil
end

#success?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/sirportly/request.rb', line 19

def success?
  @success || false
end