Class: AppBase

Inherits:
Object
  • Object
show all
Defined in:
lib/lime_light_platform/app_base.rb

Direct Known Subclasses

LimeLightPlatform

Constant Summary collapse

GET =

map protocols

'GET'
POST =
'POST'
PATCH =
'PATCH'
PUT =
'PUT'
BODY_F_JSON =
'JSON'
BODY_F_QUERY_STR =
'HTTP_QUERY_STR'
BODY_F_XML =
'XML'
BODY_F_RAW =
'RAW'
BODY_F_NONE =
'NO_BODY'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(debug = false) ⇒ AppBase

Returns a new instance of AppBase.



19
20
21
# File 'lib/lime_light_platform/app_base.rb', line 19

def initialize debug=false
  @debug = debug
end

Instance Attribute Details

#debugObject

Returns the value of attribute debug.



17
18
19
# File 'lib/lime_light_platform/app_base.rb', line 17

def debug
  @debug
end

Instance Method Details

#gen_http(uri) ⇒ Object



82
83
84
85
86
87
# File 'lib/lime_light_platform/app_base.rb', line 82

def gen_http uri
  http             = Net::HTTP.new uri.host, uri.port
  http.use_ssl     = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  http
end

#perform_api_call(call_params) ⇒ Object



23
24
25
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/lime_light_platform/app_base.rb', line 23

def perform_api_call call_params
  valid         = true
  response_body = ''
  uri           = URI.parse call_params[:url]
  http          = gen_http uri

  # determine http protocol
  case call_params[:proto]
  when POST
    api_request = Net::HTTP::Post.new uri.request_uri
  when PATCH
    api_request = Net::HTTP::Patch.new uri.request_uri
  when GET
    api_request = Net::HTTP::Get.new uri.request_uri
  when PUT
    api_request = Net::HTTP::Put.new uri.request_uri
  else
    valid = false
  end

  if valid
    # determine headers if any
    if !call_params[:headers].nil?
      call_params[:headers].each do |field|
        api_request.add_field field[:name], field[:value]
      end
    end

    # add request body
    case call_params[:format]
    when BODY_F_JSON
      api_request.body = call_params[:body].to_json
    when BODY_F_XML
      api_request.body = call_params[:body].to_xml(:root => call_params[:xml_root])
    when BODY_F_QUERY_STR
      api_request.body = call_params[:body].to_query
    when BODY_F_RAW
      api_request.body = call_params[:body]
    when BODY_F_NONE
      api_request.body = ''
    end

    if @debug
      ap "API Request =>"
      ap api_request.body
    end

    api_response = http.request api_request
    response_body = api_response.body

    if @debug
      ap "API Response =>"
      ap response_body
    end
  end

  response_body
end