Class: Supermarket::Session
- Inherits:
-
Object
- Object
- Supermarket::Session
- Defined in:
- lib/supermarket/session.rb
Constant Summary collapse
- @@order_type =
{ :none => Market::AppsRequest::OrderType.value_of(0), :popular => Market::AppsRequest::OrderType.value_of(1), :newest => Market::AppsRequest::OrderType.value_of(2), :featured => Market::AppsRequest::OrderType.value_of(3) }
Instance Attribute Summary collapse
-
#_session ⇒ Object
readonly
Returns the value of attribute _session.
Class Method Summary collapse
Instance Method Summary collapse
-
#all_comments(app_id, c = []) ⇒ Object
fetch all available comments.
- #categories ⇒ Object
- #comments(app_id, start = 0, count = 10) ⇒ Object
- #get_app(app_id) ⇒ Object
- #image(app_id, usage = :screenshot, image_id = '1') ⇒ Object
- #image_data(app_id, usage = :screenshot, image_id = '1') ⇒ Object
-
#initialize(opts = {}) ⇒ Session
constructor
A new instance of Session.
- #order_type(type_sym) ⇒ Object
-
#pkg_to_app_id(package) ⇒ Object
Resolves a package name to a package id.
- #search(query = nil, categid = nil, extended = true, start = 0, count = 10, order = :popular) ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ Session
Returns a new instance of Session.
29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/supermarket/session.rb', line 29 def initialize(opts={}) opts.merge!(self.class.config) @_session = MarketSession.new @_session.getContext().setAndroidId("3000000000000000"); if opts['authToken'] @_session.setAuthSubToken(opts['authToken']) else raise "Need login and password" unless opts['login'] && opts['password'] @_session.login(opts['login'], opts['password']) end end |
Instance Attribute Details
#_session ⇒ Object (readonly)
Returns the value of attribute _session.
20 21 22 |
# File 'lib/supermarket/session.rb', line 20 def _session @_session end |
Class Method Details
.config ⇒ Object
46 47 48 49 50 51 52 53 54 55 |
# File 'lib/supermarket/session.rb', line 46 def self.config @config ||= begin if defined? $servlet_context { 'login' => $servlet_context.getInitParameter('market_login') , 'password' => $servlet_context.getInitParameter('market_password') } else File.exists?(config_file) ? YAML.load_file(config_file) : {} end end end |
.config_file ⇒ Object
42 43 44 |
# File 'lib/supermarket/session.rb', line 42 def self.config_file File.join(ENV['HOME'], '.supermarket.yml') end |
Instance Method Details
#all_comments(app_id, c = []) ⇒ Object
fetch all available comments
110 111 112 113 114 115 116 117 118 |
# File 'lib/supermarket/session.rb', line 110 def all_comments(app_id, c=[]) comments_resp = comments(app_id, c.size, 10) c += comments_resp.comments_list.to_a if comments_resp.entriesCount == c.size c else all_comments(app_id, c) end end |
#categories ⇒ Object
88 89 90 91 |
# File 'lib/supermarket/session.rb', line 88 def categories() request = Market::CategoriesRequest.newBuilder().build() execute(request) end |
#comments(app_id, start = 0, count = 10) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/supermarket/session.rb', line 93 def comments(app_id, start=0, count=10) raise ArgumentError, "need app id" unless app_id request = Market::CommentsRequest.newBuilder(). setAppId(app_id). setStartIndex(start). setEntriesCount(count).build() if resp = execute(request) resp else #raise ArgumentError, "request returned nil" nil end end |
#get_app(app_id) ⇒ Object
57 58 59 60 61 |
# File 'lib/supermarket/session.rb', line 57 def get_app(app_id) request_builder = Market::AppsRequest.newBuilder() request_builder.set_app_id(app_id).set_with_extended_info(true) execute(request_builder.build()) end |
#image(app_id, usage = :screenshot, image_id = '1') ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/supermarket/session.rb', line 120 def image(app_id, usage=:screenshot, image_id='1') case usage when :screenshot usage_const = Market::GetImageRequest::AppImageUsage::SCREENSHOT when :icon usage_const = Market::GetImageRequest::AppImageUsage::ICON end request = Market::GetImageRequest.newBuilder(). setAppId(pkg_to_app_id(app_id)). setImageUsage(usage_const). setImageId(image_id).build() execute(request) end |
#image_data(app_id, usage = :screenshot, image_id = '1') ⇒ Object
136 137 138 139 140 141 142 |
# File 'lib/supermarket/session.rb', line 136 def image_data(app_id, usage=:screenshot, image_id='1') if resp = image(app_id, usage, image_id) String.from_java_bytes(resp.getImageData().toByteArray()) else [] end end |
#order_type(type_sym) ⇒ Object
27 |
# File 'lib/supermarket/session.rb', line 27 def order_type(type_sym);@@order_type[type_sym];end |
#pkg_to_app_id(package) ⇒ Object
Resolves a package name to a package id
145 146 147 148 149 150 151 152 153 |
# File 'lib/supermarket/session.rb', line 145 def pkg_to_app_id(package) return package if package =~ /\A-?\d+\Z/ if app = search(package).getAppList().to_a.find { |app| app.packageName == package } app.getId() else raise ArgumentError, "could not resolve package #{package}" end end |
#search(query = nil, categid = nil, extended = true, start = 0, count = 10, order = :popular) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/supermarket/session.rb', line 64 def search(query=nil, categid=nil,extended=true, start=0, count=10, order=:popular) request_builder = Market::AppsRequest.newBuilder() if query.nil? and !categid.nil? request_builder.setCategoryId(categid) elsif !query.nil? and categid.nil? request_builder.setQuery(query) elsif !query.nil? and !categid.nil? request_builder.setQuery(query) request_builder.setCategoryId(categid) else raise "query or category_id must not be nil." end request_builder. setStartIndex(start). setEntriesCount(count). setWithExtendedInfo(extended). setOrderType(order_type(order)) execute(request_builder.build()) end |