Class: Neko::HTTP

Inherits:
Object
  • Object
show all
Defined in:
lib/firebase-ruby/neko-http.rb

Constant Summary collapse

METHOD_HTTP_CLASS =
{
  get: Net::HTTP::Get,
  put: Net::HTTP::Put,
  patch: Net::HTTP::Patch,
  post: Net::HTTP::Post,
  delete: Net::HTTP::Delete
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, hdrs = nil) ⇒ HTTP

Instance constructor for tailored use

Parameters:

  • url (String)

    full URL string

  • hdrs (Hash) (defaults to: nil)

    HTTP headers

Raises:

  • (ArgumentError)


80
81
82
83
84
85
86
87
88
# File 'lib/firebase-ruby/neko-http.rb', line 80

def initialize(url, hdrs = nil)
  @logger = Neko.logger
  @init_uri = URI(url)
  raise ArgumentError, 'Invalid URL' unless @init_uri.class <= URI::HTTP
  @http = Net::HTTP.new(init_uri.host, init_uri.port)
  http.use_ssl = init_uri.scheme == 'https'
  http.verify_mode = OpenSSL::SSL::VERIFY_PEER
  @headers = hdrs
end

Instance Attribute Details

#headersObject

Returns the value of attribute headers.



75
76
77
# File 'lib/firebase-ruby/neko-http.rb', line 75

def headers
  @headers
end

#httpObject (readonly)

Returns the value of attribute http.



74
75
76
# File 'lib/firebase-ruby/neko-http.rb', line 74

def http
  @http
end

#init_uriObject (readonly)

Returns the value of attribute init_uri.



74
75
76
# File 'lib/firebase-ruby/neko-http.rb', line 74

def init_uri
  @init_uri
end

#loggerObject

Returns the value of attribute logger.



75
76
77
# File 'lib/firebase-ruby/neko-http.rb', line 75

def logger
  @logger
end

Class Method Details

.get(url, params, hdrs = nil) ⇒ Hash

Simple GET request

Parameters:

  • url (String)

    full URL string

  • params (Array, Hash)

    it will be converted to URL encoded query

  • hdrs (Hash) (defaults to: nil)

    HTTP headers

Returns:

  • (Hash)

    contains: :code, :headers, :body, :message



33
34
35
36
37
38
# File 'lib/firebase-ruby/neko-http.rb', line 33

def self.get(url, params, hdrs = nil)
  h = HTTP.new(url, hdrs)
  data = h.get(params: params)
  h.close
  return data
end

.post_form(url, params, hdrs = nil) ⇒ Object

Send POST request with form data URL encoded body

Parameters:

  • url (String)

    full URL string

  • params (Array, Hash)

    it will be converted to URL encoded body

  • hdrs (Hash) (defaults to: nil)

    HTTP headers



45
46
47
48
49
50
# File 'lib/firebase-ruby/neko-http.rb', line 45

def self.post_form(url, params, hdrs = nil)
  h = HTTP.new(url, hdrs)
  data = h.post(params: params)
  h.close
  return data
end

.post_json(url, obj, hdrs = {}) ⇒ Object

Send POST request with JSON body It will set the Content-Type to application/json.

Parameters:

  • url (String)

    full URL string

  • obj (Array, Hash, String)

    Array/Hash will be converted to JSON

  • hdrs (Hash) (defaults to: {})

    HTTP headers



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/firebase-ruby/neko-http.rb', line 58

def self.post_json(url, obj, hdrs = {})
  hdrs['Content-Type'] = 'application/json'
  h = HTTP.new(url, hdrs)
  case obj
  when Array, Hash
    body = JSON.fast_generate(obj)
  when String
    body = obj
  else
    raise ArgumentError, 'Argument is neither Array, Hash, String'
  end
  data = h.post(body: body)
  h.close
  return data
end

Instance Method Details

#closeObject



110
111
112
# File 'lib/firebase-ruby/neko-http.rb', line 110

def close
  http.finish if http.started?
end

#delete(path: nil, params: nil, query: nil) ⇒ Object



106
107
108
# File 'lib/firebase-ruby/neko-http.rb', line 106

def delete(path: nil, params: nil, query: nil)
  return operate(__method__, path: path, params: params, query: query)
end

#get(path: nil, params: nil, query: nil) ⇒ Object



90
91
92
# File 'lib/firebase-ruby/neko-http.rb', line 90

def get(path: nil, params: nil, query: nil)
  return operate(__method__, path: path, params: params, query: query)
end

#patch(path: nil, params: nil, body: nil, query: nil) ⇒ Object



102
103
104
# File 'lib/firebase-ruby/neko-http.rb', line 102

def patch(path: nil, params: nil, body: nil, query: nil)
  return operate(__method__, path: path, params: params, body: body, query: query)
end

#post(path: nil, params: nil, body: nil, query: nil) ⇒ Object



94
95
96
# File 'lib/firebase-ruby/neko-http.rb', line 94

def post(path: nil, params: nil, body: nil, query: nil)
  return operate(__method__, path: path, params: params, body: body, query: query)
end

#put(path: nil, params: nil, body: nil, query: nil) ⇒ Object



98
99
100
# File 'lib/firebase-ruby/neko-http.rb', line 98

def put(path: nil, params: nil, body: nil, query: nil)
  return operate(__method__, path: path, params: params, body: body, query: query)
end