class AppleReporter::Reporter
Constants
- ENDPOINT
- GZIP_MIMETYPE
Public Class Methods
new(config = {})
click to toggle source
Usage: reporter = Apple::Reporter::Sale.new(user_id: 'iscreen', access_token: 'secret', account: 'myAccount')
# File lib/apple_reporter/reporter.rb, line 9 def initialize(config = {}) @config = { sales_path: '/sales/v1', finance_path: '/finance/v1', mode: 'Robot.XML', version: '1_0' }.merge(config) end
Private Instance Methods
decompress_gzip(compress_string)
click to toggle source
# File lib/apple_reporter/reporter.rb, line 68 def decompress_gzip(compress_string) buffer = [] chunks = compress_string.unpack('H*').first.split(GZIP_MIMETYPE) if chunks.length == 1 buffer << ActiveSupport::Gzip.decompress([chunks.first].pack('H*')) else chunks.reject(&:empty?).each do |chunk| buffer << Zlib::GzipReader.new(StringIO.new([GZIP_MIMETYPE + chunk].pack('H*'))).read end end buffer.join end
fetch(api_path, params, url_params=nil)
click to toggle source
# File lib/apple_reporter/reporter.rb, line 20 def fetch(api_path, params, url_params=nil) headers = { 'Content-Type' => 'application/x-www-form-urlencoded' } payload = { userid: @config[:user_id], accesstoken: @config[:access_token], version: @config[:version], mode: @config[:mode], queryInput: "[p=Reporter.properties, #{params}]" } payload[:account] = @config[:account] if @config[:account] payload[:password] = @config[:password] if @config[:password] response = RestClient.post( "#{ENDPOINT}#{api_path}", "jsonRequest=#{payload.to_json}#{url_params}", headers ) handle_response(@config[:mode], response) rescue RestClient::ExceptionWithResponse => err if err.response handle_response(@config[:mode], err.response) else raise err end end
handle_response(mode, response)
click to toggle source
# File lib/apple_reporter/reporter.rb, line 48 def handle_response(mode, response) if response.code == 200 if response.headers[:content_type] == 'application/a-gzip' decompress_gzip(response.body) else handle_response_body_with_mode(response.body, mode) end else handle_response_body_with_mode(response.body, mode) end end
handle_response_body_with_mode(body, mode)
click to toggle source
# File lib/apple_reporter/reporter.rb, line 60 def handle_response_body_with_mode(body, mode) if mode =~ /robot\.xml/i Hash.from_xml(body) else body end end