Module: Gfspark::Connection

Included in:
App
Defined in:
lib/gfspark/connection.rb

Instance Method Summary collapse

Instance Method Details

#connection(host, port) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'lib/gfspark/connection.rb', line 5

def connection(host, port)
  env = ENV['http_proxy'] || ENV['HTTP_PROXY']
  if env
    uri = URI(env)
    proxy_host, proxy_port = uri.host, uri.port
    Net::HTTP::Proxy(proxy_host, proxy_port).new(host, port)
  else
    Net::HTTP.new(host, port)
  end
end

#fetch_json(url, options = {}, params = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/gfspark/connection.rb', line 16

def fetch_json(url, options = {}, params = {})
  response = send_request(url, {},options, params, :get)
  raise "#{response.code} #{response.msg}" unless response_success?(response)
  json = JSON.parse(response.body)

  if @debug
    puts '-' * 80
    puts url
    pp json
    puts '-' * 80
  end

  json
end

#response_success?(response) ⇒ Boolean

Returns:

  • (Boolean)


92
93
94
95
# File 'lib/gfspark/connection.rb', line 92

def response_success?(response)
  code = response.code.to_i
  code >= 200 && code < 300
end

#send_request(url, json = {}, options = {}, params = {}, method = :post) ⇒ Object



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
81
82
83
84
85
86
87
88
89
90
# File 'lib/gfspark/connection.rb', line 31

def send_request(url, json = {}, options = {}, params = {}, method = :post)
  url = "#{url}"
  uri = URI.parse(url)

  if @debug
    puts '-' * 80
    puts url
    pp json
    puts '-' * 80
  end

  https = connection(uri.host, uri.port)

  https.use_ssl = true if uri.scheme == 'https'
  https.verify_mode = @ssl_options[:ssl_verify_mode] || OpenSSL::SSL::VERIFY_NONE

  store = OpenSSL::X509::Store.new
  unless @ssl_options[:ssl_ca_cert].nil?
    if File.directory? @ssl_options[:ssl_ca_cert]
      store.add_path @ssl_options[:ssl_ca_cert]
    else
      store.add_file @ssl_options[:ssl_ca_cert]
    end
    http.cert_store = store
  else
    store.set_default_paths
  end
  https.cert_store = store

  https.set_debug_output $stderr if @debug && https.respond_to?(:set_debug_output)

  https.start{|http|

    path = "#{uri.path}"
    path += "?" + params.map{|k,v| "#{k}=#{v}"}.join("&") unless params.empty?

    request = case method
      when :post then Net::HTTP::Post.new(path)
      when :put  then Net::HTTP::Put.new(path)
      when :get  then Net::HTTP::Get.new(path)
      else raise "unknown method #{method}"
    end

    # add basic auth header...
    if @options[:username] && @options[:password]
      request.basic_auth @options[:username], @options[:password]
    end

    request.set_content_type("application/json")
    request.body = json.to_json unless json.nil?

    response = http.request(request)
    if @debug
      puts "#{response.code}: #{response.msg}"
      puts response.body
    end

    response
  }
end