Class: Tagalus::Account

Inherits:
Object
  • Object
show all
Includes:
CanParse
Defined in:
lib/tagalus.rb

Overview

Account

Encapsulates a user’s account on tagal.us. Currently the tagal.us API is rather limited, but it is fully supported. Example usage:

acc = tagalus::Account.new("1290185015890")
acc.define("apisandbox") #=> <Definition>
acc.post_comment "apisandbox", "Testing, 1,2,3!" #=> <Comment>

Instance Method Summary collapse

Methods included from CanParse

#xml_attribute, #xml_content, #xml_doc, #xpath

Constructor Details

#initialize(key = "") ⇒ Account

Creates a new Account object, with API Key key. This key is necessary to create tags, definitions, and comments on the server. Read-only operations don’t require a key.



71
72
73
# File 'lib/tagalus.rb', line 71

def initialize(key="")
  @api_key = key
end

Instance Method Details

#all_definitions(tag) ⇒ Object

Retrieves all the definitions for the tag with name or id tag



83
84
85
86
87
88
89
90
91
92
# File 'lib/tagalus.rb', line 83

def all_definitions tag
  path = "/definition/#{tag}/show.xml"
  doc = http_get path
  
  definitions = []
  xpath(doc,"//definition").each do |entry|
    definitions << Definition.new(:xml => entry)
  end
  definitions
end

#comments(tag) ⇒ Object

Retrieves all the comments for the tag with name or id tag



95
96
97
98
99
100
101
102
103
104
# File 'lib/tagalus.rb', line 95

def comments tag
  path = "/comment/#{tag}/show.xml"
  doc = http_get path
  
  comments = []
  xpath(doc, "//comment").each do |entry|
    comments << Comment.new(:xml => entry)
  end
  comments
end

#create_tag(tag, definition) ⇒ Object

creates a tag on tagal.us. The tag must not have already been created, or an error will be thrown. tag and definition are both strings.



129
130
131
132
133
134
135
136
137
138
# File 'lib/tagalus.rb', line 129

def create_tag tag, definition
  tag_params =  { :the_tag => tag, :the_definition => definition } 
  path = "/tag/create.xml"
  doc = http_post path, tag_params
  
  definition = Definition.new(:xml => xpath(doc,"//definition").first)
  result = Tag.new(:xml => doc)
  result.definitions = [definition]
  result
end

#define(tag) ⇒ Object

Retrieves the most authoritative definition of the tag with name or id tag



76
77
78
79
80
# File 'lib/tagalus.rb', line 76

def define tag
  path = "/tag/#{tag}/show.xml"
  doc = http_get path
  Definition.new(:xml => xpath(doc,"//definition").first)
end

#http_get(path, query_params = {}) ⇒ Object

Helper method for retrieving URLs via GET while escaping parameters and including API-specific parameters

Raises:

  • (tagalus::tagalusError)


142
143
144
145
146
147
148
149
# File 'lib/tagalus.rb', line 142

def http_get(path, query_params = {})
  query_params.merge!(:api_key => @api_key, :api_version => "0001")
  http = Net::HTTP.new API_ROOT
  path = path + "?" + URI.escape(query_params.map {|k,v| "#{k}=#{v}"}.join("&"), /[^-_!~*'()a-zA-Z\d;\/?:@&=+$,\[\]]/n)
  resp = http.get(path)
  raise tagalus::tagalusError.new("Error while querying the path #{path}") if resp.body =~ /something went wrong/
  xml_doc(resp.body)
end

#http_post(path, query_params = {}) ⇒ Object

Helper method for retrieving URLs via POST while escaping parameters and including API-specific parameters



153
154
155
156
157
158
159
# File 'lib/tagalus.rb', line 153

def http_post(path, query_params = {})
  query_params.merge!(:api_key => @api_key, :api_version => "0001")
  http = Net::HTTP.new API_ROOT
  data = URI.escape(query_params.map {|k,v| "#{k}=#{v}"}.join("&"), /[^-_!~*'()a-zA-Z\d;\/?:@&=+$,\[\]]/n)
  resp = http.post(path, data)
  xml_doc(resp.body)
end

#post_comment(tag, comment) ⇒ Object

creates a comment on tagal.us. The tag must have already been created, or an error will be thrown. comment is a string. tag can be either a tagalus::Tag, String, or Fixnum.



108
109
110
111
112
113
114
# File 'lib/tagalus.rb', line 108

def post_comment tag, comment
  tag_params = (tag.is_a? String) ? { :the_tag => tag } : {:tag_id => (tag.is_a? Tag) ? tag.id : tag }
  tag_params.merge! :the_comment => comment
  path = "/comment/create.xml"
  doc = http_post path, tag_params
  Comment.new(:xml => doc)
end

#post_definition(tag, definition) ⇒ Object

defines a tag on tagal.us. The tag must have already been created, or an error will be thrown. definition is a string. tag can be either a tagalus::Tag, String, or Fixnum.



118
119
120
121
122
123
124
125
# File 'lib/tagalus.rb', line 118

def post_definition tag, definition
  tag_params = (tag.is_a? String) ? { :the_tag => tag } : {:tag_id => (tag.is_a? Tag) ? tag.id : tag }
  tag_params.merge! :the_definition => definition
  path = "/definition/create.xml"
  doc = http_post path, tag_params
  puts doc.to_s
  Definition.new(:xml => doc)
end