Class: PlayStationNetworkAPI::Search

Inherits:
Client
  • Object
show all
Defined in:
lib/play_station_network_api/search.rb

Instance Attribute Summary

Attributes inherited from Client

#account_id, #age, #country, #default_headers, #language, #refresh_token

Instance Method Summary collapse

Methods inherited from Client

#catalog, #entitlement, #explore, #game, #get_account_devices, #get_account_id, #initialize, #search, #session, #trophy, #user

Constructor Details

This class inherits a constructor from PlayStationNetworkAPI::Client

Instance Method Details

#bucketsObject

Returns a list of buckets available to pass to .query(domains: [])



27
28
29
# File 'lib/play_station_network_api/search.rb', line 27

def buckets
  BUCKETS.keys
end

#query(query, offset: 0, limit: 50, buckets: [:CONCEPT_GAME_MOBILE_APP]) ⇒ Object

query [String] limit [Integer]

min: 1,
max: 50

country [String] language [String]



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/play_station_network_api/search.rb', line 38

def query(query, offset: 0, limit: 50, buckets: [:CONCEPT_GAME_MOBILE_APP])
  search_domains = []
  
  buckets.each do |bucket|
    search_domains.push({
      "domain": BUCKETS[bucket.to_sym],
      "pagination": {
        
        # TODO: It seems to be a Java GraphQL pagination thingy, when present it breaks the offset.
        # So let's leave it out for now as we don't really need to paginate
        # "cursor": "10"

        "pageSize": limit,
        "offset": offset
      }
    })
  end

  # https://m.np.playstation.net/api/search/v1/universalSearch
  request = post('/search/v1/universalSearch',
    body: {
      "domainRequests": search_domains,
      "age": "999",
      "countryCode": country,
      "searchTerm": query,
      "languageCode": language
    }.to_json
  )

  buckets = []

  request.dig('domainResponses').map do |domain_response|
    bucket = {}
    bucket[domain_response['domain']] = {
      total_results: domain_response['totalResultCount'],
      results: domain_response['results']
    }

    buckets.push(bucket)
  end

  return {
    query: request['prefix'],
    suggestions: request['suggestions'],
    buckets: buckets,
  }
end