Class: Juicer

Inherits:
Object
  • Object
show all
Defined in:
lib/juicer.rb,
lib/juicer/client.rb,
lib/juicer/version.rb

Defined Under Namespace

Classes: Client

Constant Summary collapse

VERSION =
"1.1.1".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key) ⇒ Juicer

Initialize the Juicer API client.

Parameters:

  • api_key (String)

    API key obtained from Apigee.



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

def initialize(api_key)
  @client = Juicer::Client.new(api_key)
end

Instance Attribute Details

#clientJuicer::Client (readonly)

Returns The HTTP client for making requests.

Returns:



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

def client
  @client
end

Instance Method Details

#article(cps_id) ⇒ Hash

Fetch an article by its CPS_ID

Parameters:

  • cps_id (String)

    the cps_id of an article.

Returns:

  • (Hash)

    the serialized article.



76
77
78
# File 'lib/juicer.rb', line 76

def article(cps_id)
  @client.request(:get, "articles/#{cps_id}")["article"]
end

#articles(opts) ⇒ Array<Hash>

Note:

The results will include all entities of an article. Therefore, if there are many results, the amount of data transferred is quite large. Just a heads up when doing queries that potentially return a large number of results.

Filter/find articles.

Parameters:

  • opts (Hash)

    a Hash of filter options.

    Possible keys are:

    • text - a search term, corresponds to Lucene syntax.
    • product - list of products to scope to. See #products.
    • content_format - list of content formats to scope to. See #formats.
    • section - list of sections to scope to. See #sections.
    • site - list of sites to scope to. See #sites.
    • published_after - only retrieve articles published after a certain date. The format is yyyy-mm-dd.
    • published_before - only retrieve articles published before a certain date. The format is yyyy-mm-dd.

Returns:

  • (Array<Hash>)

    list of articles.



67
68
69
# File 'lib/juicer.rb', line 67

def articles(opts)
  @client.request(:get, "articles", opts)["articles"]
end

#formatsArray<String>

List content formats.

Returns:

  • (Array<String>)

    list of available content formats.



42
43
44
# File 'lib/juicer.rb', line 42

def formats
  @client.request(:get, "formats")["formats"]
end

#productsArray<String>

List products.

Returns:

  • (Array<String>)

    list of available products.



35
36
37
# File 'lib/juicer.rb', line 35

def products
  @client.request(:get, "products")["products"]
end

#sectionsArray<String>

List sections.

Returns:

  • (Array<String>)

    list of available section.



28
29
30
# File 'lib/juicer.rb', line 28

def sections
  @client.request(:get, "sections")["sections"]
end

#similar_articles(cps_id, opts = {}) ⇒ Array<Hash>

Fetch articles related (similar) to an article. Uses tf-idf algorithm to find semantically similar documents.

Parameters:

  • cps_id (String)

    the cps_id of an article.

  • opts (Hash) (defaults to: {})

    a Hash of query options.

    Possible keys are:

    • size - how many results to return. Results are ordered by their "match" score, i.e how similar they are to a given article, and the top size results are returned.
    • product - a list of products to scope to. See #products.

Returns:

  • (Array<Hash>)

    list of similar articles. Defaults to 10 most similar across all products.



95
96
97
# File 'lib/juicer.rb', line 95

def similar_articles(cps_id, opts = {})
  @client.request(:get, "articles/#{cps_id}/similar", opts)["results"]
end

#similar_to(text, opts = {}) ⇒ Array<Hash>

Fetch articles related (similar) to an arbitrary blob of text. Uses tf-idf algorithm to find semantically similar documents.

Parameters:

  • text (String)

    a blob of text.

  • opts (Hash) (defaults to: {})

    a Hash of query options.

    Possible keys are:

    • size - how many results to return. Results are ordered by their "match" score, i.e how similar they are to the given blob of text, and the top size results are returned.
    • product - a list of products to scope to. See #products.

Returns:

  • (Array<Hash>)

    list of similar articles. Defaults to 10 most similar across all products.



114
115
116
117
# File 'lib/juicer.rb', line 114

def similar_to(text, opts = {})
  body = { like_text: URI.encode(text) }.to_json
  @client.request(:post, "similar_to", opts, body)["results"]
end

#sitesArray<String>

List sites.

Returns:

  • (Array<String>)

    list of available sites.



21
22
23
# File 'lib/juicer.rb', line 21

def sites
  @client.request(:get, "sites")["sites"]
end