Class: VoxcastApi::Base

Inherits:
Http
  • Object
show all
Extended by:
Forwardable
Includes:
UrlMaker
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.



17
18
19
20
21
22
23
# File 'lib/voxcast_api/base.rb', line 17

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.



15
16
17
# File 'lib/voxcast_api/base.rb', line 15

def key
  @key
end

#secret_keyObject

Returns the value of attribute secret_key.



15
16
17
# File 'lib/voxcast_api/base.rb', line 15

def secret_key
  @secret_key
end

Instance Method Details

#api_sig(params, secret) ⇒ Object



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

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)


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

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

#devicesObject



72
73
74
75
76
# File 'lib/voxcast_api/base.rb', line 72

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

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



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/voxcast_api/base.rb', line 101

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



124
125
126
127
# File 'lib/voxcast_api/base.rb', line 124

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



141
142
143
144
145
146
147
148
149
150
# File 'lib/voxcast_api/base.rb', line 141

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



152
153
154
155
# File 'lib/voxcast_api/base.rb', line 152

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

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



129
130
131
132
133
134
135
136
137
138
139
# File 'lib/voxcast_api/base.rb', line 129

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



88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/voxcast_api/base.rb', line 88

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



78
79
80
81
82
83
84
85
86
# File 'lib/voxcast_api/base.rb', line 78

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



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
52
53
54
55
# File 'lib/voxcast_api/base.rb', line 25

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



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

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

#process_response(resp, complex = false) ⇒ Object



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
184
185
186
187
# File 'lib/voxcast_api/base.rb', line 157

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



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

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

#test(params = {}) ⇒ Object



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

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