Class: Heterocera::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/heterocera/heterocera.rb

Constant Summary collapse

READ =
"read"
WRITE =
"write"
TAKE =
"take"
XML =

get .json for free

".xml"
GZ =
".gz"
HTML =
".html"
OTHER_SUFFIXES =
[XML, GZ, HTML]

Instance Method Summary collapse

Constructor Details

#initialize(server_address) ⇒ Client

Returns a new instance of Client.



24
25
26
27
# File 'lib/heterocera/heterocera.rb', line 24

def initialize server_address
  @base_address = URI.parse server_address
  @agent        = Mechanize.new
end

Instance Method Details

#read(tags = [], suffix = "") ⇒ Object



37
38
39
# File 'lib/heterocera/heterocera.rb', line 37

def read(tags = [], suffix = "")
  get_query READ, tags, suffix
end

#server_addressObject



29
30
31
# File 'lib/heterocera/heterocera.rb', line 29

def server_address 
  @base_address
end

#server_address=(server_address) ⇒ Object



33
34
35
# File 'lib/heterocera/heterocera.rb', line 33

def server_address= server_address
  @base_address = URI.parse server_address
end

#take!(tags = [], suffix = "") ⇒ Object



61
62
63
# File 'lib/heterocera/heterocera.rb', line 61

def take!(tags = [], suffix = "")
  get_query TAKE, tags, suffix
end

#write!(tags = [], value = "") ⇒ Object

Raises:

  • (ArgumentError)


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/heterocera/heterocera.rb', line 41

def write!(tags = [], value = "")
  raise ArgumentError, 'You must provide a String or a File'  unless value.class == String || value.class == File
  raise ArgumentError, "You can't use wildcards when writing" if tags.include? '*'

  case value.class.to_s 
  when "String"
    if CGI.escape(value).bytesize < 1024
      response = get(WRITE, tags, value)
    else
      response = post(WRITE, tags, value)
    end
  when "File"
    raise TypeError, 'Please open files in binary mode' unless value.binmode?
    response = post(WRITE, tags, value)
  end

  JSON.parse response.body

end