Class: Muddyit::Collections::Collection::Pages

Inherits:
Generic
  • Object
show all
Defined in:
lib/muddyit/collections/pages.rb

Defined Under Namespace

Classes: Page

Instance Attribute Summary

Attributes inherited from Generic

#attributes

Attributes inherited from Base

#access_token, #access_token_secret, #auth_type, #consumer_key, #consumer_secret, #password, #rest_endpoint, #username

Instance Method Summary collapse

Methods inherited from Generic

#initialize, #method_missing

Methods inherited from Base

#collections, #extract, #initialize, #send_request

Constructor Details

This class inherits a constructor from Muddyit::Generic

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Muddyit::Generic

Instance Method Details

#create(doc, options = {}) ⇒ Object

submit a page or text for categorisation

Params

  • options (Required)



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/muddyit/collections/pages.rb', line 52

def create(doc, options = {})

  document = {}
  if doc.is_a? Hash
    unless doc[:uri] || doc[:text]
      raise
    end
    document = doc
  elsif doc.is_a? String
    if doc =~ /^http:\/\//
      document[:uri] = doc
    else
      document[:text] = doc
    end
  end

  # Ensure we get content_data as well
  options[:include_content] = true

  body = { :page => document.merge!(:options => options) }
  api_url = "/collections/#{self.collection.attributes[:token]}/pages/"
  response = @muddyit.send_request(api_url, :post, {}, body.to_json)
  return Muddyit::Collections::Collection::Pages::Page.new(@muddyit, response['page'].merge!(:collection => self.collection))
end

#find(type, options = {}) ⇒ Object

find a specific page from the collection

Params

  • type (Required)

    one of :all or a page identifier
    


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/muddyit/collections/pages.rb', line 9

def find(type, options = {})
  raise 'no type specified' if type.nil?

  if type.is_a? Symbol
    case type
    when :all
      api_url = "/collections/#{self.collection.attributes[:token]}/pages"
      if block_given?
        token = nil
        begin
        response = @muddyit.send_request(api_url, :get, options.merge!(:page => token))
        response['pages'].each { |page|
          yield Muddyit::Collections::Collection::Pages::Page.new(@muddyit, page.merge!(:collection => self.collection))
        }
        token = response['next_page']
        # Need to figure out which of the below actually occurs
        end while !token.nil? || !token == ''
      else
        api_url = "/collections/#{self.collection.attributes[:token]}/pages"
        response = @muddyit.send_request(api_url, :get, options)

        pages = []
        response['pages'].each { |page| pages.push Muddyit::Collections::Collection::Pages::Page.new(@muddyit, page.merge!(:collection => self.collection)) }
        return { :next_page => response['next_page'], :pages => pages }
      end
    else
      raise 'invalid type specified'
    end

  elsif type.is_a? String
    api_url = "/collections/#{self.collection.attributes[:token]}/pages/#{type}"
    response = @muddyit.send_request(api_url, :get, {})
    response.has_key?('identifier') ? Muddyit::Collections::Collection::Pages::Page.new(@muddyit, response.merge!(:collection => self.collection)) : nil
  end
end

#find_by_entities(uris, options = {}, &block) ⇒ Object

find all pages with specified entities

Params

  • uris (Required) an array of dbpedia URIs

  • options (Optional)



97
98
99
# File 'lib/muddyit/collections/pages.rb', line 97

def find_by_entities(uris, options = {}, &block)
  queryAllWithURI(uris.join(','), options, &block)
end

#find_by_entity(uri, options = {}, &block) ⇒ Object

find all pages with specified entity

Params

  • uri (Required) a dbpedia URI

  • options (Optional)



85
86
87
# File 'lib/muddyit/collections/pages.rb', line 85

def find_by_entity(uri, options = {}, &block)
  queryAllWithURI(uri, options, &block)
end

#find_by_term(term, options = {}, &block) ⇒ Object

find all pages with specified term

Params

  • term (Required) a string e.g. ‘Gordon Brown’

  • options (Optional)



109
110
111
# File 'lib/muddyit/collections/pages.rb', line 109

def find_by_term(term, options = {}, &block)
  queryAllWithTerm(term, options, &block)
end

#find_by_terms(terms, options = {}, &block) ⇒ Object

find all pages with specified terms

Params

  • terms (Required) an array of strings e.g. [‘Gordon Brown’, ‘Tony Blair’]

  • options (Optional)



121
122
123
# File 'lib/muddyit/collections/pages.rb', line 121

def find_by_terms(terms, options = {}, &block)
  queryAllWithTerm(terms.join(','), options, &block)
end