Giter Site home page Giter Site logo

contentapi-ruby's Introduction

guardian-content

This gem allows you to interface with the Guardian-Content-API via Ruby.

Installation

Installation is as simple as typing the following text into a command prompt (such as Terminal):

gem install guardian-content

Alternatively, the gem can be directly downloaded from rubygems.org/gems/guardian-content

Usage

To use the library within a project, simply include the line require 'guardian-content' - you can then use any of the methods listed below.

Whilst the Guardian Content API doesn’t require an API key for basic usage, for increased rate limits, and the ability to retrieve full text content, it is recommended that you register for an API key at guardian.mashery.com/. Once authorised, you can use the key via either declaring a GUARDIAN_CONTENT_API_KEY constant, or via the key param when initialising the class.

Basic searching

To do a basic free text search of Guardian documents, simply use the following command:

articles = GuardianContent::Content.search("your search term")

If successful, this will the return an array of results, which you can loop through and access via some standard attributes:

articles.each do |article| puts article.title puts article.url end

To include additional meta-data in the results, use the :select parameter. There are two types of data you can request, tags and fields. To select all of both, use :select => {:tags => :all, :fields => :all}. You can then access the tags and keywords via attributes:

articles = GuardianContent::Content.search(“your search term”, :select => {:tags => :all, :fields => :all}) articles.first.tags.each do |tag| puts tag puts tag end puts articles.first.fields puts articles.first.fields

Retrieving an article based on id

Each article or content item has a unique id. This can be accessed either via the #id attribute on a returned search result, or you can determine it from the Guardian website itself - the id is simply everything in an article’s URL after the host name - eg for www.guardian.co.uk/world/2010/may/17/iran-nuclear-uranium-swap-turkey the id is world/2010/may/17/iran-nuclear-uranium-swap-turkey.

To retrieve an article based on its id, use the #find_by_id method:

article = GuardianContent::Content.find_by_id(“world/2010/may/17/iran-nuclear-uranium-swap-turkey”) The article’s content and meta-data can then be accessed in the same way as for search results.

Note on Patches/Pull Requests

  • Fork the project.

  • Make your feature addition or bug fix.

  • Add tests for it. This is important so I don’t break it in a future version unintentionally.

  • Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)

  • Send me a pull request. Bonus points for topic branches.

Copyright © 2010 Copyright 2010 Guardian News and Media Ltd. See LICENCE for details.

contentapi-ruby's People

Contributors

frankieroberto avatar toastkid avatar bruntonspall avatar

Stargazers

Dheeraj Reddy Vanteru avatar

Watchers

Dheeraj Reddy Vanteru avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.