Class: Yooda::Client

Inherits:
Object
  • Object
show all
Includes:
Domains, Keywords, Subscriptions
Defined in:
lib/yooda/client.rb

Constant Summary collapse

DEFAULT_CONNECTION_MIDDLEWARE =
[
  Faraday::Request::Multipart,
  Faraday::Request::UrlEncoded,
  FaradayMiddleware::Mashify,
  FaradayMiddleware::ParseJson
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Subscriptions

#credits, #histories

Methods included from Keywords

#keyword_informations, #keyword_market_kpi, #keywords_market_by_keyword

Methods included from Domains

#domain_informations, #domain_market_kpi, #keywords_market_by_domain

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



26
27
28
29
30
31
# File 'lib/yooda/client.rb', line 26

def initialize(options={})
  @apikey = options[:apikey] || Yooda.apikey
  # @connection_middleware = options[:connection_middleware] || Yooda.connection_middleware || []
  @connection_middleware = []
  @connection_middleware += DEFAULT_CONNECTION_MIDDLEWARE
end

Instance Attribute Details

#apikeyObject (readonly)

Returns the value of attribute apikey.



24
25
26
# File 'lib/yooda/client.rb', line 24

def apikey
  @apikey
end

Instance Method Details

#api_urlObject



44
45
46
# File 'lib/yooda/client.rb', line 44

def api_url
  "https://api.yooda.com"
end

#connectionObject



33
34
35
36
37
38
39
40
41
42
# File 'lib/yooda/client.rb', line 33

def connection
  params = {}
  params[:apikey] = @apikey if @apikey
  @connection ||= Faraday::Connection.new(:url => api_url, :params => params, :headers => default_headers) do |builder|
    # @connection_middleware.each do |middleware|
    #   builder.use *middleware
    # end
    builder.adapter Faraday.default_adapter
  end
end

#return_error_or_body(response) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/yooda/client.rb', line 48

def return_error_or_body(response)
  if response.status == 200
    response.body.content
  else
    raise Yooda::APIError.new(response.status, response.reason_phrase, response.body)
  end
end