Module: JsonApi::InstanceMethods
- Defined in:
- lib/json-api.rb
Instance Method Summary collapse
- #configure_request(req) ⇒ Object
- #configure_response(res) ⇒ Object
- #error(res) ⇒ Object
- #full_path(path) ⇒ Object
- #http(uri) ⇒ Object
- #log(method, path, params, res) ⇒ Object
- #merged_params(params) ⇒ Object
- #method_missing(name, *args) ⇒ Object
- #req(method, uri, params) ⇒ Object
- #request(method, path, params = {}) ⇒ Object
- #uri(path, params) ⇒ Object
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
12 13 14 15 16 17 18 |
# File 'lib/json-api.rb', line 12 def method_missing(name, *args) if [:get, :post, :delete, :put].include? name request(name, *args) else super end end |
Instance Method Details
#configure_request(req) ⇒ Object
86 87 |
# File 'lib/json-api.rb', line 86 def configure_request(req) end |
#configure_response(res) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/json-api.rb', line 66 def configure_response(res) res.instance_variable_set(:@json_api, self) def res.ok? code == '200' end def res.hash @hash ||= JSON.parse(body) rescue {} end def res.json @json ||= JSON.pretty_generate(hash) end def res.error @json_api.error(self) end end |
#error(res) ⇒ Object
107 108 109 |
# File 'lib/json-api.rb', line 107 def error(res) res. end |
#full_path(path) ⇒ Object
40 41 42 |
# File 'lib/json-api.rb', line 40 def full_path(path) (path.include?('//') or @base_path.nil?) ? path : "#{@base_path}/#{path}" end |
#http(uri) ⇒ Object
60 61 62 63 64 |
# File 'lib/json-api.rb', line 60 def http(uri) http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = uri.scheme == 'https' http end |
#log(method, path, params, res) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/json-api.rb', line 89 def log(method, path, params, res) return unless @logger @logger.call <<-heredoc [JsonApi#request begin] # Request Method - #{method} Path - #{path} Params - #{params.pretty_inspect.strip} # Response Code - #{res.code} Body - #{res.json} [JsonApi#request end] heredoc end |
#merged_params(params) ⇒ Object
44 45 46 |
# File 'lib/json-api.rb', line 44 def merged_params(params) @default_params.nil? ? params : @default_params.merge(params) end |
#req(method, uri, params) ⇒ Object
54 55 56 57 58 |
# File 'lib/json-api.rb', line 54 def req(method, uri, params) req = Net::HTTP.const_get(method.capitalize).new(uri.to_s) req.form_data = params req end |
#request(method, path, params = {}) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/json-api.rb', line 20 def request(method, path, params = {}) path = full_path(path) params = merged_params(params) query_params, form_params = (method == :get ? [params, {}] : [{}, params]) uri = uri(path, query_params) req = req(method, uri, form_params) configure_request(req) res = http(uri).request(req) configure_response(res) log(method, path, params, res) res end |
#uri(path, params) ⇒ Object
48 49 50 51 52 |
# File 'lib/json-api.rb', line 48 def uri(path, params) uri = URI.parse(path) uri.query = URI.encode_www_form(params) uri end |