Class: Arcus::Api::Request

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command_name, params, callbacks) ⇒ Request

Returns a new instance of Request.



54
55
56
57
58
59
60
61
# File 'lib/arcus/api.rb', line 54

def initialize(command_name, params, callbacks)
  @command_name = command_name
  @params = params
  @api_uri = Arcus::Api.settings.api_uri
  @api_key = Arcus::Api.settings.api_key
  @api_secret = Arcus::Api.settings.api_secret
  @callbacks = callbacks
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



52
53
54
# File 'lib/arcus/api.rb', line 52

def api_key
  @api_key
end

#api_secretObject

Returns the value of attribute api_secret.



52
53
54
# File 'lib/arcus/api.rb', line 52

def api_secret
  @api_secret
end

#api_uriObject

Returns the value of attribute api_uri.



52
53
54
# File 'lib/arcus/api.rb', line 52

def api_uri
  @api_uri
end

#callbacksObject

Returns the value of attribute callbacks.



52
53
54
# File 'lib/arcus/api.rb', line 52

def callbacks
  @callbacks
end

#command_nameObject

Returns the value of attribute command_name.



52
53
54
# File 'lib/arcus/api.rb', line 52

def command_name
  @command_name
end

#paramsObject

Returns the value of attribute params.



52
53
54
# File 'lib/arcus/api.rb', line 52

def params
  @params
end

Instance Method Details

#fetch(response_type = :object) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/arcus/api.rb', line 89

def fetch(response_type = :object)
  @params[:response] =
      case response_type
        when :json, :yaml, :prettyjson, :object
          "json"
        when :xml, :prettyxml
          "xml"
      end
  http = Net::HTTP.new(@api_uri.host, @api_uri.port)
  http.read_timeout = 30 
  http.open_timeout = 30 

  if @api_uri.scheme == "https"
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end
  params = @params.merge({:command => @command_name})
  req_url = @api_uri.path + "?" + generate_params_str(params, @api_key, @api_secret)
  Arcus.log.debug { "Sending: #{req_url}" } if Arcus::Api.settings.verbose
  response = begin
    http.get(req_url)
  rescue Timeout::Error => e
    e.instance_eval do
      class << self
        attr_accessor :api_uri
      end
    end
    e.api_uri = @api_uri
    raise e
  end
  Arcus.log.debug { "Received: #{response.body}" } if Arcus::Api.settings.verbose
  response.instance_eval do
    class << self
      attr_accessor :response_type
    end

    def body
      case @response_type
        when :yaml
          YAML::dump(JSON.parse(super))
        when :prettyjson
          JSON.pretty_generate(JSON.parse(super))
        when :prettyxml
          Nokogiri::XML(super, &:noblanks)
        when :xml, :json
          super
        when :object
          JSON.parse(super)
      end
    end
  end
  response.response_type = response_type
  if response.is_a?(Net::HTTPSuccess)
    callbacks[:success].call(response) if callbacks[:success]
  else
    callbacks[:failure].call(response) if callbacks[:failure]
  end
  response.body if response.is_a?(Net::HTTPSuccess)
end

#generate_params_str(params, api_key = nil, api_secret = nil) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/arcus/api.rb', line 81

def generate_params_str(params, api_key = nil, api_secret = nil)
  if api_key && api_secret
    params[:apiKey] = @api_key
    params[:signature] = generate_signature(params, api_secret)
  end
  parameterize(params)
end

#generate_signature(params, api_secret) ⇒ Object



63
64
65
66
67
68
# File 'lib/arcus/api.rb', line 63

def generate_signature(params, api_secret)
  sorted_params = params.map { |k, v| [k, CGI.escape(v.to_s).gsub('+', '%20').downcase] }.sort_by { |key, value| key.to_s }
  data = parameterize(sorted_params, false).downcase
  hash = OpenSSL::HMAC.digest('sha1', api_secret, data)
  Base64.encode64(hash).chomp
end

#parameterize(params, escape = true) ⇒ Object



70
71
72
73
74
75
76
77
78
79
# File 'lib/arcus/api.rb', line 70

def parameterize(params, escape=true)
  params = params.collect do |k, v|
    if escape
      "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}"
    else
      "#{k}=#{v}"
    end
  end
  params.join('&')
end