Class: GreenSMS::Http::Client
- Inherits:
-
Object
- Object
- GreenSMS::Http::Client
- Defined in:
- lib/greensms/http/rest.rb
Instance Method Summary collapse
-
#initialize(opts) ⇒ Client
constructor
A new instance of Client.
- #request(kwargs) ⇒ Object
Constructor Details
#initialize(opts) ⇒ Client
Returns a new instance of Client.
9 10 11 12 13 14 15 16 |
# File 'lib/greensms/http/rest.rb', line 9 def initialize(opts) @token = nil @default_data = {} @default_params = {} @use_camel_case = false opts.each { |name, value| instance_variable_set("@#{name}", value) } end |
Instance Method Details
#request(kwargs) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 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 75 76 77 |
# File 'lib/greensms/http/rest.rb', line 18 def request(kwargs) if !kwargs.has_key? "method" raise StandardError.new "Http Method is required" end method = kwargs["method"] if !kwargs.has_key? "url" raise StandardError.new "URL is required" end url = kwargs["url"] kwargs.delete("url") kwargs.delete("method") headers = {} if kwargs.has_key? "headers" kwargs["headers"].each { |name, value| headers[name] = value } end if !GreenSMS.is_null_or_empty(@token) headers["Authorization"] = "Bearer " + @token end params = {} @connection = Faraday.new(url: url) do |f| f.request :multipart f.request :url_encoded f.headers = headers f.adapter Faraday.default_adapter end if !@default_params.empty? @default_params.each { |name, value| params[name] = value } end if kwargs.has_key? "params" kwargs["params"].each { |name, value| params[name] = value } end d = {} if method == "POST" d = d.to_json end url = url + "?" + hash_to_query(params) resp = @connection.send(method.downcase.to_sym, url, d) resp = JSON.parse resp.body if resp.has_key? "error" raise RestError.new(resp["error"], resp["code"]) end if @use_camel_case resp = GreenSMS.camelize(resp) end return resp end |