Module: Curb_DSL

Included in:
Dynamised::Scraper
Defined in:
lib/dynamised/curb_dsl.rb

Defined Under Namespace

Modules: Singleton

Constant Summary collapse

Regex =
{
  cookie_header: /Set-Cookie: /
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/dynamised/curb_dsl.rb', line 8

def self.included(base)
  base.extend Singleton
  base.instance_eval do
    attr_reader :curl, :headers,:payload, :username, :password, :auth_type, :uri, :ssl, :redirects, :type_converter,:cookies, :form_field_name, :form_fields

    [:get, :post, :put, :delete, :head, :options, :patch, :link, :unlink].each do |func_name|
      define_method func_name do |&block|
        make_request_of func_name.to_s.upcase, &block
      end
    end

    [:password,:username,:payload, :auth_type, :uri, :ssl, :redirects,:type_converter,:cookies,:form_field_name,:error_handler].each do |func_name|
      define_method "set_#{func_name}" do |value|
        self.instance_variable_set :"@#{func_name}", value
      end
    end
  end

  def form_field name,value
    @form_fields ||= []
    if @form_field_name
      @form_fields.push(Curl::PostField.content([("%[%]" % [@form_field_name,name]),value.to_s]))
    else
      @form_fields.push(Curl::PostField.content(name ,value.to_s))
    end
  end



end

Instance Method Details

#bodyObject



106
107
108
# File 'lib/dynamised/curb_dsl.rb', line 106

def body
  @curl.body
end

#decode_html(string) ⇒ Object



90
91
92
# File 'lib/dynamised/curb_dsl.rb', line 90

def decode_html(string)
  CGI.unescapeHTML(string)
end

#encode_html(string) ⇒ Object



94
95
96
# File 'lib/dynamised/curb_dsl.rb', line 94

def encode_html(string)
  CGI.escapeHTML(string)
end

#form_field(name, value) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/dynamised/curb_dsl.rb', line 26

def form_field name,value
  @form_fields ||= []
  if @form_field_name
    @form_fields.push(Curl::PostField.content([("%[%]" % [@form_field_name,name]),value.to_s]))
  else
    @form_fields.push(Curl::PostField.content(name ,value.to_s))
  end
end

#header(name, content) ⇒ Object



56
57
58
59
# File 'lib/dynamised/curb_dsl.rb', line 56

def header(name, content)
  @headers ||= {}
  @headers[name] = content
end

#ignore_errorObject



102
103
104
# File 'lib/dynamised/curb_dsl.rb', line 102

def ignore_error
  @ignore_error = true
end

#initialize(&block) ⇒ Object



51
52
53
54
# File 'lib/dynamised/curb_dsl.rb', line 51

def initialize(&block)
  @headers ||= {}
  instance_eval(&block) if block_given?
end

#make_request_of(request_method, i_e = nil, &block) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/dynamised/curb_dsl.rb', line 61

def make_request_of(request_method,i_e =nil,&block)
  i_e ||= @ignore_error
  @resp_cookies = nil
  @curl = Curl::Easy.new(@uri) do |http|
    setup_request request_method, http
  end
  @curl.ssl_verify_peer = @ssl ||false
  # @curl.ignore_content_length = true
  if @form_fields
    @curl.http_post(*@form_field)
  else
    @curl.http request_method
  end
  if @curl.response_code == 301
    @uri =  @curl.redirect_url
    make_request_of request_method, i_e
  end
  if @curl.response_code != 200
    if @error_handler
      puts @error_handler.call unless i_e
    end
  end
  @ignore_error = false
end

#post_bodyObject



98
99
100
# File 'lib/dynamised/curb_dsl.rb', line 98

def post_body
  get_payload
end

#query_params(value) ⇒ Object



127
128
129
# File 'lib/dynamised/curb_dsl.rb', line 127

def query_params(value)
  Curl::postalize(value)
end

#response_codeObject



110
111
112
# File 'lib/dynamised/curb_dsl.rb', line 110

def response_code
  @curl.response_code
end

#response_cookiesObject



114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/dynamised/curb_dsl.rb', line 114

def response_cookies
  @resp_cookies ||=
  @curl.header_str.split("\r\n").each_with_object({}) do |header,headers|
    if header =~ Regex[:cookie_header]
      header.gsub(Regex[:cookie_header],'').split(';').each do |segment|
        unless segment =~ /secure/
          headers[$1.strip.downcase] = $2.gsub('"','') if segment =~ /(.*?)=(.*?)($|;|,(?! ))/
        end
      end
    end
  end
end

#status_codeObject



86
87
88
# File 'lib/dynamised/curb_dsl.rb', line 86

def status_code
  @curl.response_code
end