Class: BluePotionNet

Inherits:
Object show all
Defined in:
lib/project/blue_potion_net.rb

Overview

Nice wrapper around VolleyWrapper, to use in BluePotion.

result that is returned has these attributes:

response
object
body
status_code
headers
not_modified?
success?
failure?

Examples:

# Create a session and do a single HTML get. It's better
# to use the shared session below.
app.net.get("http://google.com") do |response|
  mp response.object # <- HTML
end

# To initialize the shared session, which is best to use
# rather than the one-off above, do this. Once this
# is done, all your gets, posts, puts, etc will use this
# share session
app.net.build_shared("http://baseurl.com") do |shared|
  # You can override the serializer per request
  # Leave blank for string
  shared.serializer = :json
end

# For shared, use relative paths
app.net.get("foo.html") do |response|
  mp response.object # <- returns type you set in shared.serializer
end

# Post
app.net.post("foo/bar", your_params_hash) do |response|
  mp response.object # <- returns type you set in shared.serializer
end

# If you have built a shared session, but want to use another
# session, do this:
app.net.get("foo.html", session: app.net.single_use_session) do |response|
  mp response.object # <- returns type you set in shared.serializer
end

# Get json:
url = "http://openweathermap.org/data/2.1/find/name?q=san%20francisco"
app.net.get_json(url) do |request|
  # request.object is a hash, parsed from the json
  temp_kelvin = request.object["list"].first["main"]["temp"]
end

Class Method Summary collapse

Class Method Details

.build_shared(url, &block) ⇒ Object



70
71
72
# File 'lib/project/blue_potion_net.rb', line 70

def build_shared(url, &block)
  session_client.build_shared(PMApplication.current_application.context, url, &block)
end

.delete(url, params, opts = {}, &block) ⇒ Object



99
100
101
102
103
# File 'lib/project/blue_potion_net.rb', line 99

def delete(url, params, opts={}, &block)
  raise "[BluePotion error] You must provide a block when using app.net.delete" unless block
  ses = opts.delete(:session) || self.session
  ses.delete(url, params, opts, &block)
end

.get(url, params = {}, opts = {}, &block) ⇒ Object



74
75
76
77
78
# File 'lib/project/blue_potion_net.rb', line 74

def get(url, params={}, opts={}, &block)
  raise "[BluePotion error] You must provide a block when using app.net.get" unless block
  ses = opts.delete(:session) || self.session
  ses.get(url, params, opts, &block)
end

.get_json(url, params = {}, opts = {}, &block) ⇒ Object



80
81
82
83
84
85
# File 'lib/project/blue_potion_net.rb', line 80

def get_json(url, params={}, opts={}, &block)
  raise "[BluePotion error] You must provide a block when using app.net.get_json" unless block
  ses = opts.delete(:session) || self.session
  opts[:serializer] = :json
  ses.get(url, params, opts, &block)
end

.is_shared?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/project/blue_potion_net.rb', line 62

def is_shared?
  !session_client.shared.is_nil?
end

.post(url, params, opts = {}, &block) ⇒ Object



87
88
89
90
91
# File 'lib/project/blue_potion_net.rb', line 87

def post(url, params, opts={}, &block)
  raise "[BluePotion error] You must provide a block when using app.net.post" unless block
  ses = opts.delete(:session) || self.session
  ses.post(url, params, opts, &block)
end

.put(url, params, opts = {}, &block) ⇒ Object



93
94
95
96
97
# File 'lib/project/blue_potion_net.rb', line 93

def put(url, params, opts={}, &block)
  raise "[BluePotion error] You must provide a block when using app.net.put" unless block
  ses = opts.delete(:session) || self.session
  ses.put(url, params, opts, &block)
end

.sessionObject



58
59
60
# File 'lib/project/blue_potion_net.rb', line 58

def session
  session_client.shared ? session_client.shared : single_use_session
end

.session_clientObject



54
55
56
# File 'lib/project/blue_potion_net.rb', line 54

def session_client
  @_session_client ||= VW::SessionClient
end

.single_use_sessionObject



66
67
68
# File 'lib/project/blue_potion_net.rb', line 66

def single_use_session
  session_client.new(PMApplication.current_application.context, "")
end