Class: Metwit::Metag
Overview
Metags are the weather tags
Instance Attribute Summary collapse
-
#id ⇒ String
readonly
Guaranteed.
-
#position ⇒ RGeo::Feature::Point
Mandatory and guaranteed.
-
#replies_count ⇒ Fixnum
Guaranteed.
-
#thanks_count ⇒ Fixnum
Guaranteed The number of thanks.
-
#timestamp ⇒ Time
readonly
Guaranteed.
-
#user ⇒ User
Guaranteed.
-
#weather ⇒ {Symbol => String, Hash}
Mandatory and guaranteed.
Class Method Summary collapse
-
.authenticated(opts) ⇒ Hash
Default HTTParty options.
-
.feed ⇒ Array<Metag>
Return last metags posted.
-
.find(id) ⇒ Metag
Return the metag associated with the id.
-
.from_json(response) ⇒ User
Return a metag form a JSON response.
-
.in_rect(lat_n, lng_w, lat_s, lng_e) ⇒ Array<Metag>
Return metags in a geographical region.
Instance Method Summary collapse
-
#authenticated(opts) ⇒ Hash
HTTParty options for authenticaded calls.
-
#create! ⇒ Object
This metod POST a metag.
-
#initialize(args = {}) ⇒ Metag
constructor
A new instance of Metag.
-
#position_valid? ⇒ Boolean
This method check if the position is valid.
-
#to_json ⇒ String
This method encode metag in json for submission.
-
#valid? ⇒ Boolean
This method validates the metag for the submission.
-
#weather_statuses ⇒ Array<Symbol>
This method return all the reognized weather statuses.
-
#weather_valid? ⇒ Boolean
This method check if the weathear is valid.
Constructor Details
#initialize(args = {}) ⇒ Metag
Returns a new instance of Metag.
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/metwit/metag.rb', line 51 def initialize(args={}) @id = args[:id] @weather = args[:weather] @position = args[:position] @timestamp = args[:timestamp] @weather = args[:weather] @user = args[:user] @replies_count = args[:replies_count] @thanks_count = args[:thanks_count] end |
Instance Attribute Details
#id ⇒ String (readonly)
Guaranteed. The metag id
23 24 25 |
# File 'lib/metwit/metag.rb', line 23 def id @id end |
#position ⇒ RGeo::Feature::Point
Mandatory and guaranteed. The geo location of the metag with GeoJSON format
33 34 35 |
# File 'lib/metwit/metag.rb', line 33 def position @position end |
#replies_count ⇒ Fixnum
Guaranteed. The number of replies
43 44 45 |
# File 'lib/metwit/metag.rb', line 43 def replies_count @replies_count end |
#thanks_count ⇒ Fixnum
Guaranteed The number of thanks
48 49 50 |
# File 'lib/metwit/metag.rb', line 48 def thanks_count @thanks_count end |
#timestamp ⇒ Time (readonly)
Guaranteed. The metag timestamp.
28 29 30 |
# File 'lib/metwit/metag.rb', line 28 def @timestamp end |
#user ⇒ User
Guaranteed. The issuer of the metag.
38 39 40 |
# File 'lib/metwit/metag.rb', line 38 def user @user end |
#weather ⇒ {Symbol => String, Hash}
Mandatory and guaranteed. Weather is an Hash with two keys: :status and :details Valid :status values are: :clear, :rainy, :stormy, :snowy, :partly_cloudy, :cloudy, :hailing, :heavy_seas, :calm_seas, :foggy, :snow_flurries, :windy, :partly_cloudy, :uknown
18 19 20 |
# File 'lib/metwit/metag.rb', line 18 def weather @weather end |
Class Method Details
.authenticated(opts) ⇒ Hash
Default HTTParty options
165 166 167 |
# File 'lib/metwit/metag.rb', line 165 def authenticated(opts) # opts.deep_merge(:headers => {'Authorization' => "Bearer #{Metwit.bearer_token}"}) end |
.feed ⇒ Array<Metag>
Return last metags posted
138 139 140 141 142 143 144 145 146 |
# File 'lib/metwit/metag.rb', line 138 def feed response = get('/', authenticated({})) raise "feed error" unless response.code == 200 = [] response['objects'].each do || << self.from_json() end end |
.find(id) ⇒ Metag
Return the metag associated with the id
118 119 120 121 122 |
# File 'lib/metwit/metag.rb', line 118 def find(id) response = get("/#{id}/", authenticated({})) raise "http error" unless response.code == 200 self.from_json(response) end |
.from_json(response) ⇒ User
Return a metag form a JSON response
150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/metwit/metag.rb', line 150 def from_json(response) args = { id: response['id'], timestamp: Time.parse(response['timestamp']), weather: {status: response['weather']['status'].gsub(/ /, '_').to_sym}, position: RGeo::GeoJSON.decode(response['geo']), user: User.from_json(response['user']), replies_count: response['replies_count'], thanks_count: response['thanks_count'], } Metag.new(args) end |
.in_rect(lat_n, lng_w, lat_s, lng_e) ⇒ Array<Metag>
Return metags in a geographical region
126 127 128 129 130 131 132 133 134 |
# File 'lib/metwit/metag.rb', line 126 def in_rect(lat_n, lng_w, lat_s, lng_e) response = get('/', authenticated(:query => {:rect => "#{lat_n},#{lng_w},#{lat_s},#{lng_e}"})) raise "in_rect error" unless response.code == 200 = [] response['objects'].each do || << self.from_json() end end |
Instance Method Details
#authenticated(opts) ⇒ Hash
HTTParty options for authenticaded calls
173 174 175 |
# File 'lib/metwit/metag.rb', line 173 def authenticated(opts) self.class.authenticated(opts) end |
#create! ⇒ Object
This metod POST a metag
108 109 110 111 112 113 |
# File 'lib/metwit/metag.rb', line 108 def create! raise "invalid metag" unless self.valid? response = self.class.post('/', authenticated(:body => self.to_json, :headers => {'Content-Type' => 'application/json'})) raise "post failed" unless response.code == 201 response end |
#position_valid? ⇒ Boolean
This method check if the position is valid
81 82 83 84 85 |
# File 'lib/metwit/metag.rb', line 81 def position_valid? return false if @position.nil? return false unless RGeo::Feature::Point.check_type(@position) true end |
#to_json ⇒ String
This method encode metag in json for submission
96 97 98 99 100 101 102 103 104 105 |
# File 'lib/metwit/metag.rb', line 96 def to_json raise "metag in invalid" unless valid? { weather: { status: self.weather[:status].to_s.gsub(/_/,' '), }, geo: RGeo::GeoJSON.encode(self.position), }.to_json end |
#valid? ⇒ Boolean
This method validates the metag for the submission
64 65 66 67 68 |
# File 'lib/metwit/metag.rb', line 64 def valid? return false unless weather_valid? return false unless position_valid? true end |
#weather_statuses ⇒ Array<Symbol>
This method return all the reognized weather statuses
90 91 92 |
# File 'lib/metwit/metag.rb', line 90 def weather_statuses [:clear, :rainy, :stormy, :snowy, :partly_cloudy, :cloudy, :hailing, :heavy_seas, :calm_seas, :foggy, :snow_flurries, :windy, :partly_cloudy] end |
#weather_valid? ⇒ Boolean
This method check if the weathear is valid
72 73 74 75 76 77 |
# File 'lib/metwit/metag.rb', line 72 def weather_valid? return false if @weather.nil? return false if @weather[:status].nil? return false unless weather_statuses.include?(@weather[:status]) true end |