Class: Instamsg::Client
- Inherits:
-
Object
- Object
- Instamsg::Client
- Defined in:
- lib/instamsg/client.rb
Instance Attribute Summary collapse
-
#bearer_token ⇒ Object
Returns the value of attribute bearer_token.
-
#connect_timeout ⇒ Object
writeonly
Sets the attribute connect_timeout.
-
#host ⇒ Object
Returns the value of attribute host.
-
#http_proxy ⇒ Object
Returns the value of attribute http_proxy.
-
#keep_alive_timeout ⇒ Object
writeonly
Sets the attribute keep_alive_timeout.
-
#key ⇒ Object
Returns the value of attribute key.
-
#port ⇒ Object
Returns the value of attribute port.
-
#proxy ⇒ Object
readonly
Returns the value of attribute proxy.
-
#response_timeout ⇒ Object
writeonly
Sets the attribute response_timeout.
-
#scheme ⇒ Object
Returns the value of attribute scheme.
-
#secret ⇒ Object
Returns the value of attribute secret.
-
#send_timeout ⇒ Object
writeonly
Sets the attribute send_timeout.
Instance Method Summary collapse
- #access_token ⇒ Object
- #authenticate(path, params = {}) ⇒ Object
- #delete(path, params = {}) ⇒ Object
- #delete_async(path, params = {}) ⇒ Object
- #encrypted=(boolean) ⇒ Object
- #encrypted? ⇒ Boolean
- #get(path, params = {}) ⇒ Object
- #get_async(path, params = {}) ⇒ Object
- #get_file(path, params = {}) ⇒ Object
- #http_async_client(uri) ⇒ Object
- #http_sync_client ⇒ Object
-
#initialize(options = {}) ⇒ Client
constructor
A new instance of Client.
- #invalidate(path, params = {}) ⇒ Object
- #post(path, params = {}) ⇒ Object
- #post_async(path, params = {}) ⇒ Object
- #put(path, params = {}) ⇒ Object
- #put_async(path, params = {}) ⇒ Object
- #put_file(path, params = {}) ⇒ Object
- #resource(path) ⇒ Object
- #timeout=(value) ⇒ Object
- #url(path = nil) ⇒ Object
- #url=(url) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Client
Returns a new instance of Client.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/instamsg/client.rb', line 11 def initialize( = {}) default = { :scheme => 'https', :host => 'platform.instamsg.io', :port => 443, }.merge() @scheme, @host, @port, @key, @secret, @bearer_token = default.values_at( :scheme, :host, :port, :key, :secret, :bearer_token ) @http_proxy = nil self.http_proxy = default[:http_proxy] if default[:http_proxy] @connect_timeout = 15 @send_timeout = 100 @response_timeout = 180 @keep_alive_timeout = 180 end |
Instance Attribute Details
#bearer_token ⇒ Object
Returns the value of attribute bearer_token.
6 7 8 |
# File 'lib/instamsg/client.rb', line 6 def bearer_token @bearer_token end |
#connect_timeout=(value) ⇒ Object (writeonly)
Sets the attribute connect_timeout
8 9 10 |
# File 'lib/instamsg/client.rb', line 8 def connect_timeout=(value) @connect_timeout = value end |
#host ⇒ Object
Returns the value of attribute host.
6 7 8 |
# File 'lib/instamsg/client.rb', line 6 def host @host end |
#http_proxy ⇒ Object
Returns the value of attribute http_proxy.
7 8 9 |
# File 'lib/instamsg/client.rb', line 7 def http_proxy @http_proxy end |
#keep_alive_timeout=(value) ⇒ Object (writeonly)
Sets the attribute keep_alive_timeout
8 9 10 |
# File 'lib/instamsg/client.rb', line 8 def keep_alive_timeout=(value) @keep_alive_timeout = value end |
#key ⇒ Object
Returns the value of attribute key.
6 7 8 |
# File 'lib/instamsg/client.rb', line 6 def key @key end |
#port ⇒ Object
Returns the value of attribute port.
6 7 8 |
# File 'lib/instamsg/client.rb', line 6 def port @port end |
#proxy ⇒ Object (readonly)
Returns the value of attribute proxy.
7 8 9 |
# File 'lib/instamsg/client.rb', line 7 def proxy @proxy end |
#response_timeout=(value) ⇒ Object (writeonly)
Sets the attribute response_timeout
8 9 10 |
# File 'lib/instamsg/client.rb', line 8 def response_timeout=(value) @response_timeout = value end |
#scheme ⇒ Object
Returns the value of attribute scheme.
6 7 8 |
# File 'lib/instamsg/client.rb', line 6 def scheme @scheme end |
#secret ⇒ Object
Returns the value of attribute secret.
6 7 8 |
# File 'lib/instamsg/client.rb', line 6 def secret @secret end |
#send_timeout=(value) ⇒ Object (writeonly)
Sets the attribute send_timeout
8 9 10 |
# File 'lib/instamsg/client.rb', line 8 def send_timeout=(value) @send_timeout = value end |
Instance Method Details
#access_token ⇒ Object
39 40 41 |
# File 'lib/instamsg/client.rb', line 39 def access_token @access_token ||= "Basic " + Base64.encode64(@key + ":" + @secret).delete("\n") end |
#authenticate(path, params = {}) ⇒ Object
86 87 88 |
# File 'lib/instamsg/client.rb', line 86 def authenticate(path, params = {}) Resource.new(self, path).authenticate(params) end |
#delete(path, params = {}) ⇒ Object
118 119 120 |
# File 'lib/instamsg/client.rb', line 118 def delete(path, params = {}) Resource.new(self, path).delete(params) end |
#delete_async(path, params = {}) ⇒ Object
122 123 124 |
# File 'lib/instamsg/client.rb', line 122 def delete_async(path, params = {}) Resource.new(self, path).delete_aysnc(params) end |
#encrypted=(boolean) ⇒ Object
69 70 71 72 |
# File 'lib/instamsg/client.rb', line 69 def encrypted=(boolean) @scheme = boolean ? 'https' : 'http' @port = boolean ? 8601 : 8600 end |
#encrypted? ⇒ Boolean
74 75 76 |
# File 'lib/instamsg/client.rb', line 74 def encrypted? @scheme == 'https' end |
#get(path, params = {}) ⇒ Object
94 95 96 |
# File 'lib/instamsg/client.rb', line 94 def get(path, params = {}) Resource.new(self, path).get(params) end |
#get_async(path, params = {}) ⇒ Object
98 99 100 |
# File 'lib/instamsg/client.rb', line 98 def get_async(path, params = {}) Resource.new(self, path).get_async(params) end |
#get_file(path, params = {}) ⇒ Object
126 127 128 |
# File 'lib/instamsg/client.rb', line 126 def get_file(path, params= {}) Resource.new(self, path).download(params) end |
#http_async_client(uri) ⇒ Object
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/instamsg/client.rb', line 145 def http_async_client(uri) begin unless defined?(EventMachine) && EventMachine.reactor_running? raise Error, "Must use event machine for async request." end require 'em-http' unless defined?(EventMachine::HttpRequest) = { :connect_timeout => @connect_timeout, :inactivity_timeout => @response_timeout, } if defined?(@proxy) = { :host => @proxy[:host], :port => @proxy[:port] } if @proxy[:user] [:authorization] = [@proxy[:user], @proxy[:password]] end [:proxy] = end EventMachine::HttpRequest.new(uri, ) end end |
#http_sync_client ⇒ Object
134 135 136 137 138 139 140 141 142 143 |
# File 'lib/instamsg/client.rb', line 134 def http_sync_client @client ||= begin HTTPClient.new(@http_proxy).tap do |c| c.connect_timeout = @connect_timeout c.send_timeout = @send_timeout c.receive_timeout = @response_timeout c.keep_alive_timeout = @keep_alive_timeout end end end |
#invalidate(path, params = {}) ⇒ Object
90 91 92 |
# File 'lib/instamsg/client.rb', line 90 def invalidate(path, params = {}) Resource.new(self, path).authenticate(params) end |
#post(path, params = {}) ⇒ Object
102 103 104 |
# File 'lib/instamsg/client.rb', line 102 def post(path, params = {}) Resource.new(self, path).post(params) end |
#post_async(path, params = {}) ⇒ Object
106 107 108 |
# File 'lib/instamsg/client.rb', line 106 def post_async(path, params = {}) Resource.new(self, path).post_async(params) end |
#put(path, params = {}) ⇒ Object
110 111 112 |
# File 'lib/instamsg/client.rb', line 110 def put(path, params = {}) Resource.new(self, path).put(params) end |
#put_async(path, params = {}) ⇒ Object
114 115 116 |
# File 'lib/instamsg/client.rb', line 114 def put_async(path, params = {}) Resource.new(self, path).put_async(params) end |
#put_file(path, params = {}) ⇒ Object
130 131 132 |
# File 'lib/instamsg/client.rb', line 130 def put_file(path, params= {}) Resource.new(self, path).upload(params) end |
#resource(path) ⇒ Object
82 83 84 |
# File 'lib/instamsg/client.rb', line 82 def resource(path) Resource.new(self, path) end |
#timeout=(value) ⇒ Object
78 79 80 |
# File 'lib/instamsg/client.rb', line 78 def timeout=(value) @connect_timeout, @send_timeout, @response_timeout = value, value, value end |
#url(path = nil) ⇒ Object
30 31 32 33 34 35 36 37 |
# File 'lib/instamsg/client.rb', line 30 def url(path = nil) URI::Generic.build({ :scheme => @scheme, :host => @host, :port => @port, :path => "#{path}" }) end |
#url=(url) ⇒ Object
47 48 49 50 51 52 53 54 |
# File 'lib/instamsg/client.rb', line 47 def url=(url) uri = URI.parse(url) @scheme = uri.scheme @key = uri.user @secret = uri.password @host = uri.host @port = uri.port end |