Class: Metwit::Metag

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/metwit/metag.rb

Overview

Metags are the weather tags

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#idString (readonly)

Guaranteed. The metag id

Returns:

  • (String)

    unique identifier of the metag



23
24
25
# File 'lib/metwit/metag.rb', line 23

def id
  @id
end

#positionRGeo::Feature::Point

Mandatory and guaranteed. The geo location of the metag with GeoJSON format

Returns:

  • (RGeo::Feature::Point)

    geo location of the metag



33
34
35
# File 'lib/metwit/metag.rb', line 33

def position
  @position
end

#replies_countFixnum

Guaranteed. The number of replies

Returns:

  • (Fixnum)

    the number of replies



43
44
45
# File 'lib/metwit/metag.rb', line 43

def replies_count
  @replies_count
end

#thanks_countFixnum

Guaranteed The number of thanks

Returns:

  • (Fixnum)

    the number of thanks



48
49
50
# File 'lib/metwit/metag.rb', line 48

def thanks_count
  @thanks_count
end

#timestampTime (readonly)

Guaranteed. The metag timestamp.

Returns:

  • (Time)

    when the metag was created



28
29
30
# File 'lib/metwit/metag.rb', line 28

def timestamp
  @timestamp
end

#userUser

Guaranteed. The issuer of the metag.

Returns:

  • (User)

    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

Returns:

  • ({Symbol => String, Hash})

    weather data



18
19
20
# File 'lib/metwit/metag.rb', line 18

def weather
  @weather
end

Class Method Details

.authenticated(opts) ⇒ Hash

Default HTTParty options

Returns:

  • (Hash)


165
166
167
# File 'lib/metwit/metag.rb', line 165

def authenticated(opts)
#        opts.deep_merge(:headers => {'Authorization' => "Bearer #{Metwit.bearer_token}"})
end

.feedArray<Metag>

Return last metags posted

Returns:



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
  metags = []
  response['objects'].each do |metag_json|
    metags << self.from_json(metag_json)
  end
  metags
end

.find(id) ⇒ Metag

Return the metag associated with the id

Returns:



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

Returns:



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

Returns:



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
  metags = []
  response['objects'].each do |metag_json|
    metags << self.from_json(metag_json)
  end
  metags
end

Instance Method Details

#authenticated(opts) ⇒ Hash

HTTParty options for authenticaded calls

Returns:

  • (Hash)


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

Returns:

  • (Boolean)


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_jsonString

This method encode metag in json for submission

Returns:

  • (String)


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

Returns:

  • (Boolean)


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_statusesArray<Symbol>

This method return all the reognized weather statuses

Returns:

  • (Array<Symbol>)


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

Returns:

  • (Boolean)


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