Class: Instamsg::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/instamsg/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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(options = {})
  default = {
    :scheme => 'https',
    :host => 'platform.instamsg.io',
    :port => 443,
  }.merge(options)
  @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_tokenObject

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

Parameters:

  • value

    the value to set the attribute connect_timeout to.



8
9
10
# File 'lib/instamsg/client.rb', line 8

def connect_timeout=(value)
  @connect_timeout = value
end

#hostObject

Returns the value of attribute host.



6
7
8
# File 'lib/instamsg/client.rb', line 6

def host
  @host
end

#http_proxyObject

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

Parameters:

  • value

    the value to set the attribute keep_alive_timeout to.



8
9
10
# File 'lib/instamsg/client.rb', line 8

def keep_alive_timeout=(value)
  @keep_alive_timeout = value
end

#keyObject

Returns the value of attribute key.



6
7
8
# File 'lib/instamsg/client.rb', line 6

def key
  @key
end

#portObject

Returns the value of attribute port.



6
7
8
# File 'lib/instamsg/client.rb', line 6

def port
  @port
end

#proxyObject (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

Parameters:

  • value

    the value to set the attribute response_timeout to.



8
9
10
# File 'lib/instamsg/client.rb', line 8

def response_timeout=(value)
  @response_timeout = value
end

#schemeObject

Returns the value of attribute scheme.



6
7
8
# File 'lib/instamsg/client.rb', line 6

def scheme
  @scheme
end

#secretObject

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

Parameters:

  • value

    the value to set the attribute send_timeout to.



8
9
10
# File 'lib/instamsg/client.rb', line 8

def send_timeout=(value)
  @send_timeout = value
end

Instance Method Details

#access_tokenObject



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

Returns:

  • (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)

    conn_options = {
      :connect_timeout => @connect_timeout,
      :inactivity_timeout => @response_timeout,
    }

    if defined?(@proxy)
      proxy_options = {
        :host => @proxy[:host],
        :port => @proxy[:port]
      }
      if @proxy[:user]
        proxy_options[:authorization] = [@proxy[:user], @proxy[:password]]
      end
      conn_options[:proxy] = proxy_options
    end

    EventMachine::HttpRequest.new(uri, conn_options)
  end
end

#http_sync_clientObject



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