Module: PlayStationNetwork::API

Extended by:
API
Includes:
HTTParty
Included in:
API
Defined in:
lib/playstationnetwork/api.rb

Instance Method Summary collapse

Instance Method Details

#handle_response(&block) ⇒ Object


60
61
62
63
64
65
66
67
68
# File 'lib/playstationnetwork/api.rb', line 60

def handle_response(&block)
  yield
rescue => e
  {
    success: false,
    code: 500,
    message: e
  }
end

#parse_response(url, options, reduce_to = {}) ⇒ Object


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/playstationnetwork/api.rb', line 70

def parse_response(url, options, reduce_to = {})
  request = post(url, body: options)

  if request.success?
    begin
      if reduce_to.blank?
        JSON.parse(request)
      else
        JSON.parse(request)[reduce_to]
      end
    rescue
      raise "There was a problem parsing the JSON. Most likely an API problem."
    end
  else
    raise request.response
  end
end

#requestObject


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/playstationnetwork/api.rb', line 42

def request
  handle_response do
    raise MISSING_CONFIGURATION if PlayStationNetwork.configuration.nil?
    raise MISSING_KEY if PlayStationNetwork.configuration.key.nil?
    raise MISSING_SECRET if PlayStationNetwork.configuration.secret.nil?
    raise MISSING_URL if PlayStationNetwork.configuration.url.nil?

    default_options.update(base_uri: PlayStationNetwork.configuration.url)
    default_options.update(verify: PlayStationNetwork.configuration.verify_ssl)

    return {
      api_key: PlayStationNetwork.configuration.key,
      api_secret: PlayStationNetwork.configuration.secret,
      response_type: 'json'
    }
  end
end