Class: KeywordsEverywhere::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(secret_key: KeywordsEverywhere.secret_key || ENV['KW_SECRET'], market: KeywordsEverywhere.market || DEFAULT_MARKET, source: KeywordsEverywhere.source || DEFAULT_SOURCE, currency: KeywordsEverywhere.currency || DEFAULT_CURRENCY) ⇒ Client

Returns a new instance of Client.



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/keywords_everywhere/client.rb', line 8

def initialize( 
    secret_key:     KeywordsEverywhere.secret_key       || ENV['KW_SECRET'],
    market:         KeywordsEverywhere.market           || DEFAULT_MARKET,  
    source:         KeywordsEverywhere.source           || DEFAULT_SOURCE,
    currency:       KeywordsEverywhere.currency         || DEFAULT_CURRENCY 
) 

    @source = source.to_s.downcase
    @secret_key = secret_key
    @market = market.to_s.downcase
    @currency = currency.to_s.downcase
    
end

Instance Attribute Details

#currencyObject

Returns the value of attribute currency.



5
6
7
# File 'lib/keywords_everywhere/client.rb', line 5

def currency
  @currency
end

#marketObject

Returns the value of attribute market.



5
6
7
# File 'lib/keywords_everywhere/client.rb', line 5

def market
  @market
end

#responseObject (readonly)

Returns the value of attribute response.



6
7
8
# File 'lib/keywords_everywhere/client.rb', line 6

def response
  @response
end

#secret_keyObject (readonly)

Returns the value of attribute secret_key.



6
7
8
# File 'lib/keywords_everywhere/client.rb', line 6

def secret_key
  @secret_key
end

#sourceObject

Returns the value of attribute source.



5
6
7
# File 'lib/keywords_everywhere/client.rb', line 5

def source
  @source
end

Instance Method Details

#creditsObject



36
37
38
# File 'lib/keywords_everywhere/client.rb', line 36

def credits
    response.dig('credits')
end

#listObject



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

def list
    List.new(response.dig('data'))
end

#search_items(keywords) ⇒ Object

Raises:

  • (StandardError)


22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/keywords_everywhere/client.rb', line 22

def search_items(keywords)
    if keywords.is_a?(String)
        keywords = [keywords] 
    end
    raise StandardError, "Keyword Up to 100" if keywords.size > 100 # https://api.keywordseverywhere.com/docs/#/keywords/get_keywords_data
    
    client = rest_client(   method: :post, 
                            url: "https://api.keywordseverywhere.com/v1/get_keyword_data", 
                            payload: payload( kw: keywords ),
                            headers: headers )
    @response = JSON.parse(client.body)
    to_items
end

#timeObject



40
41
42
# File 'lib/keywords_everywhere/client.rb', line 40

def time
    response.dig('time')
end

#to_itemsObject



48
49
50
# File 'lib/keywords_everywhere/client.rb', line 48

def to_items
    list.to_items
end