Class: Kiik::Client
- Inherits:
-
Object
- Object
- Kiik::Client
- Defined in:
- lib/kiik/client.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
Returns the value of attribute options.
-
#site ⇒ Object
Returns the value of attribute site.
-
#token ⇒ Object
Returns the value of attribute token.
Instance Method Summary collapse
- #authorization_header(params = {}) ⇒ Object
-
#connection ⇒ Object
The Faraday connection object.
-
#initialize(token, options = {}, &block) ⇒ Client
constructor
A new instance of Client.
- #request(verb, url, opts = {}) ⇒ Object
Constructor Details
#initialize(token, options = {}, &block) ⇒ Client
Returns a new instance of Client.
8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/kiik/client.rb', line 8 def initialize(token, ={}, &block) opts = .dup @token = token @site = opts.delete(:site) ssl = opts.delete(:ssl) @options = {:connection_opts => {}, :connection_build => block, :max_redirects => 5, :raise_errors => true}.merge(opts) @options[:connection_opts][:ssl] = ssl if ssl end |
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
6 7 8 |
# File 'lib/kiik/client.rb', line 6 def @options end |
#site ⇒ Object
Returns the value of attribute site.
5 6 7 |
# File 'lib/kiik/client.rb', line 5 def site @site end |
#token ⇒ Object
Returns the value of attribute token.
6 7 8 |
# File 'lib/kiik/client.rb', line 6 def token @token end |
Instance Method Details
#authorization_header(params = {}) ⇒ Object
36 37 38 |
# File 'lib/kiik/client.rb', line 36 def (params = {}) params.merge({ 'Authorization' => "Token token=#{token}" }) end |
#connection ⇒ Object
The Faraday connection object
26 27 28 29 30 31 32 33 34 |
# File 'lib/kiik/client.rb', line 26 def connection @connection ||= begin conn = Faraday.new(site, [:connection_opts]) conn.build do |b| [:connection_build].call(b) end if [:connection_build] conn end end |
#request(verb, url, opts = {}) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 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 |
# File 'lib/kiik/client.rb', line 40 def request(verb, url, opts = {}) connection.response :logger, ::Logger.new($stdout) if ENV['OAUTH_DEBUG'] == 'true' url = connection.build_url(url, opts[:params]).to_s Kiik::Logger.info(url) response = connection.run_request(verb, url, opts[:body], (opts[:headers])) do |req| Kiik::Logger.inspect(req) yield(req) if block_given? end Kiik::Logger.inspect(response) response = Response.new(response, :parse => opts[:parse]) case response.status when 301, 302, 303, 307 opts[:redirect_count] ||= 0 opts[:redirect_count] += 1 return response if opts[:redirect_count] > [:max_redirects] if response.status == 303 verb = :get opts.delete(:body) end request(verb, response.headers['location'], opts) when 200..299, 300..399 # on non-redirecting 3xx statuses, just return the response response when 400..599 error = Error.new(response) fail(error) if opts.fetch(:raise_errors, [:raise_errors]) response.error = error response else error = Error.new(response) fail(error, "Unhandled status code value of #{response.status}") end end |