Class: SqAuth::SqAuthRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/sq_auth/sq_auth_request.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ SqAuthRequest

Returns a new instance of SqAuthRequest.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/sq_auth/sq_auth_request.rb', line 38

def initialize options
  @host = options[:host] || raise("Host not specified")
  @port = options[:port] || 80
  @username = options[:username]
  @password = options[:password]
  path = options[:path]
  absolute_path = "/"
  if path.is_a? Symbol
    absolute_path << path.to_s
  elsif path.is_a? String
    absolute_path << path
  end
  @path = absolute_path
  method = options[:method] || :get
  @https = !!options[:https]
  @method = ::Net::HTTP.const_get(method.to_s.capitalize)
  @uri_builder = @https ? URI::HTTPS : URI::HTTP
end

Class Method Details

.hash_to_query(hash) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/sq_auth/sq_auth_request.rb', line 4

def self.hash_to_query hash
  query = ""
  encode_chars = Regexp.new("[^#{URI::PATTERN::UNRESERVED}]")
  if hash.is_a? Hash
    query = hash.map do |param_name, param_value|
      if param_value.is_a? Array
        param_value.map do |v|
          "#{param_name}[]=#{URI::encode(v.to_s, encode_chars)}"
        end.join("&")
      else
        "#{param_name}=#{URI::encode(param_value.to_s, encode_chars)}"
      end
    end.join("&")
  end
  query
end

.parse_response(response = nil) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/sq_auth/sq_auth_request.rb', line 21

def self.parse_response response = nil
  data = nil
  if response.is_a? Net::HTTPResponse
    data = {}
    data[:status] = response.code
    data[:message] = response.message
    if response.is_a? Net::HTTPOK
      data[:data] = begin
        JSON::load(response.body)
      rescue JSON::ParserError
        response.body
      end
    end
  end
  data
end

Instance Method Details

#get_responseObject



72
73
74
75
76
77
78
# File 'lib/sq_auth/sq_auth_request.rb', line 72

def get_response
  request = @method.new(@uri.request_uri)
  if @username && @password
    request.basic_auth @username, @password
  end
  send_data(request)
end

#request(query = {}) ⇒ Object



66
67
68
69
70
# File 'lib/sq_auth/sq_auth_request.rb', line 66

def request query = {}
  setup_query(query)
  response = get_response
  SqAuthRequest.parse_response response
end

#send_data(request) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/sq_auth/sq_auth_request.rb', line 80

def send_data request
  if @method == Net::HTTP::Post
    request.body = @query
    request.content_length = request.body.bytesize
  end
  @http.request(request) rescue nil
end

#setup_query(query_hash) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/sq_auth/sq_auth_request.rb', line 57

def setup_query query_hash
  @query = SqAuthRequest.hash_to_query(query_hash)
  @uri = @uri_builder.build(host: @host, port: @port, path: @path, query: (@method == Net::HTTP::Get)? @query: nil)
  @http = Net::HTTP.new(@uri.host, @uri.port)
  @http.use_ssl = @https
  @http.verify_mode = OpenSSL::SSL::VERIFY_NONE if @https

end