Class: Ve::HTTPInterface

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

Instance Method Summary collapse

Constructor Details

#initialize(language, config = {}) ⇒ HTTPInterface

Returns a new instance of HTTPInterface.



52
53
54
55
# File 'lib/ve.rb', line 52

def initialize(language, config = {})
  @language = language
  @base_url = config[:url]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(function, *args) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/ve.rb', line 57

def method_missing(function, *args)
  url = "#{@base_url}/#{@language}/#{function}"
  uri = URI.parse(url)
  response = Net::HTTP.post_form(uri, {:text => args[0]})
  data = JSON.parse(response.body)
  result = []
  
  data.each do |obj|
    # TODO: Support transliterations
    case obj['_class']
    when 'Word'
      result << Ve::Word.new(obj['word'], obj['lemma'], obj['part_of_speech'], obj['tokens'], obj['extra'], obj['info'])
    end
  end
  
  result
end