class Textmagic::REST::Client
Constants
- HTTP_HEADERS
Attributes
host[R]
token[R]
username[R]
Public Class Methods
new(username=nil, token=nil, host='https://rest.textmagic.com/api/v2')
click to toggle source
Create a new TextMagic APIv2 REST
HTTP client.
Example:
@client = Textmagic::REST::Client.new username, token
username: 'my_textmagic_username'
¶ ↑
TextMagic account's username. Can be found here: my.textmagic.com/online/account/details
token: 'my_textmagic_apiv2_token'
¶ ↑
TextMagic account's token. Can be found here: my.textmagic.com/online/api/rest-api/keys
host: 'https://api.textmagictesting.com'
¶ ↑
The domain to which you'd like the client to make HTTP requests. Useful for testing. Defaults to 'rest.textmagic.com'.
# File lib/textmagic-ruby/rest/client.rb 29 def initialize(username=nil, token=nil, host='https://rest.textmagic.com/api/v2') 30 @username = username 31 @token = token 32 @host = host 33 if @username.nil? || @token.nil? 34 raise ArgumentError, 'Username and token are required' 35 end 36 37 setup_connection 38 setup_resources 39 end
Public Instance Methods
ping()
click to toggle source
# File lib/textmagic-ruby/rest/client.rb 70 def ping 71 path = 'ping' 72 response = self.get "/#{path}", {} 73 response[path] 74 end
Protected Instance Methods
build_full_path(path, params, method)
click to toggle source
# File lib/textmagic-ruby/rest/client.rb 82 def build_full_path(path, params, method) 83 path << "?#{url_encode(to_camel_case(params, false))}" if method == :get && !params.empty? 84 "#{@host}#{path}" 85 end
make_request(request)
click to toggle source
# File lib/textmagic-ruby/rest/client.rb 99 def make_request(request) 100 101 response = @conn.request request 102 103 if response.kind_of? Net::HTTPServerError 104 raise Textmagic::REST::ServerError 105 end 106 107 if response.body and !response.body.empty? 108 object = MultiJson.load response.body 109 elsif response.code == 204.to_s 110 object = true 111 elsif response.kind_of? Net::HTTPBadRequest 112 object = {:message => 'Bad request', :code => 400} 113 end 114 115 if response.kind_of? Net::HTTPClientError 116 raise Textmagic::REST::RequestError.new object['message'], object['code'] 117 end 118 object 119 end
setup_connection()
click to toggle source
# File lib/textmagic-ruby/rest/client.rb 87 def setup_connection 88 uri = URI.parse(@host) 89 @conn = Net::HTTP.new(uri.host, uri.port) 90 setup_ssl 91 end
setup_resources()
click to toggle source
# File lib/textmagic-ruby/rest/client.rb 121 def setup_resources 122 resource self, :messages, :contacts, :lists, :custom_fields, :unsubscribers, 123 :bulks, :chats, :schedules, :sessions, :templates, :invoices, :users, 124 :numbers, :senderids, :subaccounts, :replies 125 end
setup_ssl()
click to toggle source
# File lib/textmagic-ruby/rest/client.rb 93 def setup_ssl 94 @conn.use_ssl = true 95 @conn.verify_mode = OpenSSL::SSL::VERIFY_PEER 96 @conn.ca_file = File.dirname(__FILE__) + '/../../../conf/cacert.pem' 97 end
url_encode(hash)
click to toggle source
# File lib/textmagic-ruby/rest/client.rb 78 def url_encode(hash) 79 hash.to_a.map {|p| p.map {|e| CGI.escape e.to_s}.join '='}.join '&' 80 end