Class: VoxcastApi::Http

Inherits:
Object
  • Object
show all
Includes:
UrlMaker
Defined in:
lib/voxcast_api/http.rb

Direct Known Subclasses

Base, Version0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(entry = nil) ⇒ Http

Returns a new instance of Http.



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/voxcast_api/http.rb', line 5

def initialize(entry = nil)
  @entry = entry
  if @entry
    @url = entry.purge_url
    @user = entry.purge_user
    @password = entry.purge_password
    @logger = entry.logger
  else
    @logger = ActiveRecord::Base.logger
  end
end

Instance Attribute Details

#passwordObject

Returns the value of attribute password.



3
4
5
# File 'lib/voxcast_api/http.rb', line 3

def password
  @password
end

#urlObject

Returns the value of attribute url.



3
4
5
# File 'lib/voxcast_api/http.rb', line 3

def url
  @url
end

#userObject

Returns the value of attribute user.



3
4
5
# File 'lib/voxcast_api/http.rb', line 3

def user
  @user
end

Instance Method Details

#execute(url, params = {}) ⇒ Object



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
# File 'lib/voxcast_api/http.rb', line 50

def execute(url, params = {})
  ret = get_http(url)
  return ret if ret.is_a?(Hash)
  skip_process = params.delete(:skip_process)
  complex_parser = params.delete(:complex_parser)
  ret.start do |http|
    path, data = get_path_and_params(params, url)
    user, pass = get_basic_auth(params)
    if !user.blank? && !pass.blank?
      if get_method(params) == :get
        req = Net::HTTP::Get.new(path)
      else
        req = Net::HTTP::Post.new(path)
        req.set_form_data(data)
      end
      req.basic_auth(user, pass)
      resp = http.request(req)
    else
      data = data.map {|key, val| "#{CGI.escape(key.to_s)}=#{CGI.escape(val.to_s)}"}.join("&") if data.is_a?(Hash)
      resp = http.post(path, data)
    end
    return process_response(resp, complex_parser == true) if skip_process != true
    resp
  end
end

#get_basic_auth(params = {}) ⇒ Object



21
22
23
# File 'lib/voxcast_api/http.rb', line 21

def get_basic_auth(params = {})
  [nil, nil]
end

#get_http(url = nil) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/voxcast_api/http.rb', line 30

def get_http(url = nil)
  url ||= @url
  return {:status => false, :detail => 'no purge url specified'} if url.blank?
  uri = URI.parse(url)
  if uri.scheme == 'https'
    port = 443
    use_ssl = true
  else
    port = uri.port
    use_ssl = false
  end
  http = Net::HTTP.new(uri.host, port)
  http.use_ssl = use_ssl
  http
end

#get_method(params) ⇒ Object



46
47
48
# File 'lib/voxcast_api/http.rb', line 46

def get_method(params)
  :post
end

#get_path_and_params(params = {}) ⇒ Object



25
26
27
28
# File 'lib/voxcast_api/http.rb', line 25

def get_path_and_params(params = {})
  uri = URI.parse(@url)
  [uri.path, params]
end

#timestampObject



17
18
19
# File 'lib/voxcast_api/http.rb', line 17

def timestamp
  Time.now.iso8601
end