Class: Wordpress::Client

Inherits:
Base
  • Object
show all
Includes:
API
Defined in:
lib/wordpress/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from API

included

Methods inherited from Base

#metaclass

Constructor Details

#initializeClient

Returns a new instance of Client.



16
17
18
19
20
21
22
23
24
# File 'lib/wordpress/client.rb', line 16

def initialize
  @conn = Faraday.new do |faraday|
    faraday.use Faraday::Response::Logger, logger
    faraday.request :multipart
    faraday.request :url_encoded
    #faraday.response :gzip
    faraday.adapter Faraday.default_adapter
  end
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



14
15
16
# File 'lib/wordpress/client.rb', line 14

def access_token
  @access_token
end

Instance Method Details

#call(request, options = {}) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/wordpress/client.rb', line 26

def call(request, options = {})
  debug "Request: #{request.inspect}"

  response = @conn.send(request.method) do |req|

    case req.method
      when :delete, :get
        req.url(request.url, request.params)
      when :put, :post
        req.path = request.url
        req.body = request.body unless request.body.empty?
    end


    if options[:bearer_token_request] && !bearer_auth_header.nil?
      req.headers['Authorization'] = bearer_auth_header
    end
    #req.headers['Accept-Encoding'] = 'gzip,deflate'
    req.options[:timeout] = 10
    req.options[:open_timeout] = 5
  end

  begin
    json = MultiJson.load(response.body)
    if json.nil? || json == ''
      raise Wordpress::ResponseError
    end
  rescue MultiJson::LoadError => e
    raise Wordpress::ResponseError, {'error' => e, 'message' => "Can not parse the response: #{response.body.inspect}"}
  end

  debug "Response: #{json}"

  json
end