Class: Hurley::Client

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/hurley/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(endpoint = nil) {|_self| ... } ⇒ Client

Returns a new instance of Client.

Yields:

  • (_self)

Yield Parameters:



13
14
15
16
17
18
19
20
21
22
# File 'lib/hurley/client.rb', line 13

def initialize(endpoint = nil)
  @before_callbacks = []
  @after_callbacks = []
  @url = Url.parse(endpoint)
  @header = Header.new :user_agent => Hurley::USER_AGENT
  @connection = nil
  @request_options = RequestOptions.new
  @ssl_options = SslOptions.new
  yield self if block_given?
end

Instance Attribute Details

#connectionObject



32
33
34
# File 'lib/hurley/client.rb', line 32

def connection
  @connection ||= Hurley.default_connection
end

#headerObject (readonly)

Returns the value of attribute header.



8
9
10
# File 'lib/hurley/client.rb', line 8

def header
  @header
end

#request_optionsObject (readonly)

Returns the value of attribute request_options.



10
11
12
# File 'lib/hurley/client.rb', line 10

def request_options
  @request_options
end

#ssl_optionsObject (readonly)

Returns the value of attribute ssl_options.



11
12
13
# File 'lib/hurley/client.rb', line 11

def ssl_options
  @ssl_options
end

#urlObject (readonly)

Returns the value of attribute url.



7
8
9
# File 'lib/hurley/client.rb', line 7

def url
  @url
end

Instance Method Details

#after_call(name_or_callback = nil) ⇒ Object



98
99
100
101
102
# File 'lib/hurley/client.rb', line 98

def after_call(name_or_callback = nil)
  @after_callbacks << (block_given? ?
    NamedCallback.for(name_or_callback  , Proc.new) :
    NamedCallback.for(nil, name_or_callback))
end

#after_callbacksObject



108
109
110
# File 'lib/hurley/client.rb', line 108

def after_callbacks
  @after_callbacks.map(&:name)
end

#before_call(name_or_callback = nil) ⇒ Object



92
93
94
95
96
# File 'lib/hurley/client.rb', line 92

def before_call(name_or_callback = nil)
  @before_callbacks << (block_given? ?
    NamedCallback.for(name_or_callback, Proc.new) :
    NamedCallback.for(nil, name_or_callback))
end

#before_callbacksObject



104
105
106
# File 'lib/hurley/client.rb', line 104

def before_callbacks
  @before_callbacks.map(&:name)
end

#call(request) ⇒ Object



88
89
90
# File 'lib/hurley/client.rb', line 88

def call(request)
  call_with_redirects(request, [])
end

#delete(path, query = nil) {|req| ... } ⇒ Object

Yields:

  • (req)


74
75
76
77
78
79
# File 'lib/hurley/client.rb', line 74

def delete(path, query = nil)
  req = request(:delete, path)
  req.query.update(query) if query
  yield req if block_given?
  call(req)
end

#get(path, query = nil) {|req| ... } ⇒ Object

Yields:

  • (req)


43
44
45
46
47
48
# File 'lib/hurley/client.rb', line 43

def get(path, query = nil)
  req = request(:get, path)
  req.query.update(query) if query
  yield req if block_given?
  call(req)
end

#head(path, query = nil) {|req| ... } ⇒ Object

Yields:

  • (req)


36
37
38
39
40
41
# File 'lib/hurley/client.rb', line 36

def head(path, query = nil)
  req = request(:head, path)
  req.query.update(query) if query
  yield req if block_given?
  call(req)
end

#options(path, query = nil) {|req| ... } ⇒ Object

Yields:

  • (req)


81
82
83
84
85
86
# File 'lib/hurley/client.rb', line 81

def options(path, query = nil)
  req = request(:options, path)
  req.query.update(query) if query
  yield req if block_given?
  call(req)
end

#patch(path, body = nil, ctype = nil) {|req| ... } ⇒ Object

Yields:

  • (req)


50
51
52
53
54
55
56
# File 'lib/hurley/client.rb', line 50

def patch(path, body = nil, ctype = nil)
  req = request(:patch, path)
  req.body = body if body
  req.header[:content_type] = ctype if ctype
  yield req if block_given?
  call(req)
end

#post(path, body = nil, ctype = nil) {|req| ... } ⇒ Object

Yields:

  • (req)


66
67
68
69
70
71
72
# File 'lib/hurley/client.rb', line 66

def post(path, body = nil, ctype = nil)
  req = request(:post, path)
  req.body = body if body
  req.header[:content_type] = ctype if ctype
  yield req if block_given?
  call(req)
end

#put(path, body = nil, ctype = nil) {|req| ... } ⇒ Object

Yields:

  • (req)


58
59
60
61
62
63
64
# File 'lib/hurley/client.rb', line 58

def put(path, body = nil, ctype = nil)
  req = request(:put, path)
  req.body = body if body
  req.header[:content_type] = ctype if ctype
  yield req if block_given?
  call(req)
end

#request(method, path) ⇒ Object



112
113
114
# File 'lib/hurley/client.rb', line 112

def request(method, path)
  Request.new(method, Url.join(@url, path), @header.dup, nil, @request_options.dup, @ssl_options.dup)
end