Class: Curl::Easy

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url = nil) ⇒ Easy

Returns a new instance of Easy.



14
15
16
17
18
# File 'lib/curbemu.rb', line 14

def initialize(url = nil)
  @url = url
  @headers = {}
  @body_str = nil
end

Instance Attribute Details

#body_strObject

Returns the value of attribute body_str.



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

def body_str
  @body_str
end

#connObject

Returns the value of attribute conn.



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

def conn
  @conn
end

#headersObject

Returns the value of attribute headers.



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

def headers
  @headers
end

#timeoutObject

Returns the value of attribute timeout.



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

def timeout
  @timeout
end

#urlObject

Returns the value of attribute url.



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

def url
  @url
end

Class Method Details

.http_get(url) {|c| ... } ⇒ Object

Yields:

  • (c)


34
35
36
37
38
39
# File 'lib/curbemu.rb', line 34

def self.http_get(url)
  c = self.new(url)
  yield(c) if block_given?
  c.perform
  c
end

.http_post(url, options = {}) {|c| ... } ⇒ Object

Curl::Easy.http_post(“foo.com”, => url) { |r| r.headers = ‘Content-Type: text/json’ }.body_str)

Yields:

  • (c)


42
43
44
45
46
47
# File 'lib/curbemu.rb', line 42

def self.http_post(url, options = {})
  c = self.new(url)
  yield(c) if block_given?
  c.http_post(options)
  c 
end

.perform(url) {|c| ... } ⇒ Object

Curl::Easy.perform(“old-xisbn.oclc.org/xid/isbn/1234”).body_str Curl::Easy.perform(“old-xisbn.oclc.org/xid/isbn/1234”).header_str

Yields:

  • (c)


27
28
29
30
31
32
# File 'lib/curbemu.rb', line 27

def self.perform(url)
  c = self.new(url)
  yield(c) if block_given?
  c.perform
  c
end

Instance Method Details

#header_strObject

Not yet implemented.. only needed for importing from LibraryThing



21
22
23
# File 'lib/curbemu.rb', line 21

def header_str
  ""
end

#http_post(options = {}) ⇒ Object



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

def http_post(options = {})
  uri = URI.parse(url)
  http = Net::HTTP.new(uri.host, uri.port)
  resp, data = http.post(uri.request_uri, options, headers)
  @body_str = data
rescue => e
   raise ::Curl::Err::HttpError, e.message                  
end

#performObject



49
50
51
52
53
54
55
56
57
# File 'lib/curbemu.rb', line 49

def perform
  uri = URI.parse(url)
  res = Net::HTTP.start(uri.host, uri.port) {|http|
    http.request(Net::HTTP::Get.new(uri.request_uri))
  }
  @body_str = res.body
rescue => e
  raise ::Curl::Err::HttpError, e.message                  
end