|
| 1 | +class GuardianContent::Content < GuardianContent::Base |
| 2 | + |
| 3 | + attr_reader :title, :id, :url, :publication_date, :section_id, :body, :keywords, :fields, :headline, :tags, :attributes |
| 4 | + |
| 5 | + def initialize(attributes = {}) |
| 6 | + @attributes = attributes |
| 7 | + @id = attributes[:id] |
| 8 | + @title = attributes[:webTitle] |
| 9 | + @url = attributes[:webUrl] |
| 10 | + @section_id = attributes[:sectionId] |
| 11 | + @publication_date = DateTime.parse(attributes[:webPublicationDate]) if attributes && attributes[:webPublicationDate] |
| 12 | + @body = attributes[:body] |
| 13 | + |
| 14 | + if attributes[:tags] |
| 15 | + @tags = [] |
| 16 | + attributes[:tags].each do |tag| |
| 17 | + @tags << tag |
| 18 | + end |
| 19 | + end |
| 20 | + |
| 21 | + @fields = attributes[:fields].nested_symbolize_keys! if attributes[:fields] |
| 22 | + |
| 23 | + if attributes[:fields] |
| 24 | + @headline = attributes[:fields][:headline] |
| 25 | + end |
| 26 | + end |
| 27 | + |
| 28 | + def inspect |
| 29 | + "#<Article id: \"" + self.id.to_s + "\" title: \"" + self.title.to_s + "\" + url: \"" + self.url.to_s + "\">" |
| 30 | + end |
| 31 | + |
| 32 | + def section |
| 33 | + return GuardianContent::Section.find_by_id(self.section_id) |
| 34 | + end |
| 35 | + |
| 36 | + # Does a text search for content, with various options. |
| 37 | + # === Parameters |
| 38 | + # * <tt>:conditions</tt> - A hash which can accept <tt>:tag</tt>, <tt>:section</tt>, <tt>:from</tt> and <tt>:to</tt>, eg: <code>:conditions => {:tag => "poltiics/gordon-brown", :from => Date.yesterday}</code> |
| 39 | + # * <tt>:limit</tt> - An integer determining the maximum number of results to return (max: 50). |
| 40 | + # * <tt>:order</tt> - The order of the results. Can be <tt>:relevance</tt>, <tt>:oldest</tt> or <tt>:newest</tt> |
| 41 | + # === Examples |
| 42 | + # * GuardianContent::Content.search("election") |
| 43 | + # * GuardianContent::Content.search("election", :conditions => {:section => "politics"}) |
| 44 | + def self.search(q, options = {}) |
| 45 | + |
| 46 | + query = {} |
| 47 | + query["page-size"] = options[:limit] if options[:limit] |
| 48 | + query["order-by"] = options[:order] if options[:order] |
| 49 | + |
| 50 | + if options[:select] |
| 51 | + if options[:select][:tags] |
| 52 | + query["show-tags"] = options[:select][:tags] |
| 53 | + end |
| 54 | + if options[:select][:fields] |
| 55 | + query["show-fields"] = options[:select][:fields] |
| 56 | + end |
| 57 | + end |
| 58 | + |
| 59 | + if options[:conditions] |
| 60 | + conditions = options[:conditions] |
| 61 | + |
| 62 | + query[:tag] = conditions[:tag] if conditions[:tag] |
| 63 | + query[:section] = conditions[:section] if conditions[:section] |
| 64 | + |
| 65 | + query["from-date"] = Date.parse(conditions[:from]).to_s if conditions[:from] |
| 66 | + query["to-date"] = Date.parse(conditions[:to]).to_s if conditions[:to] |
| 67 | + |
| 68 | + end |
| 69 | + |
| 70 | + |
| 71 | + query[:format] = "json" |
| 72 | + |
| 73 | + query[:q] = q |
| 74 | + |
| 75 | + opts = {:query => query} |
| 76 | + |
| 77 | + response = self.get("/search", opts).nested_symbolize_keys![:response] |
| 78 | + |
| 79 | + results = recursively_symbolize_keys!(response[:results]) |
| 80 | + content = [] |
| 81 | + |
| 82 | + results.each do |result| |
| 83 | + content << GuardianContent::Content.new(recursively_symbolize_keys!(result)) |
| 84 | + end |
| 85 | + return content |
| 86 | + end |
| 87 | + |
| 88 | + # Fetch a Content item using its id. IDs are usually in the form of <tt>section/YYYY/month/DD/name-of-article</tt>. |
| 89 | + def self.find_by_id(id, options = {}) |
| 90 | + |
| 91 | + query = {} |
| 92 | + query["show-fields"] = "all" |
| 93 | + query["show-tags"] = "all" |
| 94 | + query[:format] = "json" |
| 95 | + opts = {:query => query} |
| 96 | + |
| 97 | + attributes = {} |
| 98 | + |
| 99 | + response = recursively_symbolize_keys!self.get("/#{id}", opts).nested_symbolize_keys![:response] |
| 100 | + |
| 101 | + content = response[:content] |
| 102 | + |
| 103 | + return GuardianContent::Content.new(recursively_symbolize_keys!(content)) |
| 104 | + |
| 105 | + end |
| 106 | + |
| 107 | +end |
0 commit comments