Module: Freebase

Defined in:
lib/freebase/item.rb,
lib/freebase/version.rb,
lib/freebase/category.rb,
lib/freebase/freebase.rb,
lib/freebase/item_link.rb

Defined Under Namespace

Classes: Category, Item, ItemLink

Constant Summary collapse

VERSION =
'0.0.9'
COMMON_PROXY_PORT =

Common proxy port

8080
DEFAULT_LANGUAGE =

Default language

:en

Class Method Summary collapse

Class Method Details

.item(descriptor) ⇒ Object

Returns the Item with the specified descriptor, which can be either a URI to freebase.com, an Item GUID or an Item name.

Freebase.item('Aphex Twin')
# => #<Freebase::Item:0xb73fdba0 ...>


182
183
184
# File 'lib/freebase/freebase.rb', line 182

def Freebase.item(descriptor)
  Item.from(descriptor)
end

.languageObject

Returns the language to access Freebase with.



164
165
166
# File 'lib/freebase/freebase.rb', line 164

def Freebase.language
  @@free_base_language ||= DEFAULT_LANGUAGE
end

.language=(new_language) ⇒ Object

Sets the language to access Freebase with to the new_language.



171
172
173
# File 'lib/freebase/freebase.rb', line 171

def Freebase.language=(new_language)
  @@free_base_language = new_language.to_sym
end

.open_page(uri, options = {}) ⇒ Object

Similar to Freebase.open_uri but returns an Hpricot document.



119
120
121
# File 'lib/freebase/freebase.rb', line 119

def Freebase.open_page(uri,options={})
  Hpricot(Freebase.open_uri(uri,options))
end

.open_uri(uri, options = {}) ⇒ Object

Opens the uri with the given options. The contents of the uri will be returned.

options may contain the following keys:

:user_agent_alias

The User-Agent Alias to use.

:user_agent

The User-Agent String to use.

:proxy

A Hash of proxy information which may contain the following keys:

:host

The proxy host.

:port

The proxy port.

:user

The user-name to login as.

:password

The password to login with.

Freebase.open_uri('http://www.hackety.org/')

Freebase.open_uri('http://tenderlovemaking.com/',
  :user_agent_alias => 'Linux Mozilla')
Freebase.open_uri('http://www.wired.com/',
  :user_agent => 'the future')


97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/freebase/freebase.rb', line 97

def Freebase.open_uri(uri,options={})
  headers = {}

  if options[:user_agent_alias]
    headers['User-Agent'] = WWW::Mechanize::AGENT_ALIASES[options[:user_agent_alias]]
  elsif options[:user_agent]
    headers['User-Agent'] = options[:user_agent]
  elsif Freebase.user_agent
    headers['User-Agent'] = Freebase.user_agent
  end

  proxy = (options[:proxy] || Freebase.proxy)
  if proxy[:host]
    headers[:proxy] = Freebase.proxy_uri(proxy)
  end

  return Kernel.open(uri,headers)
end

.proxyObject

Returns the Hash of proxy information.



18
19
20
21
22
23
24
25
# File 'lib/freebase/freebase.rb', line 18

def Freebase.proxy
  @@free_base_proxy ||= {
    :host => nil,
    :port => COMMON_PROXY_PORT,
    :user => nil,
    :password => nil
  }
end

.proxy_uri(proxy_info = Freebase.proxy) ⇒ Object

Creates a HTTP URI based from the given proxy_info hash. The proxy_info hash defaults to Web.proxy, if not given.

proxy_info may contain the following keys:

:host

The proxy host.

:port

The proxy port. Defaults to COMMON_PROXY_PORT, if not specified.

:user

The user-name to login as.

:password

The password to login with.



38
39
40
41
42
43
44
45
# File 'lib/freebase/freebase.rb', line 38

def Freebase.proxy_uri(proxy_info=Freebase.proxy)
  if Freebase.proxy[:host]
    return URI::HTTP.build(:host => Freebase.proxy[:host],
                           :port => Freebase.proxy[:port],
                           :userinfo => "#{Freebase.proxy[:user]}:#{Freebase.proxy[:password]}",
                           :path => '/')
  end
end

.user_agentObject

Returns the Freebase User-Agent



57
58
59
# File 'lib/freebase/freebase.rb', line 57

def Freebase.user_agent
  @@free_base_user_agent ||= Freebase.user_agent_aliases['Windows IE 6']
end

.user_agent=(agent) ⇒ Object

Sets the Freebase User-Agent to the specified agent.



64
65
66
# File 'lib/freebase/freebase.rb', line 64

def Freebase.user_agent=(agent)
  @@free_base_user_agent = agent
end

.user_agent_alias=(name) ⇒ Object

Sets the Freebase User-Agent using the specified user-agent alias name.



72
73
74
# File 'lib/freebase/freebase.rb', line 72

def Freebase.user_agent_alias=(name)
  @@free_base_user_agent = Freebase.user_agent_aliases[name.to_s]
end

.user_agent_aliasesObject

Returns the supported Freebase User-Agent Aliases.



50
51
52
# File 'lib/freebase/freebase.rb', line 50

def Freebase.user_agent_aliases
  WWW::Mechanize::AGENT_ALIASES
end

.web_agent(options = {}, &block) ⇒ Object

Creates a new WWW::Mechanize agent with the given options.

options may contain the following keys:

:user_agent_alias

The User-Agent Alias to use.

:user_agent

The User-Agent string to use.

:proxy

A Hash of proxy information which may contain the following keys:

:host

The proxy host.

:port

The proxy port.

:user

The user-name to login as.

:password

The password to login with.

Freebase.web_agent

Freebase.web_agent(:user_agent_alias => 'Linux Mozilla')
Freebase.web_agent(:user_agent => 'Google Bot')


141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/freebase/freebase.rb', line 141

def Freebase.web_agent(options={},&block)
  agent = WWW::Mechanize.new

  if options[:user_agent_alias]
    agent.user_agent_alias = options[:user_agent_alias]
  elsif options[:user_agent]
    agent.user_agent = options[:user_agent]
  elsif Freebase.user_agent
    agent.user_agent = Freebase.user_agent
  end

  proxy = (options[:proxy] || Freebase.proxy)
  if proxy[:host]
    agent.set_proxy(proxy[:host],proxy[:port],proxy[:user],proxy[:password])
  end

  block.call(agent) if block
  return agent
end