Class: Splitwise::Request
- Inherits:
-
Object
- Object
- Splitwise::Request
- Defined in:
- lib/splitwise/client.rb
Instance Attribute Summary collapse
-
#uri ⇒ Object
Returns the value of attribute uri.
Instance Method Summary collapse
- #fetch(params) ⇒ Object
- #get_path(params) ⇒ Object
-
#initialize ⇒ Request
constructor
A new instance of Request.
- #parse_response(response) ⇒ Object
- #request(req_method, uri, post_data = nil) ⇒ Object
- #update(params, data) ⇒ Object
- #valid_response_code?(code) ⇒ Boolean
Constructor Details
#initialize ⇒ Request
Returns a new instance of Request.
8 9 10 11 |
# File 'lib/splitwise/client.rb', line 8 def initialize @uri = Splitwise::BASE_URL+ "/" + Splitwise::API_VERSION + "/" @accessor_token = Splitwise.access_token end |
Instance Attribute Details
#uri ⇒ Object
Returns the value of attribute uri.
6 7 8 |
# File 'lib/splitwise/client.rb', line 6 def uri @uri end |
Instance Method Details
#fetch(params) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/splitwise/client.rb', line 13 def fetch(params) url = get_path(params) if @accessor_token.nil? url = URI.parse(url) response = request(:get, url) else response = request(:get, url) end parse_response(response) response.body end |
#get_path(params) ⇒ Object
32 33 34 |
# File 'lib/splitwise/client.rb', line 32 def get_path(params) url = @uri + params end |
#parse_response(response) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/splitwise/client.rb', line 60 def parse_response(response) response_code = response.code.to_i if valid_response_code?(response_code) begin JSON.parse(response.body) rescue JSON::ParserError => e raise Splitwise::InvalidJSONResponse(response.body) end else error_class = Splitwise::Error.codes[response_code] || Splitwise::UnknownError raise error_class.new(response.body, response_code) end end |
#request(req_method, uri, post_data = nil) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/splitwise/client.rb', line 36 def request(req_method, uri, post_data=nil) if @accessor_token.nil? http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = (uri.scheme == "https") if http.use_ssl? http.verify_mode = OpenSSL::SSL::VERIFY_PEER else http.verify_mode = OpenSSL::SSL::VERIFY_NONE end if req_method == :get request = Net::HTTP::Get.new(uri) end response = http.request(request) else if req_method == :get response = @accessor_token.get(uri) else header = {'Content-Type' => 'text/json'} response = @accessor_token.post(uri, post_data, header) end end response end |
#update(params, data) ⇒ Object
25 26 27 28 29 30 |
# File 'lib/splitwise/client.rb', line 25 def update(params, data) url = get_path(params) response = request(:post, url, data) parse_response(response) response.body end |
#valid_response_code?(code) ⇒ Boolean
74 75 76 |
# File 'lib/splitwise/client.rb', line 74 def valid_response_code?(code) [200, 201].include?(code) end |