Module: Mediafire::Connection

Included in:
API
Defined in:
lib/mediafire/connection.rb

Constant Summary collapse

ENDPOINT =
'http://www.mediafire.com/'

Instance Method Summary collapse

Instance Method Details

#get(path, options = {}) ⇒ Object



12
13
14
# File 'lib/mediafire/connection.rb', line 12

def get(path, options={})
  request(:get, path, options)
end

#has_multipart?(options = {}) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
62
63
64
65
66
# File 'lib/mediafire/connection.rb', line 59

def has_multipart?(options={})
  options.values.each do |v|
    if v.is_a? UploadIO
      return true
    end
  end
  false
end

#post(path, options = {}) ⇒ Object



16
17
18
# File 'lib/mediafire/connection.rb', line 16

def post(path, options={})
  request(:post, path, options)
end

#request(method, path, options) ⇒ Object



20
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
52
53
54
55
56
57
# File 'lib/mediafire/connection.rb', line 20

def request(method, path, options)
  uri = URI.parse("#{ENDPOINT}#{path}")

  request = nil
  if method == :get
    request = Net::HTTP::Get.new(uri.request_uri)
  elsif method == :post
    if has_multipart? options
      request = Net::HTTP::Post::Multipart.new(uri.request_uri, options)
    else
      request = Net::HTTP::Post.new(uri.request_uri)
      request.set_form_data(options)
    end
  end
  request['User-Agent'] = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.120 Safari/535.2'
  request['Cookie'] = cookie

  if has_multipart? options
    t = Thread.new do
      loop do
        @s_queue.pop.call request.upload_size
      end
    end
    options.values.each do |v|
      if v.is_a? UploadIO
        t[:filename] = v.original_filename
      end
    end
  end
  response = Net::HTTP.start(uri.host, uri.port) do |http|
    http.request(request)
  end
  t.kill if has_multipart? options
  check_statuscode(response)
  build_cookie(response.get_fields('Set-Cookie'))

  return response
end

#upload_size(filename) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/mediafire/connection.rb', line 68

def upload_size(filename)
  Thread.list.each do |t|
    if t[:filename] == filename
      @s_queue.push Proc.new {|n| @r_queue.push n}
      return @r_queue.pop
    end
  end
  0
rescue Timeout::Error => e
  return nil
end