Class: Cleantalk::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/cleantalk/request.rb

Direct Known Subclasses

CheckMessage, CheckNewuser

Defined Under Namespace

Classes: BadParameters

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Request

Fill params with constructor



13
14
15
16
17
18
19
20
# File 'lib/cleantalk/request.rb', line 13

def initialize(params = {})
  self.method_name, self.auth_key = params.delete(:method_name) || self.class::METHOD, nil
  unless params.empty?
    params.each do |key, value|
      send("#{key}=", value)
    end
  end
end

Instance Attribute Details

#agentObject

required



5
6
7
# File 'lib/cleantalk/request.rb', line 5

def agent
  @agent
end

#all_headersObject

required



5
6
7
# File 'lib/cleantalk/request.rb', line 5

def all_headers
  @all_headers
end

required



5
6
7
# File 'lib/cleantalk/request.rb', line 5

def allow_links
  @allow_links
end

#auth_keyObject

required



5
6
7
# File 'lib/cleantalk/request.rb', line 5

def auth_key
  @auth_key
end

#exampleObject

required



5
6
7
# File 'lib/cleantalk/request.rb', line 5

def example
  @example
end

#feedbackObject

required



5
6
7
# File 'lib/cleantalk/request.rb', line 5

def feedback
  @feedback
end

#js_onObject

required



5
6
7
# File 'lib/cleantalk/request.rb', line 5

def js_on
  @js_on
end

#last_error_noObject

required



5
6
7
# File 'lib/cleantalk/request.rb', line 5

def last_error_no
  @last_error_no
end

#last_error_textObject

required



5
6
7
# File 'lib/cleantalk/request.rb', line 5

def last_error_text
  @last_error_text
end

#last_error_timeObject

required



5
6
7
# File 'lib/cleantalk/request.rb', line 5

def last_error_time
  @last_error_time
end

#messageObject

required



5
6
7
# File 'lib/cleantalk/request.rb', line 5

def message
  @message
end

#method_nameObject

required



5
6
7
# File 'lib/cleantalk/request.rb', line 5

def method_name
  @method_name
end

#phoneObject

required



5
6
7
# File 'lib/cleantalk/request.rb', line 5

def phone
  @phone
end

#post_infoObject

required



5
6
7
# File 'lib/cleantalk/request.rb', line 5

def 
  @post_info
end

#response_langObject

required



5
6
7
# File 'lib/cleantalk/request.rb', line 5

def response_lang
  @response_lang
end

#sender_emailObject

required



5
6
7
# File 'lib/cleantalk/request.rb', line 5

def sender_email
  @sender_email
end

#sender_infoObject

required



5
6
7
# File 'lib/cleantalk/request.rb', line 5

def sender_info
  @sender_info
end

#sender_ipObject

required



5
6
7
# File 'lib/cleantalk/request.rb', line 5

def sender_ip
  @sender_ip
end

#sender_nicknameObject

required



5
6
7
# File 'lib/cleantalk/request.rb', line 5

def sender_nickname
  @sender_nickname
end

#stoplist_checkObject

required



5
6
7
# File 'lib/cleantalk/request.rb', line 5

def stoplist_check
  @stoplist_check
end

#submit_timeObject

required



5
6
7
# File 'lib/cleantalk/request.rb', line 5

def submit_time
  @submit_time
end

#tzObject

required



5
6
7
# File 'lib/cleantalk/request.rb', line 5

def tz
  @tz
end

Instance Method Details

#allowed?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/cleantalk/request.rb', line 56

def allowed?
  self.result.allow == 1
end

#http_requestObject

Remote Call



40
41
42
# File 'lib/cleantalk/request.rb', line 40

def http_request
  JSON.parse http_request_without_parse
end

#http_request_without_parseObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/cleantalk/request.rb', line 22

def http_request_without_parse
  valid?
  form_data = self.instance_variables.inject({}) do |params, var_name|
    param_key = var_name.to_s.sub('@','')
    params[param_key] = send(param_key)
    params
  end

  req = Net::HTTP::Post.new(API_URI, API_HEADERS)
  req.body = JSON.generate(form_data)
  response = Net::HTTP.start(API_URI.hostname, API_URI.port, use_ssl: true) do |http|
    http.request(req)
  end

  response.entity
end

#resultObject



52
53
54
# File 'lib/cleantalk/request.rb', line 52

def result
  @result ||= http_request
end