metwit 0.0.4 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 58242f1ce3f075ea331eca28f6526161a1d56923
4
+ data.tar.gz: aed018ed68cd9672e18aa4963966f5e6f639eecc
5
+ SHA512:
6
+ metadata.gz: 72ab478b3cd93e2a04033fc2026ceb3e2c3abb1438c5012d13e77a8fe85b2de7ea2e49806a4c8954fb87d15cd5a22e458621a4e9d94c985e3a538e52fa7aa778
7
+ data.tar.gz: a70309df81b9f73aef7107a16aa58039554546f043ee4b67ac74899ae5ef18516e550b952a59d55e570afdf9e0944738df0affc89f006800afa8ad7b31a2747a
data/README.md CHANGED
@@ -22,34 +22,13 @@ Or install it yourself as:
22
22
  # First require the gem
23
23
  require 'metwit'
24
24
 
25
- # Set your api key
26
- Metwit.api_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
27
-
28
- ## How to post a Metag
29
- # If you don't authenticate a user you may not be able to post metags
30
- Metwit.authenticate("username", "password")
31
- # You need a rgeo factory to project the point coordinates
32
- factory = RGeo::Cartesian.factory
33
- # Then you can create a basic metag
34
- metag = Metwit::Metag.new(
35
- :weather => {:status => :clear},
36
- :position => factory.point(45.4, 9.1)
37
- )
38
- # Then post it to the server
39
- metag.create!
40
-
41
- ## How to get a user by id
42
- user = Metwit::User.find('id')
43
-
44
- ## How to get a metag by id
45
- metag = Metwit::Metag.find('id')
46
-
47
- ## How to get metags in a geographical region
48
- metags = Metwit::Metag.in_rect(45.4, 9.1, 45.3, 9.0)
49
-
50
- ## How to get last metags
51
- metags = Metwit::Metag.feed
52
- ```
25
+ # Set your client_id and client secret. See http://metwit.com/developers
26
+ Metwit.client_id = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
27
+ Metwit.client_secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
28
+ Metwit.get_access_token
29
+
30
+ # To retrieve the current weather status and forecasts for a geographical point
31
+ Metwit::Weather.in_location(latitude, longitude)
53
32
 
54
33
  ## Contributing
55
34
 
@@ -3,6 +3,7 @@ require "metwit/constants"
3
3
  require "metwit/version"
4
4
  require "metwit/metag"
5
5
  require "metwit/user"
6
+ require "metwit/weather"
6
7
  require 'active_support/all'
7
8
 
8
9
  # All classes are qualified in the namespace Metwit
@@ -1,32 +1,47 @@
1
+ require 'base64'
2
+
1
3
  module Metwit
2
4
  class << self
3
-
4
- # The developer api key
5
- attr_accessor :api_key
6
5
 
7
- # The user access token
6
+ # The developer application id
7
+ attr_accessor :client_id
8
+
9
+ # The developer application secret
10
+ attr_accessor :client_secret
11
+
12
+ # The access token
8
13
  attr_accessor :access_token
9
14
 
15
+ # The refresh token
16
+ attr_accessor :refresh_token
17
+
10
18
  # Tell if login was successuful
11
19
  # @return [Boolean]
12
20
  def logged?
13
21
  @logged ||= false
14
22
  end
15
23
 
16
- # Exchange the developer api key for a user access token
17
- def authenticate(username, password)
18
- @logged = false
19
- url = BASE_URL + '/auth/'
20
-
21
- response = HTTParty.post(url, :body => {:username=>username, :password=>password}.to_json, :headers => {'Authorization' => "Bearer #{@api_key}"})
22
- @access_token = response['bearer_token']
23
- @logged = true if response.code == 200
24
- response
24
+ def refresh_access_token
25
+ url = 'https://api.metwit.com/token/'
26
+ response = HTTParty.post(url, :body => {:grant_type => 'refresh_token',
27
+ :refresh_token => refresh_token},
28
+ :headers => {'Authorization' => "Basic #{Base64.strict_encode64(client_id+":"+client_secret)}"})
29
+ # TODO: check if correctly logged
30
+ @logged = true
31
+ @refresh_token = response['refresh_token'] if response['refresh_token']
32
+ @access_token = response['access_token']
25
33
  end
26
34
 
27
- def bearer_token
28
- self.logged? ? @access_token : @api_key
35
+ def get_access_token
36
+ url = 'https://api.metwit.com/token/'
37
+ response = HTTParty.post(url, :body => {:grant_type => 'client_credentials'},
38
+ :headers => {'Authorization' => "Basic #{Base64.strict_encode64(client_id+":"+client_secret)}"})
39
+ # TODO: check if correctly logged
40
+ @logged = true
41
+ @refresh_token = response['refresh_token']
42
+ @access_token = response['access_token']
29
43
  end
30
-
31
44
  end
45
+
46
+ class AccessTokenExpiredError < StandardError ; end
32
47
  end
@@ -1,3 +1,3 @@
1
1
  module Metwit
2
- BASE_URL = "https://api.metwit.me/v2"
2
+ BASE_URL = "https://api.metwit.com/v2"
3
3
  end
@@ -10,9 +10,9 @@ module Metwit
10
10
  include HTTParty
11
11
  base_uri(BASE_URL+'/metags')
12
12
 
13
- # Mandatory and guaranteed.
14
- # Weather is an Hash with two keys: :status and :details
15
- # Valid :status values are:
13
+ # Mandatory and guaranteed.
14
+ # Weather is an Hash with two keys: :status and :details
15
+ # Valid :status values are:
16
16
  # :clear, :rainy, :stormy, :snowy, :partly\_cloudy, :cloudy, :hailing, :heavy\_seas, :calm\_seas, :foggy, :snow\_flurries, :windy, :partly\_cloudy, :uknown
17
17
  # @return [{Symbol => String, Hash}] weather data
18
18
  attr_accessor :weather
@@ -46,8 +46,8 @@ module Metwit
46
46
  # The number of thanks
47
47
  # @return [Fixnum] the number of thanks
48
48
  attr_accessor :thanks_count
49
-
50
-
49
+
50
+
51
51
  def initialize(args={})
52
52
  @id = args[:id]
53
53
  @weather = args[:weather]
@@ -83,8 +83,8 @@ module Metwit
83
83
  return false unless RGeo::Feature::Point.check_type(@position)
84
84
  true
85
85
  end
86
-
87
-
86
+
87
+
88
88
  # This method return all the reognized weather statuses
89
89
  # @return [Array<Symbol>]
90
90
  def weather_statuses
@@ -95,7 +95,7 @@ module Metwit
95
95
  # @return [String]
96
96
  def to_json
97
97
  raise "metag in invalid" unless valid?
98
-
98
+
99
99
  {
100
100
  weather: {
101
101
  status: self.weather[:status].to_s.gsub(/_/,' '),
@@ -111,7 +111,7 @@ module Metwit
111
111
  raise "post failed" unless response.code == 201
112
112
  response
113
113
  end
114
-
114
+
115
115
  class << self
116
116
  # Return the metag associated with the id
117
117
  # @return [Metag]
@@ -144,7 +144,7 @@ module Metwit
144
144
  end
145
145
  metags
146
146
  end
147
-
147
+
148
148
  # Return a metag form a JSON response
149
149
  # @return [User]
150
150
  def from_json(response)
@@ -163,9 +163,9 @@ module Metwit
163
163
  # Default HTTParty options
164
164
  # @return [Hash]
165
165
  def authenticated(opts)
166
- opts.deep_merge(:headers => {'Authorization' => "Bearer #{Metwit.bearer_token}"})
166
+ # opts.deep_merge(:headers => {'Authorization' => "Bearer #{Metwit.bearer_token}"})
167
167
  end
168
-
168
+
169
169
  end
170
170
 
171
171
  # HTTParty options for authenticaded calls
@@ -173,6 +173,6 @@ module Metwit
173
173
  def authenticated(opts)
174
174
  self.class.authenticated(opts)
175
175
  end
176
-
176
+
177
177
  end
178
178
  end
@@ -6,8 +6,8 @@ module Metwit
6
6
  class User
7
7
  include HTTParty
8
8
  base_uri(BASE_URL+'/users')
9
- headers 'Authorization' => "Bearer #{Metwit.bearer_token}"
10
-
9
+ # headers 'Authorization' => "Bearer #{Metwit.bearer_token}"
10
+
11
11
  # Guaranteed.
12
12
  # The user id
13
13
  # @return [String] unique identifier of the user
@@ -88,7 +88,7 @@ module Metwit
88
88
  @has_facebook = args[:has_facebook]
89
89
  @has_twitter = args[:has_twitter]
90
90
  end
91
-
91
+
92
92
  class << self
93
93
  # Return the user associated with the id
94
94
  # @return [User]
@@ -119,7 +119,7 @@ module Metwit
119
119
  # Default HTTParty options
120
120
  # @return [Hash]
121
121
  def authenticated(opts)
122
- opts.deep_merge(:headers => {'Authorization' => "Bearer #{Metwit.bearer_token}"})
122
+ # opts.deep_merge(:headers => {'Authorization' => "Bearer #{Metwit.bearer_token}"})
123
123
  end
124
124
  end
125
125
 
@@ -1,4 +1,4 @@
1
1
  module Metwit
2
2
  # The version of the metwit gem
3
- VERSION = "0.0.4"
3
+ VERSION = "0.0.5"
4
4
  end
@@ -0,0 +1,73 @@
1
+ require 'rgeo'
2
+ require 'rgeo-geojson'
3
+ require 'httparty'
4
+ require 'json'
5
+
6
+ module Metwit
7
+ class Weather
8
+ include HTTParty
9
+ base_uri(BASE_URL+'/weather')
10
+
11
+ # Guaranteed.
12
+ # Weather is an Hash with two keys: :status and :details
13
+ # Valid :status values are:
14
+ # :clear, :rainy, :stormy, :snowy, :partly\_cloudy, :cloudy, :hailing, :heavy\_seas, :calm\_seas, :foggy, :snow\_flurries, :windy, :partly\_cloudy, :uknown
15
+ # @return [{Symbol => String, Hash}] weather data
16
+ attr_reader :weather
17
+
18
+ # Guaranteed.
19
+ # The metag timestamp.
20
+ # @return [Time] when the metag was created
21
+ attr_reader :timestamp
22
+
23
+ # Guaranteed.
24
+ # The geo location of the metag with GeoJSON format
25
+ # @return [RGeo::Feature::Point] geo location of the metag
26
+ attr_reader :geo
27
+
28
+ # The locality and country name for this geographical location
29
+ attr_reader :location
30
+
31
+ # The icon URL
32
+ attr_reader :icon
33
+
34
+ # The altitude of the sun
35
+ attr_reader :sun_altitude
36
+
37
+ # The sources for the weather status
38
+ attr_reader :sources
39
+
40
+ def initialize(args={})
41
+ args.each do |key, value|
42
+ instance_variable_set("@#{key}", value)
43
+ end
44
+ end
45
+
46
+ class << self
47
+
48
+ # An array with current weather and weather forecast
49
+ def in_location(lat, lng)
50
+ response = get("/?location_lat=#{lat}&location_lng=#{lng}", authenticated({}))
51
+ raise AccessTokenExpiredError if response.code == 401
52
+ raise "api error" unless response.code == 200
53
+ response['objects'].map {|weather_json| self.from_json(weather_json)}
54
+ rescue AccessTokenExpiredError => e
55
+ Metwit.refresh_access_token
56
+ in_location(lat, lng)
57
+ end
58
+
59
+ def from_json(response)
60
+ self.new(timestamp: Time.parse(response['timestamp']),
61
+ weather: response['weather'],
62
+ geo: RGeo::GeoJSON.decode(response['geo']),
63
+ sun_altitude: response['sun_altitude'],
64
+ sources: response['sources'],
65
+ location: response['location'])
66
+ end
67
+
68
+ def authenticated(opts)
69
+ opts.deep_merge(:headers => {'Authorization' => "Basic #{Metwit.access_token}"}) if Metwit.logged?
70
+ end
71
+ end
72
+ end
73
+ end
metadata CHANGED
@@ -1,62 +1,55 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: metwit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
5
- prerelease:
4
+ version: 0.0.5
6
5
  platform: ruby
7
6
  authors:
8
7
  - Simone D'Amico
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2012-07-21 00:00:00.000000000 Z
11
+ date: 2013-04-08 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: httparty
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ! '>='
17
+ - - '>='
20
18
  - !ruby/object:Gem::Version
21
19
  version: '0'
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ! '>='
24
+ - - '>='
28
25
  - !ruby/object:Gem::Version
29
26
  version: '0'
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: rgeo-geojson
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
- - - ! '>='
31
+ - - '>='
36
32
  - !ruby/object:Gem::Version
37
33
  version: '0'
38
34
  type: :runtime
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
- - - ! '>='
38
+ - - '>='
44
39
  - !ruby/object:Gem::Version
45
40
  version: '0'
46
41
  - !ruby/object:Gem::Dependency
47
42
  name: activesupport
48
43
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
44
  requirements:
51
- - - ! '>='
45
+ - - '>='
52
46
  - !ruby/object:Gem::Version
53
47
  version: '0'
54
48
  type: :runtime
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
51
  requirements:
59
- - - ! '>='
52
+ - - '>='
60
53
  - !ruby/object:Gem::Version
61
54
  version: '0'
62
55
  description: Ruby SDK for Metwit public APIs
@@ -78,6 +71,7 @@ files:
78
71
  - lib/metwit/metag.rb
79
72
  - lib/metwit/user.rb
80
73
  - lib/metwit/version.rb
74
+ - lib/metwit/weather.rb
81
75
  - metwit.gemspec
82
76
  - spec/fixtures/metag1234
83
77
  - spec/fixtures/user6576
@@ -86,27 +80,26 @@ files:
86
80
  - spec/spec_helper.rb
87
81
  homepage: http://metwit.com
88
82
  licenses: []
83
+ metadata: {}
89
84
  post_install_message:
90
85
  rdoc_options: []
91
86
  require_paths:
92
87
  - lib
93
88
  required_ruby_version: !ruby/object:Gem::Requirement
94
- none: false
95
89
  requirements:
96
- - - ! '>='
90
+ - - '>='
97
91
  - !ruby/object:Gem::Version
98
92
  version: '0'
99
93
  required_rubygems_version: !ruby/object:Gem::Requirement
100
- none: false
101
94
  requirements:
102
- - - ! '>='
95
+ - - '>='
103
96
  - !ruby/object:Gem::Version
104
97
  version: '0'
105
98
  requirements: []
106
99
  rubyforge_project:
107
- rubygems_version: 1.8.23
100
+ rubygems_version: 2.0.1
108
101
  signing_key:
109
- specification_version: 3
102
+ specification_version: 4
110
103
  summary: Ruby SDK for Metwit public APIs
111
104
  test_files:
112
105
  - spec/fixtures/metag1234