Class: VoxcastApi::Base

Inherits:
Http
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/voxcast_api/base.rb

Direct Known Subclasses

Purge

Instance Attribute Summary collapse

Attributes inherited from Http

#password, #url, #user

Instance Method Summary collapse

Methods inherited from Http

#execute, #get_http, #timestamp

Constructor Details

#initialize(entry = nil) ⇒ Base

Returns a new instance of Base.



13
14
15
16
17
18
19
# File 'lib/voxcast_api/base.rb', line 13

def initialize(entry = nil)
  super(entry)
  if @entry
    @key = entry.purge_key
    @secret_key = entry.purge_secret_key
  end
end

Instance Attribute Details

#keyObject

Returns the value of attribute key.



11
12
13
# File 'lib/voxcast_api/base.rb', line 11

def key
  @key
end

#secret_keyObject

Returns the value of attribute secret_key.



11
12
13
# File 'lib/voxcast_api/base.rb', line 11

def secret_key
  @secret_key
end

Instance Method Details

#api_sig(params, secret) ⇒ Object



109
110
111
112
113
114
# File 'lib/voxcast_api/base.rb', line 109

def api_sig(params, secret)
  sig = secret.to_s
  p = params.sort {|a, b| a[0].to_s <=> b[0].to_s}
  p.each {|k, v| sig += k.to_s + v.to_s}
  Digest::MD5.hexdigest(sig)
end

#auth_key?(params = {}) ⇒ Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/voxcast_api/base.rb', line 116

def auth_key?(params = {})
  params['method'] == 'voxel.hapi.authkeys.read'
end

#devicesObject



68
69
70
71
72
# File 'lib/voxcast_api/base.rb', line 68

def devices
  ret = execute_method(:devices)
  devices = []
  devices = ret['devices'] if ret
end

#execute_method(method, params = {}) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
# File 'lib/voxcast_api/base.rb', line 97

def execute_method(method, params = {})
  url = nil
  params['method'] = lookup_method(method)
  if method.to_s == 'auth_key'
    # if its auth key, replace http with https
    url = @url
    url = url.gsub('http://', 'https://')
    url = "https://#{url}" if url.index('https://').blank?
  end
  execute(url, params)
end

#get_basic_auth(params = {}) ⇒ Object



120
121
122
123
# File 'lib/voxcast_api/base.rb', line 120

def get_basic_auth(params = {})
  return [@user, @password] if auth_key?(params)
  super(params)
end

#get_data(params = {}, key = nil, secret_key = nil, key_label = 'key') ⇒ Object



137
138
139
140
141
142
143
144
145
146
# File 'lib/voxcast_api/base.rb', line 137

def get_data(params = {}, key = nil, secret_key = nil, key_label = 'key')
  key = @key if key.blank?
  secret_key = @secret_key if secret_key.blank?
  @logger.warn('warning, the key or secret key is blank, purging might not work') if key.blank? || secret_key.blank?
  params = params.merge({'timestamp' => timestamp(),
                          key_label => key})
  sig = api_sig(params, secret_key)
  params['api_sig'] = sig
  data = build_query_string(params)
end

#get_method(params = {}) ⇒ Object



148
149
150
151
# File 'lib/voxcast_api/base.rb', line 148

def get_method(params = {})
  return :get if auth_key?(params)
  super(params)
end

#get_path_and_params(params = {}, url = nil) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
# File 'lib/voxcast_api/base.rb', line 125

def get_path_and_params(params = {}, url = nil)
  url = @url if url.blank?
  uri = URI.parse(url)
  get_params = {'method' => params['method']}
  get_path = build_query_string(get_params)
  path = "#{uri.path}?#{get_path}"
  # return without params if its an authkey
  return [path, ''] if auth_key?(params)
  data = get_data(params)
  [path, data]
end

#log_download(filename, dst_file) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/voxcast_api/base.rb', line 84

def log_download(filename, dst_file)
  # we just want the raw output, so skip the processing of the response and
  # use the special params "raw"
  params = {'filename' => filename, 'convert' => 'raw', :skip_process => true}
  resp = execute_method(:log_download, params)
  if resp.code.to_i == 200
    File.open(dst_file, 'wb') {|f| f.write(resp.body)}
    true
  else
    false
  end
end

#log_list(params = {}) ⇒ Object



74
75
76
77
78
79
80
81
82
# File 'lib/voxcast_api/base.rb', line 74

def log_list(params = {})
  params[:complex_parser] = true
  ret = execute_method(:log_list, params)
  # parse the response with complex
  return [] if ret[:status] != true
  data = ret[:detail]
  # get the log files
  data.search('log_file')
end

#lookup_method(key) ⇒ Object



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
# File 'lib/voxcast_api/base.rb', line 21

def lookup_method(key)
  # this will default to the new 1.0 api, but 0.1 can override it
  case key.to_s
  when 'devices'
    'voxel.devices.list'
  when 'test'
    'voxel.test.echo'
  when 'purge_site'
    'voxel.voxcast.ondemand.content.purge_site'
  when 'purge_file'
    'voxel.voxcast.ondemand.content.purge_file'
  when 'purge_dir'
    'voxel.voxcast.ondemand.content.purge_directory'
  when 'populate'
    'voxel.voxcast.ondemand.content.populate'
  when 'status'
    'voxel.voxcast.ondemand.content.transaction_status'
  when 'auth_key'
    'voxel.hapi.authkeys.read'
  when 'log_list'
    'voxel.voxcast.ondemand.logs.list'
  when 'log_download'
    'voxel.voxcast.ondemand.logs.download'
  when 'live_log_list'
    'voxel.voxcast.live.logs.list'
  when 'live_log_download'
    'voxel.voxcast.live.logs.download'
  else
    nil
  end
end

#populate(paths) ⇒ Object



57
58
59
60
61
62
# File 'lib/voxcast_api/base.rb', line 57

def populate(paths)
  params = { 'device_id' => device_id,
    'paths' => get_urls(paths)
  }
  execute(:populate, params)
end

#process_response(resp, complex = false) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/voxcast_api/base.rb', line 153

def process_response(resp, complex = false)
  if resp.code.to_i == 200
    body = resp.body
    if complex
      ret = Hpricot.parse(body) rescue {}
      rsp = ret.find_element('rsp')
      if rsp
        stat = rsp.attributes['stat']
        err = rsp.attributes['err']
      end
    else
      ret = (Hash.from_xml(body) rescue {}) || {}
      rsp = ret['rsp']
      if rsp
        stat = rsp['stat']
        err = rsp['err']
      end
    end
    return {:status => false, :detail => "return body does not appear valid #{body}"} if rsp.blank?
    if stat == 'ok'
      return {:status => true, :detail => rsp}
    else
      msg = "error purging content #{err['msg']}, #{err['code']}"
      return {:status => false, :detail => msg}
    end
  else
    msg = "purge failed with http error #{resp.code}"
    @logger.warn(msg)
    return {:status => false, :detail => msg}
  end
end

#status(id) ⇒ Object



64
65
66
# File 'lib/voxcast_api/base.rb', line 64

def status(id)
  execute_method(:status, {'transaction_id'=>id})
end

#test(params = {}) ⇒ Object



53
54
55
# File 'lib/voxcast_api/base.rb', line 53

def test(params = {})
  execute_method(:test, params)
end