Class: Xignite::Service

Inherits:
Object
  • Object
show all
Extended by:
Helpers
Includes:
Helpers
Defined in:
lib/xignite/service.rb

Direct Known Subclasses

Currencies, Metals

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers

constantize, underscore

Constructor Details

#initialize(curl_response = nil) ⇒ Service

Returns a new instance of Service.



56
57
58
59
60
61
62
63
64
# File 'lib/xignite/service.rb', line 56

def initialize(curl_response=nil)
  return if curl_response.nil?
  Crack::XML.parse(curl_response.body_str).each do |klass, data|
    data = weed(data)
    Xignite.const_set(klass, Class.new(Xignite.const_get(data.class.to_s))) unless Xignite.const_defined?(klass)
    instance_variable_set("@#{underscore(klass)}", Xignite.const_get(klass).build(data, self.class.options))
    instance_eval "def #{underscore(klass)} ; @#{underscore(klass)} ; end"
  end
end

Class Attribute Details

.optionsObject

Returns the value of attribute options.



7
8
9
# File 'lib/xignite/service.rb', line 7

def options
  @options
end

Class Method Details

.get(options = {}) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/xignite/service.rb', line 19

def get(options={})
  options = options.merge('Header_Username' => Xignite.configuration.username) if Xignite.configuration.username
  querystring = options.map do |key, value|
    "#{CGI.escape(key.to_s).gsub(/%(5B|5D)/n) { [$1].pack('H*') }}=#{CGI.escape(value)}"
  end.sort * '&'
  response = Curl::Easy.http_get([endpoint, querystring].reject{|s| s == '' }.join('?'))
  new(response)
end

.post(options = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/xignite/service.rb', line 9

def post(options={})
  response = Curl::Easy.http_post(endpoint, *[].tap do |postbody|
    postbody << Curl::PostField.content('Header_Username', Xignite.configuration.username) if Xignite.configuration.username
    options.each do |key, value|
      postbody << Curl::PostField.content(key, value)
    end
  end)
  new(response)
end