Class: TropoClient
- Inherits:
-
Object
- Object
- TropoClient
- Defined in:
- lib/tropo-provisioning/tropo_client.rb
Overview
This class is in charge of handling HTTP requests to the Tropo HTTP endpoint
Instance Attribute Summary collapse
-
#base_uri ⇒ Object
readonly
Tropo provisioning API endpoint.
-
#headers ⇒ Object
required HTTP headers.
-
#password ⇒ Object
readonly
password associated to :username.
-
#username ⇒ Object
readonly
Valid Tropo username.
-
#verify_certificate ⇒ Object
readonly
whether to require a valid certificate if using SSL.
Class Method Summary collapse
-
.hashie_array(array) ⇒ Object
Converts the hashes inside the array to Hashie::Mash objects.
Instance Method Summary collapse
-
#camelize_params(params) ⇒ Object
Format the parameters.
-
#delete(resource = "", params = {}) ⇒ Object
Send a HTTP Delete.
-
#get(resource = "", params = {}) ⇒ Object
Send a HTTP Get.
-
#initialize(username, password, base_uri = "https://api.tropo.com/v1/", headers = nil, proxy = nil, verify_certificate = true) ⇒ TropoClient
constructor
Creates a new TropoClient instance.
-
#post(resource = "", params = {}) ⇒ Object
Send a HTTP Post.
-
#put(resource = "", params = {}) ⇒ Object
Send a HTTP Put.
-
#set_request_type(method, uri) ⇒ Object
Sets the HTTP REST type based on the method being called.
Constructor Details
#initialize(username, password, base_uri = "https://api.tropo.com/v1/", headers = nil, proxy = nil, verify_certificate = true) ⇒ TropoClient
Creates a new TropoClient instance
Parameters
- required, String
-
username valid Tropo username
- required, String
-
password valid password
- optional, String
-
base_uri Tropo provisioning API endpoint
- optional, String
-
headers required HTTP headers
- optional, Hash
-
proxy => : <host>, “port” : <port>
-
- optional, Boolean
-
:verify_certificate whether to require a valid certificate if using SSL, defaults to true
Return
-
new TropoClient instance
36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/tropo-provisioning/tropo_client.rb', line 36 def initialize(username, password, base_uri = "https://api.tropo.com/v1/", headers = nil, proxy = nil, verify_certificate = true) @base_uri = base_uri if RUBY_VERSION =~ /1.8/ @base_uri << "/" if !@base_uri[-1].eql?(47) elsif RUBY_VERSION =~ /1.9/ @base_uri << "/" if !@base_uri[-1].eql?("/") end @username = username @password = password @headers = headers.nil? ? {} : headers @proxy = proxy @verify_certificate = verify_certificate end |
Instance Attribute Details
#base_uri ⇒ Object (readonly)
Tropo provisioning API endpoint
15 16 17 |
# File 'lib/tropo-provisioning/tropo_client.rb', line 15 def base_uri @base_uri end |
#headers ⇒ Object
required HTTP headers
13 14 15 |
# File 'lib/tropo-provisioning/tropo_client.rb', line 13 def headers @headers end |
#password ⇒ Object (readonly)
password associated to :username
19 20 21 |
# File 'lib/tropo-provisioning/tropo_client.rb', line 19 def password @password end |
#username ⇒ Object (readonly)
Valid Tropo username
17 18 19 |
# File 'lib/tropo-provisioning/tropo_client.rb', line 17 def username @username end |
#verify_certificate ⇒ Object (readonly)
whether to require a valid certificate if using SSL
21 22 23 |
# File 'lib/tropo-provisioning/tropo_client.rb', line 21 def verify_certificate @verify_certificate end |
Class Method Details
.hashie_array(array) ⇒ Object
Converts the hashes inside the array to Hashie::Mash objects
Parameters
- required, Array
-
array to be Hashied
Return
- Array
-
array that is now Hashied
228 229 230 231 232 233 234 |
# File 'lib/tropo-provisioning/tropo_client.rb', line 228 def hashie_array(array) hashied_array = [] array.each do |ele| hashied_array << Hashie::Mash.new(ele) end hashied_array end |
Instance Method Details
#camelize_params(params) ⇒ Object
Format the parameters
Parameters
- required, Hash
-
request parameters
Return
-
camelized params
116 117 118 119 120 |
# File 'lib/tropo-provisioning/tropo_client.rb', line 116 def camelize_params(params) camelized = {} params.each { |k,v| camelized.merge!(k.to_s.camelize(:lower).to_sym => v) } camelized end |
#delete(resource = "", params = {}) ⇒ Object
Send a HTTP Delete
Parameters
- optional, String
-
resource path URI
- optional, Hash
-
Query parameters
Return
JSON decoded object
89 90 91 92 93 |
# File 'lib/tropo-provisioning/tropo_client.rb', line 89 def delete(resource = "", params = {}) uri = "#{base_uri}#{resource}" params.empty? or uri = uri.concat('?').concat(params.collect { |k, v| "#{k}=#{v.to_s}" }.join("&")) request(Net::HTTP::Delete.new(uri)) end |
#get(resource = "", params = {}) ⇒ Object
Send a HTTP Get
Parameters
- optional, String
-
path URI
- optional, Hash
-
Query parameters
Return
JSON decoded object
60 61 62 63 64 |
# File 'lib/tropo-provisioning/tropo_client.rb', line 60 def get(resource = "", params = {}) uri = "#{base_uri}#{resource}" params.empty? or uri = uri.concat('?').concat(params.collect { |k, v| "#{k}=#{v.to_s}" }.join("&")) request(Net::HTTP::Get.new(uri)) end |
#post(resource = "", params = {}) ⇒ Object
Send a HTTP Post
Parameters
- optional, String
-
resource path URI
- optional, Hash
-
params body to be JSON encoded
Return
JSON decoded object
75 76 77 78 |
# File 'lib/tropo-provisioning/tropo_client.rb', line 75 def post(resource = "", params = {}) uri = "#{base_uri}#{resource}" request(Net::HTTP::Post.new(uri), params) end |
#put(resource = "", params = {}) ⇒ Object
Send a HTTP Put
Parameters
- optional, String
-
resource path URI
- optional, Hash
-
params body to be JSON encoded
Return
JSON decoded object
104 105 106 107 |
# File 'lib/tropo-provisioning/tropo_client.rb', line 104 def put(resource = "", params = {}) uri = "#{base_uri}#{resource}" request(Net::HTTP::Put.new(uri), params) end |
#set_request_type(method, uri) ⇒ Object
Sets the HTTP REST type based on the method being called
Parameters
- required, ymbol
-
the HTTP method to use, may be :delete, :get, :post or :put
- Object
-
the uri object to create the request for
- Object
-
the request object to be used to operate on the resource
Return
-
Valid HTTP verb instance
133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/tropo-provisioning/tropo_client.rb', line 133 def set_request_type(method, uri) case method when :delete Net::HTTP::Delete.new(uri) when :get Net::HTTP::Get.new(uri) when :post Net::HTTP::Post.new(uri) when :put Net::HTTP::Put.new(uri) end end |