Class: Reddit_Net_Wrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/Rubbit/Reddit_Net_Wrapper.rb

Constant Summary collapse

@@instance =
nil

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user) ⇒ Reddit_Net_Wrapper

Returns a new instance of Reddit_Net_Wrapper.



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/Rubbit/Reddit_Net_Wrapper.rb', line 41

def initialize(user)
  @used = 0
  @remaining = 30
  @reset = 60
  @first_request = 0
  @reset_timer = 60
  @user_agent = 'Rubbit/1.0 Ruby RAW by The1RGood USED BY: '
  @user_agent+=user

  @cookie = ""
end

Class Method Details

.instance(name = nil) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/Rubbit/Reddit_Net_Wrapper.rb', line 19

def self.instance(name=nil)
  if(@@instance==nil)
    if(name!=nil)
      @@instance = new(name)
    else
      return nil
    end
  end
  return @@instance
end

Instance Method Details

#make_request(request_type, url, params, redirect = false) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/Rubbit/Reddit_Net_Wrapper.rb', line 53

def make_request(request_type,url,params,redirect=false)
  uri = URI(url)

  if((Time.now - @first_request).to_i > @reset_timer)
    @used = 0
    @remaining = @reset_timer/2
    @reset = @reset_timer
    @first_request = Time.now
  end

  if(redirect==false)
    if(@remaining==0)
      while((Time.now-@first_request).to_i < @reset_timer)
      end
    end
    @used += 1
    @remaining -= 1
    @reset = (Time.now - @first_request).to_i
  end

  #puts url

  case request_type.downcase
  when 'post'
    req = Net::HTTP::Post.new uri.request_uri
    req['X-Ratelimit-Used'] = @used
    req['X-Ratelimit-Remaining'] = @remaining
    req['x-Ratelimit-Reset'] = @reset

    req['Cookie']=@cookie

    req.set_form_data(params)

    res = Net::HTTP.start(uri.hostname, uri.port){|http|
      http.request(req)
    }

    if(res.code=='302' or res.code=='301')
      res = make_request(request_type,res['location'],params,true)
    elsif(res['set-cookie']!=nil)
      @cookie = res['set-cookie']
    end

    return res
  when 'get'
    req = Net::HTTP::Get.new uri.request_uri
    req['X-Ratelimit-Used'] = @used
    req['X-Ratelimit-Remaining'] = @remaining
    req['x-Ratelimit-Reset'] = @reset

    req['Cookie']=@cookie

    res = Net::HTTP.start(uri.hostname, uri.port){|http|
      http.request(req)
    }

    if(res.code=='302' or res.code=='301')
      res = make_request(request_type,res['location'],params,true)
    elsif(res['set-cookie']!=nil)
      @cookie=res['set-cookie']
    end

    return res
  when 'delete'
    puts 'delete placeholder'
  else
    puts 'Bad Request Type'
  end
end

#set_reset_period(period = 60) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/Rubbit/Reddit_Net_Wrapper.rb', line 30

def set_reset_period(period=60)
  if(period%2!=0 or period>60 or period<2)
    print('Reset period must be an even integer between 2 and 60')
    return false
  else
    @reset_timer = period
    return true
  end

end