Class: Puzzle::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/puzzle/request.rb

Class Method Summary collapse

Class Method Details

.delete(path, options = {}) ⇒ Object



10
11
12
13
14
15
# File 'lib/puzzle/request.rb', line 10

def self.delete(path, options = {})
  options[:token] ||= Puzzle.configuration.token

  response = request.delete(uri(path, options))
  response.code.to_i == 200
end

.get(path, options = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/puzzle/request.rb', line 17

def self.get(path, options = {})
  token = Puzzle.configuration.token
  format = Puzzle.configuration.format

  unless token.nil?
    options[:token] ||= token
  end

  response = request.get(uri(path, options))

  case response.code.to_i
  when 200
    case format
    when "json"
      JSON.parse(response.body)
    when "xml"
      # TODO: Support XML format  
    end
  else
    handle_error(response.body, format)
  end
end

.handle_error(error_body, format = "json") ⇒ Object



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
91
92
93
94
95
96
97
98
99
# File 'lib/puzzle/request.rb', line 64

def self.handle_error(error_body, format = "json")
  error = parse_error(error_body, format)
  case error.http_status_code
  when 400
    raise Puzzle::Exception::ParamError, error.message
  when 403
    case error.code
    when Puzzle::ErrorCode::LOGIN_FAIL
      raise Puzzle::Exception::LoginFail, error.message
    when Puzzle::ErrorCode::TOKEN_FAIL
      raise Puzzle::Exception::TokenFail, error.message
    end
  when 404
    raise Puzzle::Exception::ContactNotExist, error.message
  when 405
    case error.code
    when Puzzle::ErrorCode::CONTACT_NOT_OWNED
      raise Puzzle::Exception::ContactNotOwned, error.message
    when Puzzle::ErrorCode::PURCHASE_LOW_POINTS
      raise Puzzle::Exception::PurchaseLowPoints, error.message
    end
  when 500
    case error.code
    when Puzzle::ErrorCode::SEARCH_ERROR
      raise Puzzle::Exception::SearchError, error.message
    when Puzzle::ErrorCode::SYS_ERROR
      raise Puzzle::Exception::SysError, error.message
    end
  when 501
    raise Puzzle::Exception::NotImplemented, error.message
  when 503
    raise Puzzle::Exception::NotAvailable, error.message
  else
    raise StandardError, "Unknown Error"
  end
end

.hash_to_query_string(hash) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/puzzle/request.rb', line 54

def self.hash_to_query_string(hash)
  hash.sort_by { |key, value|
    key.to_s
  }.delete_if { |key, value|
    value.to_s.empty?
  }.collect { |key, value|
    "#{CGI.escape(key.to_s).gsub(/%(5B|5D)/n) { [$1].pack("H*") }}=#{CGI.escape(value.to_s)}"
  }.join("&")
end

.parse_error(error_data, format = "json") ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/puzzle/request.rb', line 101

def self.parse_error(error_data, format = "json")
  error = nil
  case format
  when "json"
    parsed_error = JSON.parse(error_data)
    if parsed_error && parsed_error.length > 0
      error = Error.new(parsed_error[0])
    end
  when "xml"
    # TODO: Add xml support
  end
  error
end

.requestObject



40
41
42
43
44
45
46
47
# File 'lib/puzzle/request.rb', line 40

def self.request
  http = Net::HTTP.new(Puzzle.configuration.host, Puzzle.configuration.port)
  http.verify_mode = Puzzle.configuration.verify_mode
  http.ca_path = Puzzle.configuration.ca_path
  http.ca_file = Puzzle.configuration.ca_file
  http.use_ssl = true
  http
end

.uri(path, options = {}) ⇒ Object



49
50
51
52
# File 'lib/puzzle/request.rb', line 49

def self.uri(path, options = {})
  format = Puzzle.configuration.format
  "/rest#{path}.#{format}?#{hash_to_query_string(options)}"
end