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.



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

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



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/ve.rb', line 68

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