Class: Forward::API::Resource

Inherits:
Object
  • Object
show all
Extended by:
Common
Includes:
Common
Defined in:
lib/forward/api/resource.rb

Direct Known Subclasses

Tunnel, User

Defined Under Namespace

Classes: JSONify

Constant Summary collapse

DEFAULT_ERROR_MESSAGE =
"Unable to connect to API, please contact [email protected]".freeze

Constants included from Common

Common::EMAIL_REGEX

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Common

config, exit_with_error, exit_with_message, logged_in?, logger, os, stop_reactor_and_exit, windows?

Constructor Details

#initializeResource

Returns a new instance of Resource.



26
27
28
29
30
# File 'lib/forward/api/resource.rb', line 26

def initialize
  @http = EM::HttpRequest.new(Forward::API.host)

  @http.use JSONify
end

Instance Attribute Details

#httpObject

Returns the value of attribute http.



15
16
17
# File 'lib/forward/api/resource.rb', line 15

def http
  @http
end

#uriObject

Returns the value of attribute uri.



16
17
18
# File 'lib/forward/api/resource.rb', line 16

def uri
  @uri
end

Instance Method Details

#add_body!Object



70
71
72
73
74
75
# File 'lib/forward/api/resource.rb', line 70

def add_body!
  return unless @options.has_key?(:params)

  logger.debug "[API] request params: #{@options[:params].inspect}"
  @options[:body] = @options.delete(:params).to_json
end

#add_head!Object



77
78
79
80
81
82
83
# File 'lib/forward/api/resource.rb', line 77

def add_head!
  @options[:head] ||= {
    'content-type' => 'application/json',
    'accept'       => 'application/json'
  }
  @options[:head]['authorization'] = "Token token=#{config.api_key}" if @options[:authenticated]
end

#delete(options = {}, &block) ⇒ Object



66
67
68
# File 'lib/forward/api/resource.rb', line 66

def delete(options = {}, &block)
  request(:delete, options, &block)
end

#get(options = {}, &block) ⇒ Object



58
59
60
# File 'lib/forward/api/resource.rb', line 58

def get(options = {}, &block)
  request(:get, options, &block)
end

#post(options = {}, &block) ⇒ Object



62
63
64
# File 'lib/forward/api/resource.rb', line 62

def post(options = {}, &block)
  request(:post, options, &block)
end

#request(method = :get, options = {}, &block) ⇒ Object



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
# File 'lib/forward/api/resource.rb', line 32

def request(method = :get, options = {}, &block)
  logger.debug "[API] request: #{method.to_s.upcase} #{@http.uri}#{options[:path]}"
  @options = options || {}

  add_head!
  add_body!

  @_request = @http.send(method, @options)

  @_request.callback {
    status = @_request.response_header.status

    logger.debug "[API] response: #{status} #{@_request.response}"
    if @_request.response.is_a?(String) && !@_request.response.empty?
      # no JSON response
      exit_with_error DEFAULT_ERROR_MESSAGE
    else
      block.call(@_request.response, status)
    end
  }

  @_request.errback {
    exit_with_error DEFAULT_ERROR_MESSAGE
  }
end