class PS::Api::Json

Attributes

apikey[R]
company_name[R]
env[R]
userkey[R]

Public Class Methods

new() click to toggle source
# File lib/ps/api/json.rb, line 8
def initialize 
  @apikey = nil
  @userkey = nil
  @company_name = nil
  @env = "production"
end

Public Instance Methods

date?(object) click to toggle source
# File lib/ps/api/json.rb, line 20
def date?(object)
  object.instance_of?(String) && object.include?("Date")
end
parse_date(str) click to toggle source

do some conversion for the ASP.net json dates

# File lib/ps/api/json.rb, line 25
def parse_date(str)             
  Time.at(str[/([0-9]+)-([0-9]+)/,1].to_i/1000)
end
request(method, params={}) click to toggle source
# File lib/ps/api/json.rb, line 15
def request(method, params={})
  post_response = self.class.post(request_url(method), options_hash(params)).parsed_response
  post_response['d'] || post_response
end

Private Instance Methods

format_date(date) click to toggle source

format msdn.microsoft.com/en-us/library/bb299886.aspx#intro_to_json_sidebarb

# File lib/ps/api/json.rb, line 46
def format_date(date)
  "/Date(#{(date.to_i*1000)}-0600)/"
end
format_request_dates(request) click to toggle source

TODO refactor

# File lib/ps/api/json.rb, line 31
def format_request_dates(request)
  request.each do |request_key,request_value|
    if request_value.instance_of? Hash then
      request_value.each do |obj_key, obj_value|
        if obj_value.instance_of? Time then
          request[request_key][obj_key] = format_date(obj_value)
        end
      end
    elsif request_value.instance_of? Time then
      request[request_key] = format_date(request_value)
    end
  end
end
header(content_length) click to toggle source
# File lib/ps/api/json.rb, line 68
def header(content_length)
  {
    'Content-Type' => "application/json;charset=utf-8",
    'Accept' => "application/json",
    'User-Agent' => @company_name,
    'Content-Length' => content_length
  }
end
name() click to toggle source
# File lib/ps/api/json.rb, line 64
def name 
  self.class.name.split('::').last.downcase
end
options_hash(post_data) click to toggle source
# File lib/ps/api/json.rb, line 50
def options_hash(post_data)
  post_data[:apikey] = @apikey
  post_data[:userkey] = @userkey
  post_data = format_request_dates(post_data).to_json
  { 
    :body => post_data, 
    :headers => header(post_data.to_json.length.to_s)
  }
end
request_url(method) click to toggle source
# File lib/ps/api/json.rb, line 60
def request_url(method)
  "#{Base.host()}/#{name()}/#{method}"
end