Class: Cuenote::Api::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/cuenote/api/connection.rb

Instance Method Summary collapse

Constructor Details

#initialize(*commands) ⇒ Connection

Returns a new instance of Connection.



9
10
11
# File 'lib/cuenote/api/connection.rb', line 9

def initialize *commands
  @commands = commands
end

Instance Method Details

#bodyObject



35
36
37
38
39
40
# File 'lib/cuenote/api/connection.rb', line 35

def body
  {
    CCC: "",
    xml: build
  }
end

#buildObject



42
43
44
45
46
47
48
# File 'lib/cuenote/api/connection.rb', line 42

def build
  builder.forcast do |forcast|
    @commands.map.with_index do |command, id|
      forcast << command.build(id)
    end
  end.tap{|xml| puts xml }
end

#builderObject



31
32
33
# File 'lib/cuenote/api/connection.rb', line 31

def builder
  @builder ||= ::Builder::XmlMarkup.new
end

#configObject



62
63
64
# File 'lib/cuenote/api/connection.rb', line 62

def config
  Cuenote::Api.config
end

#passwordObject



58
59
60
# File 'lib/cuenote/api/connection.rb', line 58

def password
  config.password
end

#requestObject



23
24
25
26
27
28
29
# File 'lib/cuenote/api/connection.rb', line 23

def request
  req = Net::HTTP::Post.new(uri)
  req.basic_auth username, password
  req.content_type = 'form-data'
  req.body = body.map{|k,v| "#{k}=#{v}" }.join("&")
  req
end

#responseObject



19
20
21
# File 'lib/cuenote/api/connection.rb', line 19

def response
  @response ||= Result.new(send.body)
end

#sendObject



13
14
15
16
17
# File 'lib/cuenote/api/connection.rb', line 13

def send
  Net::HTTP.start uri.hostname, uri.port, use_ssl: (uri.scheme == 'https') do |http|
    http.request request
  end.tap {|res| puts res.body }
end

#uriObject



50
51
52
# File 'lib/cuenote/api/connection.rb', line 50

def uri
  @uri ||= URI(config.endpoint)
end

#usernameObject



54
55
56
# File 'lib/cuenote/api/connection.rb', line 54

def username
  config.username
end