Class: Nsrr::Helpers::JsonRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/nsrr/helpers/json_request.rb

Overview

Aids in generating JSON requests for GET, POST, and PATCH.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, args = {}) ⇒ JsonRequest

Returns a new instance of JsonRequest.



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/nsrr/helpers/json_request.rb', line 30

def initialize(url, args = {})
  @params = nested_hash_to_params(args)
  @url = URI.parse(url)

  @http = Net::HTTP.new(@url.host, @url.port)
  if @url.scheme == "https"
    @http.use_ssl = true
    @http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end
rescue
  @error = "Invalid URL: #{url.inspect}"
  puts @error.red
end

Instance Attribute Details

#urlObject (readonly)

Returns the value of attribute url.



28
29
30
# File 'lib/nsrr/helpers/json_request.rb', line 28

def url
  @url
end

Class Method Details

.get(url, *args) ⇒ Object



15
16
17
# File 'lib/nsrr/helpers/json_request.rb', line 15

def get(url, *args)
  new(url, *args).get
end

.patch(url, *args) ⇒ Object



23
24
25
# File 'lib/nsrr/helpers/json_request.rb', line 23

def patch(url, *args)
  new(url, *args).patch
end

.post(url, *args) ⇒ Object



19
20
21
# File 'lib/nsrr/helpers/json_request.rb', line 19

def post(url, *args)
  new(url, *args).post
end

Instance Method Details

#getObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/nsrr/helpers/json_request.rb', line 44

def get
  return unless @error.nil?

  full_path = @url.path
  query = ([@url.query] + @params).flatten.compact.join("&")
  full_path += "?#{query}" if query.to_s != ""
  response = @http.start do |http|
    http.get(full_path)
  end
  if response.body
    [JSON.parse(response.body), response]
  else
    [nil, response]
  end
rescue => e # JSON::ParserError, Net::ReadTimeout
  puts "GET error: #{e}".red
  [nil, nil]
end

#key_value_to_string(key, value, scope = nil) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/nsrr/helpers/json_request.rb', line 90

def key_value_to_string(key, value, scope = nil)
  current_scope = (scope ? "#{scope}[#{key}]" : key)
  if value.is_a? Hash
    value.collect do |k,v|
      key_value_to_string(k, v, current_scope)
    end.join("&")
  elsif value.is_a? Array
    value.collect do |v|
      key_value_to_string("", v, current_scope)
    end
  else
    "#{current_scope}=#{CGI.escape(value.to_s)}"
  end
end

#nested_hash_to_params(args) ⇒ Object



84
85
86
87
88
# File 'lib/nsrr/helpers/json_request.rb', line 84

def nested_hash_to_params(args)
  args.collect do |key, value|
    key_value_to_string(key, value, nil)
  end
end

#patchObject



79
80
81
82
# File 'lib/nsrr/helpers/json_request.rb', line 79

def patch
  @params << "_method=patch"
  post
end

#postObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/nsrr/helpers/json_request.rb', line 63

def post
  return unless @error.nil?

  response = @http.start do |http|
    http.post(@url.path, @params.flatten.compact.join("&"))
  end
  if response.body
    [JSON.parse(response.body), response]
  else
    [nil, response]
  end
rescue => e # JSON::ParserError, Net::ReadTimeout
  puts "POST error: #{e}".red
  [nil, nil]
end